From 25dc8ca24d1b0257c2dd2b757ae1944a20ef0832 Mon Sep 17 00:00:00 2001 From: George Arama <50641385+gearama@users.noreply.github.com> Date: Wed, 11 Oct 2023 08:38:29 -0700 Subject: [PATCH] Implement purge check intead of blind timeout (#5021) * implement purge check intead of blind timeput * typo * timeout --- .../sample2_backup_and_restore.cpp | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/sdk/keyvault/azure-security-keyvault-keys/test/samples/sample2-backup-and-restore/sample2_backup_and_restore.cpp b/sdk/keyvault/azure-security-keyvault-keys/test/samples/sample2-backup-and-restore/sample2_backup_and_restore.cpp index 1e79278f8..ea7abb965 100644 --- a/sdk/keyvault/azure-security-keyvault-keys/test/samples/sample2-backup-and-restore/sample2_backup_and_restore.cpp +++ b/sdk/keyvault/azure-security-keyvault-keys/test/samples/sample2-backup-and-restore/sample2_backup_and_restore.cpp @@ -70,8 +70,26 @@ int main() // You only need to wait for completion if you want to purge or recover the key. operation.PollUntilDone(std::chrono::milliseconds(2000)); keyClient.PurgeDeletedKey(rsaKeyName); - // let's wait for one minute so we know the key was purged. - std::this_thread::sleep_for(std::chrono::seconds(60)); + + // Let's wait for a bit (maximum ~5 minutes) so we know the key was purged. + try + { + uint16_t loop = 0; + // To check if the key was purged we attempt to get the key from the Key Vault. + // If we get an exception, the key was purged. + // If not, we wait a bit more,since the key is in the purge process. + // If we get more than 300 loops (~5minutes), we assume something went wrong. + while (!keyClient.GetDeletedKey(rsaKeyName).Value.Name().empty() && loop < 300) + { + std::this_thread::sleep_for(std::chrono::seconds(1)); + loop++; + } + throw std::runtime_error("Key was not purged."); + } + catch (Azure::Core::RequestFailedException const&) + { + std::cout << "\t-Key purged" << std::endl; + } // Restore the key from the file backup std::cout << "\t-Read from file." << std::endl;