diff --git a/sdk/core/azure-core/CHANGELOG.md b/sdk/core/azure-core/CHANGELOG.md index d9c4bc78a..9e4cb2280 100644 --- a/sdk/core/azure-core/CHANGELOG.md +++ b/sdk/core/azure-core/CHANGELOG.md @@ -6,6 +6,10 @@ - Added support for HTTP conditional requests `MatchConditions` and `RequestConditions`. +### Breaking Changes + +- Remove `Context::CancelWhen()`. + ### Bug Fixes - Fixed computation of the token expiration time in `BearerTokenAuthenticationPolicy`. diff --git a/sdk/core/azure-core/inc/azure/core/context.hpp b/sdk/core/azure-core/inc/azure/core/context.hpp index 9acf1cfd0..77c409965 100644 --- a/sdk/core/azure-core/inc/azure/core/context.hpp +++ b/sdk/core/azure-core/inc/azure/core/context.hpp @@ -290,6 +290,8 @@ namespace Azure { namespace Core { { } + time_point CancelWhen() const; + public: /** * @brief Construct a new context with no expiration, and no value associated. @@ -329,11 +331,6 @@ namespace Azure { namespace Core { m_contextSharedState, time_point::max(), key, std::move(value))}; } - /** - * @ - */ - time_point CancelWhen() const; - /** * @brief Get a value associated with a \p key parameter within this context or the branch of * contexts this context belongs to. diff --git a/sdk/core/azure-core/test/ut/context.cpp b/sdk/core/azure-core/test/ut/context.cpp index cbb1407b2..5775bae19 100644 --- a/sdk/core/azure-core/test/ut/context.cpp +++ b/sdk/core/azure-core/test/ut/context.cpp @@ -81,9 +81,34 @@ TEST(Context, BasicChar) EXPECT_TRUE(kind == ContextValue::ContextValueType::StdString); } +TEST(Context, ApplicationContext) +{ + Context appContext = GetApplicationContext(); + + EXPECT_FALSE(appContext.HasKey("Key")); + EXPECT_FALSE(appContext.HasKey("key")); + EXPECT_FALSE(appContext.HasKey("Value")); + EXPECT_FALSE(appContext.HasKey("value")); + EXPECT_FALSE(appContext.HasKey("1")); + EXPECT_FALSE(appContext.HasKey("")); + + auto duration = std::chrono::milliseconds(250); + EXPECT_FALSE(appContext.IsCancelled()); + std::this_thread::sleep_for(duration); + EXPECT_FALSE(appContext.IsCancelled()); + + appContext.Cancel(); + EXPECT_TRUE(appContext.IsCancelled()); + + // AppContext2 is the same context as AppContext + // The context should be cancelled + Context appContext2 = GetApplicationContext(); + EXPECT_TRUE(appContext.IsCancelled()); +} + TEST(Context, IsCancelled) { - auto duration = std::chrono::milliseconds(150); + auto duration = std::chrono::milliseconds(250); auto deadline = std::chrono::system_clock::now() + duration; Context context; @@ -93,6 +118,61 @@ TEST(Context, IsCancelled) EXPECT_TRUE(c2.IsCancelled()); } +TEST(Context, NestedIsCancelled) +{ + auto duration = std::chrono::milliseconds(250); + auto deadline = std::chrono::system_clock::now() + duration; + + Context context; + auto c2 = context.WithValue("Key", "Value"); + EXPECT_FALSE(c2.IsCancelled()); + EXPECT_TRUE(c2.HasKey("Key")); + EXPECT_FALSE(context.HasKey("Key")); + + auto c3 = context.WithDeadline(deadline); + EXPECT_FALSE(context.IsCancelled()); + EXPECT_FALSE(c2.IsCancelled()); + EXPECT_FALSE(c3.IsCancelled()); + std::this_thread::sleep_for(duration); + + EXPECT_FALSE(context.IsCancelled()); + EXPECT_FALSE(c2.IsCancelled()); + EXPECT_TRUE(c3.IsCancelled()); + + EXPECT_TRUE(c2.HasKey("Key")); + EXPECT_FALSE(context.HasKey("Key")); + EXPECT_FALSE(c3.HasKey("Key")); +} + +TEST(Context, CancelWithValue) +{ + Context context; + auto c2 = context.WithValue("Key", "Value"); + EXPECT_FALSE(context.IsCancelled()); + EXPECT_FALSE(c2.IsCancelled()); + EXPECT_TRUE(c2.HasKey("Key")); + EXPECT_FALSE(context.HasKey("Key")); + + c2.Cancel(); + EXPECT_TRUE(c2.IsCancelled()); + EXPECT_FALSE(context.IsCancelled()); + + EXPECT_TRUE(c2.HasKey("Key")); + EXPECT_FALSE(context.HasKey("Key")); +} + +TEST(Context, ThrowIfCancelled) +{ + auto duration = std::chrono::milliseconds(250); + auto deadline = std::chrono::system_clock::now() + duration; + + Context context; + auto c2 = context.WithDeadline(deadline); + EXPECT_NO_THROW(c2.ThrowIfCancelled()); + std::this_thread::sleep_for(duration); + EXPECT_THROW(c2.ThrowIfCancelled(), Azure::Core::OperationCancelledException); +} + TEST(Context, Alternative) { Context context;