Add context tests for ThrowIfCancelled and ApplicationContext (#1602)

This commit is contained in:
Rick Winter 2021-02-05 11:12:47 -08:00 committed by GitHub
parent d92e88415b
commit a57cb349eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 87 additions and 6 deletions

View File

@ -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`.

View File

@ -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.

View File

@ -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;