Simplify the Context structure (#1824)

* Simplify the Context structure
* Move Azure::Core::GetApplicationContext() to Azure::Core::Context::GetApplicationContext()
This commit is contained in:
Rick Winter 2021-03-10 15:19:23 -08:00 committed by GitHub
parent 48258f8f81
commit ec24f659c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
21 changed files with 162 additions and 415 deletions

View File

@ -18,16 +18,6 @@
namespace Azure { namespace Core {
/**
* @brief A base abstract class for the `std::unique_ptr` value representation in
* #Azure::Core::ContextValue.
*
*/
struct ValueBase
{
virtual ~ValueBase() {}
};
/**
* @brief An exception that gets thrown when some operation is cancelled.
*
@ -44,210 +34,6 @@ namespace Azure { namespace Core {
}
};
/**
* @brief Represents a value that is associated with context.
* @remark Exists as a substitute for variant which isn't available until C++17.
*/
class ContextValue {
public:
/**
* @brief A type of context value.
*/
enum class ContextValueType
{
Undefined, ///< Undefined.
Bool, ///< `bool`.
Int, ///< `int`.
StdString, ///< `std::string`
UniquePtr ///< `std::unique_ptr<ValueBase>`
};
private:
ContextValueType m_contextValueType;
union
{
bool m_b; // if m_contextValueType == ContextValueType::Bool
int m_i; // if m_contextValueType == ContextValueType::Int
std::string m_s; // if m_contextValueType == ContextValueType::StdString
std::unique_ptr<ValueBase> m_p; // if m_contextValueType == ContextValueType::UniquePtr
};
public:
#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable : 26495)
#endif
/**
* @brief Create a context value with undefined value type.
*/
ContextValue() noexcept : m_contextValueType(ContextValueType::Undefined) {}
/**
* @brief Create a context value with `bool` value type.
*
* @param b Boolean value.
*/
ContextValue(bool b) noexcept : m_contextValueType(ContextValueType::Bool), m_b(b) {}
/**
* @brief Create a context value with `int` value type.
*
* @param i Integer value.
*/
ContextValue(int i) noexcept : m_contextValueType(ContextValueType::Int), m_i(i) {}
/**
* @brief Create a context value with `std::string` value type.
*
* @param s String value.
*/
ContextValue(const std::string& s) : m_contextValueType(ContextValueType::StdString), m_s(s) {}
/**
* @brief Create a context value with `std::string` value type.
*
* @param s String value represented as 0-terminated C-string.
*/
ContextValue(const char* s) : m_contextValueType(ContextValueType::StdString), m_s(s) {}
/**
* @brief Create a context value with `std::string` value type, using `std::move` semantics.
*
* @param s String value.
*/
ContextValue(std::string&& s)
: m_contextValueType(ContextValueType::StdString), m_s(std::move(s))
{
}
/**
* @brief Create a context value with `std::unique_ptr<Azure::Core::ValueBase>` value type.
*
* @param p Smart pointer to #Azure::Core::ValueBase.
*/
template <
class DerivedFromValueBase,
typename std::
enable_if<std::is_convertible<DerivedFromValueBase*, ValueBase*>::value, int>::type
= 0>
ContextValue(std::unique_ptr<DerivedFromValueBase>&& p)
: m_contextValueType(ContextValueType::UniquePtr), m_p(std::move(p))
{
}
/**
* @brief Move constructor.
*
* @param other An rvalue reference to another instance of #Azure::Core::ContextValue.
*/
ContextValue(ContextValue&& other) noexcept : m_contextValueType(other.m_contextValueType)
{
switch (m_contextValueType)
{
case ContextValueType::Bool:
m_b = other.m_b;
break;
case ContextValueType::Int:
m_i = other.m_i;
break;
case ContextValueType::StdString:
::new (&m_s) std::string(std::move(other.m_s));
break;
case ContextValueType::UniquePtr:
::new (&m_p) std::unique_ptr<ValueBase>(std::move(other.m_p));
break;
case ContextValueType::Undefined:
break;
}
}
#if defined(_MSC_VER)
#pragma warning(pop)
#endif
/**
* @brief #Azure::Core::ContextValue destructor.
*/
~ContextValue()
{
switch (m_contextValueType)
{
case ContextValueType::StdString:
m_s.~basic_string();
break;
case ContextValueType::UniquePtr:
m_p.~unique_ptr<ValueBase>();
break;
case ContextValueType::Bool:
case ContextValueType::Int:
case ContextValueType::Undefined:
break;
}
}
ContextValue& operator=(const ContextValue& other) = delete;
/**
* @brief Get the context value.
*
* @tparam ExpectedType The type of value to get.
*/
template <class ExpectedType> const ExpectedType& Get() const noexcept;
/**
* @brief Get the context value type.
*/
ContextValueType Alternative() const noexcept { return m_contextValueType; }
};
/**
* @brief Get the context value type as boolean (`bool`).
*/
template <> inline const bool& ContextValue::Get() const noexcept
{
if (m_contextValueType != ContextValueType::Bool)
{
abort();
}
return m_b;
}
/**
* @brief Get the context value type as integer (`int`).
*/
template <> inline const int& ContextValue::Get() const noexcept
{
if (m_contextValueType != ContextValueType::Int)
{
abort();
}
return m_i;
}
/**
* @brief Get the context value type as string (`std::string`).
*/
template <> inline const std::string& ContextValue::Get() const noexcept
{
if (m_contextValueType != ContextValueType::StdString)
{
abort();
}
return m_s;
}
/**
* @brief Get the context value type as a pointer (`std::unique_ptr<ValueBase>`).
*/
template <> inline const std::unique_ptr<ValueBase>& ContextValue::Get() const noexcept
{
if (m_contextValueType != ContextValueType::UniquePtr)
{
abort();
}
return m_p;
}
/**
* @brief A context is a node within a tree that represents expiration times and key/value pairs.
*/
@ -264,7 +50,8 @@ namespace Azure { namespace Core {
std::shared_ptr<ContextSharedState> Parent;
std::atomic_int64_t CancelAtMsecSinceEpoch;
std::string Key;
ContextValue Value;
std::shared_ptr<void> Value;
const std::type_info& ValueType;
static constexpr int64_t ToMsecSinceEpoch(time_point time)
{
@ -277,17 +64,28 @@ namespace Azure { namespace Core {
return time_point() + static_cast<std::chrono::milliseconds>(msec);
}
explicit ContextSharedState() : CancelAtMsecSinceEpoch(ToMsecSinceEpoch((time_point::max)()))
explicit ContextSharedState()
: CancelAtMsecSinceEpoch(ToMsecSinceEpoch((time_point::max)())), Value(nullptr),
ValueType(typeid(std::nullptr_t))
{
}
explicit ContextSharedState(
const std::shared_ptr<ContextSharedState>& parent,
time_point cancelAt)
: Parent(parent), CancelAtMsecSinceEpoch(ToMsecSinceEpoch(cancelAt)), Value(nullptr),
ValueType(typeid(std::nullptr_t))
{
}
template <class T>
explicit ContextSharedState(
const std::shared_ptr<ContextSharedState>& parent,
time_point cancelAt,
const std::string& key,
ContextValue&& value)
T value) // NOTE, should this be T&&
: Parent(parent), CancelAtMsecSinceEpoch(ToMsecSinceEpoch(cancelAt)), Key(key),
Value(std::move(value))
Value(std::make_shared<T>(std::move(value))), ValueType(typeid(T))
{
}
};
@ -321,8 +119,7 @@ namespace Azure { namespace Core {
*/
Context WithDeadline(time_point cancelWhen) const
{
return Context{std::make_shared<ContextSharedState>(
m_contextSharedState, cancelWhen, std::string(), ContextValue{})};
return Context{std::make_shared<ContextSharedState>(m_contextSharedState, cancelWhen)};
}
/**
@ -334,10 +131,10 @@ namespace Azure { namespace Core {
*
* @return A child context with no expiration and the \p key and \p value associated with it.
*/
Context WithValue(const std::string& key, ContextValue&& value) const
template <class T> Context WithValue(const std::string& key, T&& value) const
{
return Context{std::make_shared<ContextSharedState>(
m_contextSharedState, (time_point::max)(), key, std::move(value))};
m_contextSharedState, (time_point::max)(), key, std::forward<T>(value))};
}
/**
@ -349,7 +146,7 @@ namespace Azure { namespace Core {
* @return A value associated with the context found; an empty value if a specific value can't
* be found.
*/
const ContextValue& operator[](const std::string& key) const
template <class T> const T& Get(const std::string& key) const
{
if (!key.empty())
{
@ -357,13 +154,18 @@ namespace Azure { namespace Core {
{
if (ptr->Key == key)
{
return ptr->Value;
if (typeid(T) != ptr->ValueType)
{
// type mismatch
std::abort();
}
return *reinterpret_cast<const T*>(ptr->Value.get());
}
}
}
static ContextValue empty;
return empty;
std::abort();
// It should be expected that keys may not exist
// That implies we return T* and NOT a T&
}
/**
@ -415,10 +217,10 @@ namespace Azure { namespace Core {
throw OperationCancelledException("Request was cancelled by context.");
}
}
};
/**
* @brief Get the application context (root).
*/
Context& GetApplicationContext();
/**
* @brief Get the application context (root).
*/
static Context& GetApplicationContext();
};
}} // namespace Azure::Core

View File

@ -92,7 +92,7 @@ namespace Azure { namespace Core {
{
// In the cases where the customer doesn't want to use a context we new one up and pass it
// through
return PollInternal(GetApplicationContext());
return PollInternal(Context::GetApplicationContext());
}
/**
@ -115,7 +115,7 @@ namespace Azure { namespace Core {
{
// In the cases where the customer doesn't want to use a context we new one up and pass it
// through
return PollUntilDoneInternal(period, GetApplicationContext());
return PollUntilDoneInternal(period, Context::GetApplicationContext());
}
/**

View File

@ -6,7 +6,7 @@
using namespace Azure::Core;
using time_point = std::chrono::system_clock::time_point;
Context& Azure::Core::GetApplicationContext()
Context& Azure::Core::Context::GetApplicationContext()
{
static Context ctx;
return ctx;

View File

@ -157,7 +157,7 @@ Context inline CreateRetryContext(Context const& parent)
int retryCount = 0;
if (parent.HasKey(RetryKey))
{
retryCount = parent[RetryKey].Get<int>() + 1;
retryCount = parent.Get<int>(RetryKey) + 1;
}
return parent.WithValue(RetryKey, retryCount);
}
@ -174,7 +174,7 @@ int Azure::Core::Http::RetryPolicy::GetRetryNumber(Context const& context)
// ...
return -1;
}
return context[RetryKey].Get<int>();
return context.Get<int>(RetryKey);
}
std::unique_ptr<RawResponse> Azure::Core::Http::RetryPolicy::Send(

View File

@ -13,7 +13,7 @@ int main(int argc, char** argv)
// Create the test list
std::vector<Azure::Perf::TestMetadata> tests{Azure::Core::Test::NullableTest::GetTestMetadata()};
Azure::Perf::Program::Run(Azure::Core::GetApplicationContext(), tests, argc, argv);
Azure::Perf::Program::Run(Azure::Core::Context::GetApplicationContext(), tests, argc, argv);
return 0;
}

View File

@ -76,18 +76,19 @@ TEST(ClientOptions, copyWithOperator)
EXPECT_EQ(1, copyOptions.Retry.MaxRetries);
EXPECT_EQ(std::string("pleaseCopyMe"), copyOptions.Telemetry.ApplicationId);
Request r(HttpMethod::Get, Url(""));
auto result = copyOptions.Transport.Transport->Send(r, GetApplicationContext());
auto result = copyOptions.Transport.Transport->Send(r, Context::GetApplicationContext());
EXPECT_EQ(nullptr, result);
EXPECT_EQ(1, copyOptions.PerOperationPolicies.size());
result = copyOptions.PerOperationPolicies[0]->Send(
r, NextHttpPolicy(0, {}), GetApplicationContext());
r, NextHttpPolicy(0, {}), Context::GetApplicationContext());
EXPECT_EQ(3, result->GetMajorVersion());
EXPECT_EQ(3, result->GetMinorVersion());
EXPECT_EQ(std::string("IamAPerCallPolicy"), result->GetReasonPhrase());
EXPECT_EQ(1, copyOptions.PerRetryPolicies.size());
result = copyOptions.PerRetryPolicies[0]->Send(r, NextHttpPolicy(0, {}), GetApplicationContext());
result = copyOptions.PerRetryPolicies[0]->Send(
r, NextHttpPolicy(0, {}), Context::GetApplicationContext());
EXPECT_EQ(6, result->GetMajorVersion());
EXPECT_EQ(6, result->GetMinorVersion());
EXPECT_EQ(std::string("IamAPerRetryPolicy"), result->GetReasonPhrase());
@ -110,18 +111,19 @@ TEST(ClientOptions, copyWithConstructor)
EXPECT_EQ(1, copyOptions.Retry.MaxRetries);
EXPECT_EQ(std::string("pleaseCopyMe"), copyOptions.Telemetry.ApplicationId);
Request r(HttpMethod::Get, Url(""));
auto result = copyOptions.Transport.Transport->Send(r, GetApplicationContext());
auto result = copyOptions.Transport.Transport->Send(r, Context::GetApplicationContext());
EXPECT_EQ(nullptr, result);
EXPECT_EQ(1, copyOptions.PerOperationPolicies.size());
result = copyOptions.PerOperationPolicies[0]->Send(
r, NextHttpPolicy(0, {}), GetApplicationContext());
r, NextHttpPolicy(0, {}), Context::GetApplicationContext());
EXPECT_EQ(3, result->GetMajorVersion());
EXPECT_EQ(3, result->GetMinorVersion());
EXPECT_EQ(std::string("IamAPerCallPolicy"), result->GetReasonPhrase());
EXPECT_EQ(1, copyOptions.PerRetryPolicies.size());
result = copyOptions.PerRetryPolicies[0]->Send(r, NextHttpPolicy(0, {}), GetApplicationContext());
result = copyOptions.PerRetryPolicies[0]->Send(
r, NextHttpPolicy(0, {}), Context::GetApplicationContext());
EXPECT_EQ(6, result->GetMajorVersion());
EXPECT_EQ(6, result->GetMinorVersion());
EXPECT_EQ(std::string("IamAPerRetryPolicy"), result->GetReasonPhrase());
@ -151,18 +153,19 @@ TEST(ClientOptions, copyDerivedClassConstructor)
EXPECT_EQ(1, copyOptions.Retry.MaxRetries);
EXPECT_EQ(std::string("pleaseCopyMe"), copyOptions.Telemetry.ApplicationId);
Request r(HttpMethod::Get, Url(""));
auto result = copyOptions.Transport.Transport->Send(r, GetApplicationContext());
auto result = copyOptions.Transport.Transport->Send(r, Context::GetApplicationContext());
EXPECT_EQ(nullptr, result);
EXPECT_EQ(1, copyOptions.PerOperationPolicies.size());
result = copyOptions.PerOperationPolicies[0]->Send(
r, NextHttpPolicy(0, {}), GetApplicationContext());
r, NextHttpPolicy(0, {}), Context::GetApplicationContext());
EXPECT_EQ(3, result->GetMajorVersion());
EXPECT_EQ(3, result->GetMinorVersion());
EXPECT_EQ(std::string("IamAPerCallPolicy"), result->GetReasonPhrase());
EXPECT_EQ(1, copyOptions.PerRetryPolicies.size());
result = copyOptions.PerRetryPolicies[0]->Send(r, NextHttpPolicy(0, {}), GetApplicationContext());
result = copyOptions.PerRetryPolicies[0]->Send(
r, NextHttpPolicy(0, {}), Context::GetApplicationContext());
EXPECT_EQ(6, result->GetMajorVersion());
EXPECT_EQ(6, result->GetMinorVersion());
EXPECT_EQ(std::string("IamAPerRetryPolicy"), result->GetReasonPhrase());
@ -192,18 +195,19 @@ TEST(ClientOptions, copyDerivedClassOperator)
EXPECT_EQ(1, copyOptions.Retry.MaxRetries);
EXPECT_EQ(std::string("pleaseCopyMe"), copyOptions.Telemetry.ApplicationId);
Request r(HttpMethod::Get, Url(""));
auto result = copyOptions.Transport.Transport->Send(r, GetApplicationContext());
auto result = copyOptions.Transport.Transport->Send(r, Context::GetApplicationContext());
EXPECT_EQ(nullptr, result);
EXPECT_EQ(1, copyOptions.PerOperationPolicies.size());
result = copyOptions.PerOperationPolicies[0]->Send(
r, NextHttpPolicy(0, {}), GetApplicationContext());
r, NextHttpPolicy(0, {}), Context::GetApplicationContext());
EXPECT_EQ(3, result->GetMajorVersion());
EXPECT_EQ(3, result->GetMinorVersion());
EXPECT_EQ(std::string("IamAPerCallPolicy"), result->GetReasonPhrase());
EXPECT_EQ(1, copyOptions.PerRetryPolicies.size());
result = copyOptions.PerRetryPolicies[0]->Send(r, NextHttpPolicy(0, {}), GetApplicationContext());
result = copyOptions.PerRetryPolicies[0]->Send(
r, NextHttpPolicy(0, {}), Context::GetApplicationContext());
EXPECT_EQ(6, result->GetMajorVersion());
EXPECT_EQ(6, result->GetMinorVersion());
EXPECT_EQ(std::string("IamAPerRetryPolicy"), result->GetReasonPhrase());
@ -233,18 +237,19 @@ TEST(ClientOptions, moveConstruct)
EXPECT_EQ(1, copyOptions.Retry.MaxRetries);
EXPECT_EQ(std::string("pleaseCopyMe"), copyOptions.Telemetry.ApplicationId);
Request r(HttpMethod::Get, Url(""));
auto result = copyOptions.Transport.Transport->Send(r, GetApplicationContext());
auto result = copyOptions.Transport.Transport->Send(r, Context::GetApplicationContext());
EXPECT_EQ(nullptr, result);
EXPECT_EQ(1, copyOptions.PerOperationPolicies.size());
result = copyOptions.PerOperationPolicies[0]->Send(
r, NextHttpPolicy(0, {}), GetApplicationContext());
r, NextHttpPolicy(0, {}), Context::GetApplicationContext());
EXPECT_EQ(3, result->GetMajorVersion());
EXPECT_EQ(3, result->GetMinorVersion());
EXPECT_EQ(std::string("IamAPerCallPolicy"), result->GetReasonPhrase());
EXPECT_EQ(1, copyOptions.PerRetryPolicies.size());
result = copyOptions.PerRetryPolicies[0]->Send(r, NextHttpPolicy(0, {}), GetApplicationContext());
result = copyOptions.PerRetryPolicies[0]->Send(
r, NextHttpPolicy(0, {}), Context::GetApplicationContext());
EXPECT_EQ(6, result->GetMajorVersion());
EXPECT_EQ(6, result->GetMinorVersion());
EXPECT_EQ(std::string("IamAPerRetryPolicy"), result->GetReasonPhrase());

View File

@ -16,12 +16,8 @@ using namespace Azure::Core;
TEST(Context, Basic)
{
Context context;
auto& valueT = context["key"];
EXPECT_FALSE(context.HasKey(""));
EXPECT_FALSE(context.HasKey("key"));
auto kind = valueT.Alternative();
EXPECT_TRUE(kind == ContextValue::ContextValueType::Undefined);
}
TEST(Context, BasicBool)
@ -29,12 +25,8 @@ TEST(Context, BasicBool)
Context context;
// New context from previous
auto c2 = context.WithValue("key", true);
auto& valueT = c2["key"];
auto value = valueT.Get<bool>();
auto& value = c2.Get<bool>("key");
EXPECT_TRUE(value == true);
auto kind = valueT.Alternative();
EXPECT_TRUE(kind == ContextValue::ContextValueType::Bool);
}
TEST(Context, BasicInt)
@ -42,12 +34,8 @@ TEST(Context, BasicInt)
Context context;
// New context from previous
auto c2 = context.WithValue("key", 123);
auto& valueT = c2["key"];
auto value = valueT.Get<int>();
auto& value = c2.Get<int>("key");
EXPECT_TRUE(value == 123);
auto kind = valueT.Alternative();
EXPECT_TRUE(kind == ContextValue::ContextValueType::Int);
}
TEST(Context, BasicStdString)
@ -57,12 +45,8 @@ TEST(Context, BasicStdString)
Context context;
// New context from previous
auto c2 = context.WithValue("key", s);
auto& valueT = c2["key"];
auto value = valueT.Get<std::string>();
auto& value = c2.Get<std::string>("key");
EXPECT_TRUE(value == s);
auto kind = valueT.Alternative();
EXPECT_TRUE(kind == ContextValue::ContextValueType::StdString);
}
TEST(Context, BasicChar)
@ -73,12 +57,9 @@ TEST(Context, BasicChar)
Context context;
// New context from previous
auto c2 = context.WithValue("key", str);
auto& valueT = c2["key"];
auto value = valueT.Get<std::string>();
auto& value = c2.Get<const char*>("key");
EXPECT_TRUE(value == s);
auto kind = valueT.Alternative();
EXPECT_TRUE(kind == ContextValue::ContextValueType::StdString);
EXPECT_TRUE(value == str);
}
TEST(Context, IsCancelled)
@ -148,18 +129,6 @@ TEST(Context, ThrowIfCancelled)
EXPECT_THROW(c2.ThrowIfCancelled(), Azure::Core::OperationCancelledException);
}
TEST(Context, Alternative)
{
Context context;
// New context from previous
auto c2 = context.WithValue("key", 123);
auto& valueT1 = c2["key"];
auto& valueT2 = c2["otherKey"];
EXPECT_TRUE(valueT1.Alternative() == ContextValue::ContextValueType::Int);
EXPECT_TRUE(valueT2.Alternative() == ContextValue::ContextValueType::Undefined);
}
TEST(Context, Chain)
{
Context context;
@ -172,40 +141,21 @@ TEST(Context, Chain)
auto c7 = c6.WithValue("c7", "7");
auto finalContext = c7.WithValue("finalContext", "Final");
auto& valueT2 = finalContext["c2"];
auto& valueT3 = finalContext["c3"];
auto& valueT4 = finalContext["c4"];
auto& valueT5 = finalContext["c5"];
auto& valueT6 = finalContext["c6"];
auto& valueT7 = finalContext["c7"];
auto& valueT8 = finalContext["finalContext"];
auto& valueT9 = finalContext["otherKey"];
auto& valueT2 = finalContext.Get<int>("c2");
auto& valueT3 = finalContext.Get<int>("c3");
auto& valueT4 = finalContext.Get<int>("c4");
auto& valueT5 = finalContext.Get<const char*>("c5");
auto& valueT6 = finalContext.Get<const char*>("c6");
auto& valueT7 = finalContext.Get<const char*>("c7");
auto& valueT8 = finalContext.Get<const char*>("finalContext");
EXPECT_TRUE(valueT2.Alternative() == ContextValue::ContextValueType::Int);
EXPECT_TRUE(valueT3.Alternative() == ContextValue::ContextValueType::Int);
EXPECT_TRUE(valueT4.Alternative() == ContextValue::ContextValueType::Int);
EXPECT_TRUE(valueT5.Alternative() == ContextValue::ContextValueType::StdString);
EXPECT_TRUE(valueT6.Alternative() == ContextValue::ContextValueType::StdString);
EXPECT_TRUE(valueT7.Alternative() == ContextValue::ContextValueType::StdString);
EXPECT_TRUE(valueT8.Alternative() == ContextValue::ContextValueType::StdString);
EXPECT_TRUE(valueT9.Alternative() == ContextValue::ContextValueType::Undefined);
auto value = valueT2.Get<int>();
EXPECT_TRUE(value == 123);
value = valueT3.Get<int>();
EXPECT_TRUE(value == 456);
value = valueT4.Get<int>();
EXPECT_TRUE(value == 789);
auto str = valueT5.Get<std::string>();
EXPECT_TRUE(str == "5");
str = valueT6.Get<std::string>();
EXPECT_TRUE(str == "6");
str = valueT7.Get<std::string>();
EXPECT_TRUE(str == "7");
str = valueT8.Get<std::string>();
EXPECT_TRUE(str == "Final");
EXPECT_TRUE(valueT2 == 123);
EXPECT_TRUE(valueT3 == 456);
EXPECT_TRUE(valueT4 == 789);
EXPECT_TRUE(valueT5 == std::string("5"));
EXPECT_TRUE(valueT6 == std::string("6"));
EXPECT_TRUE(valueT7 == std::string("7"));
EXPECT_TRUE(valueT8 == std::string("Final"));
}
TEST(Context, MatchingKeys)
@ -215,32 +165,21 @@ TEST(Context, MatchingKeys)
auto c2 = context.WithValue("key", 123);
auto c3 = c2.WithValue("key", 456);
auto& valueT2 = c2["key"];
auto& valueT3 = c3["key"];
auto& missing = c3["otherKey"];
auto& valueT2 = c2.Get<int>("key");
auto& valueT3 = c3.Get<int>("key");
EXPECT_TRUE(valueT2.Alternative() == ContextValue::ContextValueType::Int);
EXPECT_TRUE(valueT3.Alternative() == ContextValue::ContextValueType::Int);
EXPECT_TRUE(missing.Alternative() == ContextValue::ContextValueType::Undefined);
auto value = valueT2.Get<int>();
EXPECT_TRUE(value == 123);
value = valueT3.Get<int>();
EXPECT_TRUE(value == 456);
EXPECT_TRUE(valueT2 == 123);
EXPECT_TRUE(valueT3 == 456);
}
struct SomeStructForContext : ValueBase
struct SomeStructForContext
{
bool someField = false;
int someField = 12345;
};
TEST(Context, UniquePtr)
{
auto contextP
= GetApplicationContext().WithValue("bool", std::make_unique<SomeStructForContext>());
auto& contextValueRef = contextP["bool"].Get<std::unique_ptr<ValueBase>>();
SomeStructForContext* pointerToStruct{static_cast<SomeStructForContext*>(contextValueRef.get())};
EXPECT_FALSE(pointerToStruct->someField);
pointerToStruct->someField = true;
EXPECT_TRUE(pointerToStruct->someField);
auto contextP = Context::GetApplicationContext().WithValue("struct", SomeStructForContext());
auto& contextValueRef = contextP.Get<SomeStructForContext>("struct");
EXPECT_EQ(contextValueRef.someField, 12345);
}

View File

@ -44,7 +44,8 @@ namespace Azure { namespace Core { namespace Test {
Azure::Core::Http::Request request(Azure::Core::Http::HttpMethod::Get, url);
std::unique_ptr<Azure::Core::Http::RawResponse> response;
EXPECT_NO_THROW(response = pipeline.Send(request, Azure::Core::GetApplicationContext()));
EXPECT_NO_THROW(
response = pipeline.Send(request, Azure::Core::Context::GetApplicationContext()));
auto responseCode = response->GetStatusCode();
int expectedCode = 200;
EXPECT_PRED2(
@ -73,7 +74,8 @@ namespace Azure { namespace Core { namespace Test {
Azure::Core::Http::Request request(Azure::Core::Http::HttpMethod::Get, url);
std::unique_ptr<Azure::Core::Http::RawResponse> response;
EXPECT_NO_THROW(response = pipeline.Send(request, Azure::Core::GetApplicationContext()));
EXPECT_NO_THROW(
response = pipeline.Send(request, Azure::Core::Context::GetApplicationContext()));
auto responseCode = response->GetStatusCode();
int expectedCode = 200;
EXPECT_PRED2(
@ -105,10 +107,8 @@ namespace Azure { namespace Core { namespace Test {
Azure::Core::Http::Request request(Azure::Core::Http::HttpMethod::Get, url);
std::unique_ptr<Azure::Core::Http::RawResponse> response;
EXPECT_NO_THROW(response = pipeline.Send(Azure::Core::GetApplicationContext(), request));
auto responseCode = response->GetStatusCode();
int expectedCode = 200;
EXPECT_PRED2(
EXPECT_NO_THROW(response = pipeline.Send(Azure::Core::Context::GetApplicationContext(),
request)); auto responseCode = response->GetStatusCode(); int expectedCode = 200; EXPECT_PRED2(
[](int expected, int code) { return expected == code; },
expectedCode,
static_cast<typename std::underlying_type<Azure::Core::Http::HttpStatusCode>::type>(
@ -132,10 +132,8 @@ namespace Azure { namespace Core { namespace Test {
Azure::Core::Http::Request request(Azure::Core::Http::HttpMethod::Get, url);
std::unique_ptr<Azure::Core::Http::RawResponse> response;
EXPECT_NO_THROW(response = pipeline.Send(Azure::Core::GetApplicationContext(), request));
auto responseCode = response->GetStatusCode();
int expectedCode = 200;
EXPECT_PRED2(
EXPECT_NO_THROW(response = pipeline.Send(Azure::Core::Context::GetApplicationContext(),
request)); auto responseCode = response->GetStatusCode(); int expectedCode = 200; EXPECT_PRED2(
[](int expected, int code) { return expected == code; },
expectedCode,
static_cast<typename std::underlying_type<Azure::Core::Http::HttpStatusCode>::type>(
@ -159,10 +157,8 @@ namespace Azure { namespace Core { namespace Test {
Azure::Core::Http::Request request(Azure::Core::Http::HttpMethod::Get, url);
std::unique_ptr<Azure::Core::Http::RawResponse> response;
EXPECT_NO_THROW(response = pipeline.Send(Azure::Core::GetApplicationContext(), request));
auto responseCode = response->GetStatusCode();
int expectedCode = 200;
EXPECT_PRED2(
EXPECT_NO_THROW(response = pipeline.Send(Azure::Core::Context::GetApplicationContext(),
request)); auto responseCode = response->GetStatusCode(); int expectedCode = 200; EXPECT_PRED2(
[](int expected, int code) { return expected == code; },
expectedCode,
static_cast<typename std::underlying_type<Azure::Core::Http::HttpStatusCode>::type>(
@ -192,7 +188,8 @@ namespace Azure { namespace Core { namespace Test {
Azure::Core::Http::Request request(Azure::Core::Http::HttpMethod::Get, url);
std::unique_ptr<Azure::Core::Http::RawResponse> response;
EXPECT_NO_THROW(response = pipeline.Send(request, Azure::Core::GetApplicationContext()));
EXPECT_NO_THROW(
response = pipeline.Send(request, Azure::Core::Context::GetApplicationContext()));
auto responseCode = response->GetStatusCode();
int expectedCode = 200;
EXPECT_PRED2(
@ -222,7 +219,8 @@ namespace Azure { namespace Core { namespace Test {
Azure::Core::Http::Request request(Azure::Core::Http::HttpMethod::Get, url);
std::unique_ptr<Azure::Core::Http::RawResponse> response;
EXPECT_NO_THROW(response = pipeline.Send(request, Azure::Core::GetApplicationContext()));
EXPECT_NO_THROW(
response = pipeline.Send(request, Azure::Core::Context::GetApplicationContext()));
auto responseCode = response->GetStatusCode();
int expectedCode = 200;
EXPECT_PRED2(
@ -257,7 +255,8 @@ namespace Azure { namespace Core { namespace Test {
Azure::Core::Http::Request request(Azure::Core::Http::HttpMethod::Get, url);
std::unique_ptr<Azure::Core::Http::RawResponse> response;
EXPECT_NO_THROW(response = pipeline.Send(request, Azure::Core::GetApplicationContext()));
EXPECT_NO_THROW(
response = pipeline.Send(request, Azure::Core::Context::GetApplicationContext()));
auto responseCode = response->GetStatusCode();
int expectedCode = 200;
EXPECT_PRED2(

View File

@ -43,7 +43,7 @@ namespace Azure { namespace Core { namespace Test {
auto session = std::make_unique<Azure::Core::Http::CurlSession>(
request, std::move(uniqueCurlMock), true);
EXPECT_NO_THROW(session->Perform(Azure::Core::GetApplicationContext()));
EXPECT_NO_THROW(session->Perform(Azure::Core::Context::GetApplicationContext()));
}
TEST_F(CurlSession, chunkResponseSizeZero)
@ -76,7 +76,7 @@ namespace Azure { namespace Core { namespace Test {
auto session = std::make_unique<Azure::Core::Http::CurlSession>(
request, std::move(uniqueCurlMock), true);
EXPECT_NO_THROW(session->Perform(Azure::Core::GetApplicationContext()));
EXPECT_NO_THROW(session->Perform(Azure::Core::Context::GetApplicationContext()));
}
// Clear the connections from the pool to invoke clean routine
Azure::Core::Http::CurlConnectionPool::ConnectionPoolIndex.clear();
@ -116,14 +116,15 @@ namespace Azure { namespace Core { namespace Test {
auto session = std::make_unique<Azure::Core::Http::CurlSession>(
request, std::move(uniqueCurlMock), true);
EXPECT_NO_THROW(session->Perform(Azure::Core::GetApplicationContext()));
EXPECT_NO_THROW(session->Perform(Azure::Core::Context::GetApplicationContext()));
auto r = session->GetResponse();
r->SetBodyStream(std::move(session));
auto bodyS = r->GetBodyStream();
// Read the bodyStream to get all chunks
EXPECT_THROW(
Azure::Core::IO::BodyStream::ReadToEnd(*bodyS, Azure::Core::GetApplicationContext()),
Azure::Core::IO::BodyStream::ReadToEnd(
*bodyS, Azure::Core::Context::GetApplicationContext()),
Azure::Core::Http::TransportException);
}
// Clear the connections from the pool to invoke clean routine
@ -193,14 +194,14 @@ namespace Azure { namespace Core { namespace Test {
auto session = std::make_unique<Azure::Core::Http::CurlSession>(
request, std::move(uniqueCurlMock), true);
EXPECT_NO_THROW(session->Perform(Azure::Core::GetApplicationContext()));
EXPECT_NO_THROW(session->Perform(Azure::Core::Context::GetApplicationContext()));
auto response = session->GetResponse();
response->SetBodyStream(std::move(session));
auto bodyS = response->GetBodyStream();
// Read the bodyStream to get all chunks
EXPECT_NO_THROW(
Azure::Core::IO::BodyStream::ReadToEnd(*bodyS, Azure::Core::GetApplicationContext()));
EXPECT_NO_THROW(Azure::Core::IO::BodyStream::ReadToEnd(
*bodyS, Azure::Core::Context::GetApplicationContext()));
}
// Clear the connections from the pool to invoke clean routine
Azure::Core::Http::CurlConnectionPool::ConnectionPoolIndex.clear();
@ -228,7 +229,7 @@ namespace Azure { namespace Core { namespace Test {
auto session = std::make_unique<Azure::Core::Http::CurlSession>(
request, std::move(uniqueCurlMock), true);
auto returnCode = session->Perform(Azure::Core::GetApplicationContext());
auto returnCode = session->Perform(Azure::Core::Context::GetApplicationContext());
EXPECT_EQ(CURLE_SEND_ERROR, returnCode);
}
// Check connection pool is empty (connection was not moved to the pool)

View File

@ -22,7 +22,7 @@ using namespace Azure::Core;
TEST(Context, ApplicationContext)
{
Context appContext = GetApplicationContext();
Context appContext = Context::GetApplicationContext();
EXPECT_FALSE(appContext.HasKey("Key"));
EXPECT_FALSE(appContext.HasKey("key"));
@ -41,6 +41,6 @@ TEST(Context, ApplicationContext)
// AppContext2 is the same context as AppContext
// The context should be cancelled
Context appContext2 = GetApplicationContext();
Context appContext2 = Context::GetApplicationContext();
EXPECT_TRUE(appContext2.IsCancelled());
}

View File

@ -192,7 +192,8 @@ namespace Azure { namespace Core { namespace Test {
// Change the offset of the stream to be non-zero by reading a byte.
std::vector<uint8_t> temp(2);
EXPECT_EQ(
Azure::Core::IO::BodyStream::ReadToCount(stream, temp.data(), 1, GetApplicationContext()),
Azure::Core::IO::BodyStream::ReadToCount(
stream, temp.data(), 1, Context::GetApplicationContext()),
1);
EXPECT_EQ(temp[0], 1);
@ -213,7 +214,7 @@ namespace Azure { namespace Core { namespace Test {
auto getStream = req.GetBodyStream();
EXPECT_EQ(
Azure::Core::IO::BodyStream::ReadToCount(
*getStream, temp.data(), 2, GetApplicationContext()),
*getStream, temp.data(), 2, Context::GetApplicationContext()),
2);
EXPECT_EQ(temp[0], 1);

View File

@ -89,7 +89,8 @@ TEST(Policy, throwWhenNoTransportPolicy)
Azure::Core::Http::_internal::HttpPipeline pipeline(policies);
Azure::Core::Http::Url url("");
Azure::Core::Http::Request request(Azure::Core::Http::HttpMethod::Get, url);
EXPECT_THROW(pipeline.Send(request, Azure::Core::GetApplicationContext()), std::invalid_argument);
EXPECT_THROW(
pipeline.Send(request, Azure::Core::Context::GetApplicationContext()), std::invalid_argument);
}
TEST(Policy, throwWhenNoTransportPolicyMessage)
@ -107,7 +108,7 @@ TEST(Policy, throwWhenNoTransportPolicyMessage)
try
{
pipeline.Send(request, Azure::Core::GetApplicationContext());
pipeline.Send(request, Azure::Core::Context::GetApplicationContext());
}
catch (const std::invalid_argument& ex)
{
@ -132,7 +133,7 @@ TEST(Policy, ValuePolicy)
Request request(HttpMethod::Get, Url("https:://www.example.com"));
pipeline.Send(request, GetApplicationContext());
pipeline.Send(request, Context::GetApplicationContext());
auto headers = request.GetHeaders();
auto queryParams = request.GetUrl().GetQueryParameters();
@ -150,7 +151,7 @@ TEST(Policy, RetryPolicyCounter)
retryCounterState = 0;
// Check when there's no info about retry on the context
auto initialContext = GetApplicationContext();
auto initialContext = Context::GetApplicationContext();
EXPECT_EQ(-1, RetryPolicy::GetRetryNumber(initialContext));
// Pipeline with retry test
@ -185,5 +186,5 @@ TEST(Policy, RetryPolicyRetryCycle)
HttpPipeline pipeline(policies);
Request request(HttpMethod::Get, Url("url"));
pipeline.Send(request, GetApplicationContext());
pipeline.Send(request, Context::GetApplicationContext());
}

View File

@ -33,7 +33,7 @@ namespace Azure { namespace Core { namespace Test {
Azure::Core::Http::Url host("http://httpbin.org/get");
auto request = Azure::Core::Http::Request(Azure::Core::Http::HttpMethod::Get, host);
auto response = m_pipeline->Send(request, Azure::Core::GetApplicationContext());
auto response = m_pipeline->Send(request, Azure::Core::Context::GetApplicationContext());
checkResponseCode(response->GetStatusCode());
auto expectedResponseBodySize = std::stoull(response->GetHeaders().at("content-length"));
CheckBodyFromBuffer(*response, expectedResponseBodySize);
@ -42,7 +42,7 @@ namespace Azure { namespace Core { namespace Test {
request = Azure::Core::Http::Request(Azure::Core::Http::HttpMethod::Get, host);
// Add a header and send again. RawResponse should return that header in the body
request.SetHeader("123", "456");
response = m_pipeline->Send(request, Azure::Core::GetApplicationContext());
response = m_pipeline->Send(request, Azure::Core::Context::GetApplicationContext());
checkResponseCode(response->GetStatusCode());
// header length is 6 (data) + 13 (formating) -> ` "123": "456"\r\n,`
CheckBodyFromBuffer(*response, expectedResponseBodySize + 6 + 13);
@ -53,7 +53,7 @@ namespace Azure { namespace Core { namespace Test {
Azure::Core::Http::Url host("http://mt3.google.com/generate_204");
auto request = Azure::Core::Http::Request(Azure::Core::Http::HttpMethod::Get, host);
auto response = m_pipeline->Send(request, Azure::Core::GetApplicationContext());
auto response = m_pipeline->Send(request, Azure::Core::Context::GetApplicationContext());
checkResponseCode(response->GetStatusCode(), Azure::Core::Http::HttpStatusCode::NoContent);
auto expectedResponseBodySize = std::stoull(response->GetHeaders().at("content-length"));
CheckBodyFromBuffer(*response, expectedResponseBodySize);
@ -68,7 +68,7 @@ namespace Azure { namespace Core { namespace Test {
// loop sending request
for (auto i = 0; i < 50; i++)
{
auto response = m_pipeline->Send(request, Azure::Core::GetApplicationContext());
auto response = m_pipeline->Send(request, Azure::Core::Context::GetApplicationContext());
auto expectedResponseBodySize = std::stoull(response->GetHeaders().at("content-length"));
checkResponseCode(response->GetStatusCode());
CheckBodyFromBuffer(*response, expectedResponseBodySize);
@ -81,7 +81,7 @@ namespace Azure { namespace Core { namespace Test {
auto expectedResponseBodySize = 0;
auto request = Azure::Core::Http::Request(Azure::Core::Http::HttpMethod::Head, host);
auto response = m_pipeline->Send(request, Azure::Core::GetApplicationContext());
auto response = m_pipeline->Send(request, Azure::Core::Context::GetApplicationContext());
checkResponseCode(response->GetStatusCode());
CheckBodyFromBuffer(*response, expectedResponseBodySize);
@ -99,7 +99,7 @@ namespace Azure { namespace Core { namespace Test {
auto bodyRequest = Azure::Core::IO::MemoryBodyStream(requestBodyVector);
auto request
= Azure::Core::Http::Request(Azure::Core::Http::HttpMethod::Put, host, &bodyRequest);
auto response = m_pipeline->Send(request, Azure::Core::GetApplicationContext());
auto response = m_pipeline->Send(request, Azure::Core::Context::GetApplicationContext());
checkResponseCode(response->GetStatusCode());
auto expectedResponseBodySize = std::stoull(response->GetHeaders().at("content-length"));
@ -115,7 +115,7 @@ namespace Azure { namespace Core { namespace Test {
auto bodyRequest = Azure::Core::IO::MemoryBodyStream(requestBodyVector);
auto request
= Azure::Core::Http::Request(Azure::Core::Http::HttpMethod::Delete, host, &bodyRequest);
auto response = m_pipeline->Send(request, Azure::Core::GetApplicationContext());
auto response = m_pipeline->Send(request, Azure::Core::Context::GetApplicationContext());
checkResponseCode(response->GetStatusCode());
auto expectedResponseBodySize = std::stoull(response->GetHeaders().at("content-length"));
@ -131,7 +131,7 @@ namespace Azure { namespace Core { namespace Test {
auto bodyRequest = Azure::Core::IO::MemoryBodyStream(requestBodyVector);
auto request
= Azure::Core::Http::Request(Azure::Core::Http::HttpMethod::Patch, host, &bodyRequest);
auto response = m_pipeline->Send(request, Azure::Core::GetApplicationContext());
auto response = m_pipeline->Send(request, Azure::Core::Context::GetApplicationContext());
checkResponseCode(response->GetStatusCode());
auto expectedResponseBodySize = std::stoull(response->GetHeaders().at("content-length"));
@ -150,7 +150,7 @@ namespace Azure { namespace Core { namespace Test {
"sent to a client.</h5></body></html>");
auto request = Azure::Core::Http::Request(Azure::Core::Http::HttpMethod::Get, host);
auto response = m_pipeline->Send(request, Azure::Core::GetApplicationContext());
auto response = m_pipeline->Send(request, Azure::Core::Context::GetApplicationContext());
checkResponseCode(response->GetStatusCode());
CheckBodyFromBuffer(*response, expectedResponseBodySize, expectedChunkResponse);
@ -169,7 +169,7 @@ namespace Azure { namespace Core { namespace Test {
auto bodyRequest = Azure::Core::IO::MemoryBodyStream(requestBodyVector);
auto request
= Azure::Core::Http::Request(Azure::Core::Http::HttpMethod::Put, host, &bodyRequest);
auto response = m_pipeline->Send(request, Azure::Core::GetApplicationContext());
auto response = m_pipeline->Send(request, Azure::Core::Context::GetApplicationContext());
}
}
@ -182,7 +182,7 @@ namespace Azure { namespace Core { namespace Test {
Azure::Core::Http::Url host("http://httpbin.org/get");
auto request = Azure::Core::Http::Request(Azure::Core::Http::HttpMethod::Get, host, true);
auto response = m_pipeline->Send(request, Azure::Core::GetApplicationContext());
auto response = m_pipeline->Send(request, Azure::Core::Context::GetApplicationContext());
checkResponseCode(response->GetStatusCode());
auto expectedResponseBodySize = std::stoull(response->GetHeaders().at("content-length"));
CheckBodyFromStream(*response, expectedResponseBodySize);
@ -190,7 +190,7 @@ namespace Azure { namespace Core { namespace Test {
request = Azure::Core::Http::Request(Azure::Core::Http::HttpMethod::Get, host, true);
// Add a header and send again. Response should return that header in the body
request.SetHeader("123", "456");
response = m_pipeline->Send(request, Azure::Core::GetApplicationContext());
response = m_pipeline->Send(request, Azure::Core::Context::GetApplicationContext());
checkResponseCode(response->GetStatusCode());
// header length is 6 (data) + 13 (formating) -> ` "123": "456"\r\n,`
CheckBodyFromStream(*response, expectedResponseBodySize + 6 + 13);
@ -205,7 +205,7 @@ namespace Azure { namespace Core { namespace Test {
// loop sending request
for (auto i = 0; i < 50; i++)
{
auto response = m_pipeline->Send(request, Azure::Core::GetApplicationContext());
auto response = m_pipeline->Send(request, Azure::Core::Context::GetApplicationContext());
auto expectedResponseBodySize = std::stoull(response->GetHeaders().at("content-length"));
checkResponseCode(response->GetStatusCode());
CheckBodyFromStream(*response, expectedResponseBodySize);
@ -218,7 +218,7 @@ namespace Azure { namespace Core { namespace Test {
auto expectedResponseBodySize = 0;
auto request = Azure::Core::Http::Request(Azure::Core::Http::HttpMethod::Head, host, true);
auto response = m_pipeline->Send(request, Azure::Core::GetApplicationContext());
auto response = m_pipeline->Send(request, Azure::Core::Context::GetApplicationContext());
checkResponseCode(response->GetStatusCode());
CheckBodyFromStream(*response, expectedResponseBodySize);
@ -236,7 +236,7 @@ namespace Azure { namespace Core { namespace Test {
auto bodyRequest = Azure::Core::IO::MemoryBodyStream(requestBodyVector);
auto request
= Azure::Core::Http::Request(Azure::Core::Http::HttpMethod::Put, host, &bodyRequest, true);
auto response = m_pipeline->Send(request, Azure::Core::GetApplicationContext());
auto response = m_pipeline->Send(request, Azure::Core::Context::GetApplicationContext());
checkResponseCode(response->GetStatusCode());
auto expectedResponseBodySize = std::stoull(response->GetHeaders().at("content-length"));
@ -252,7 +252,7 @@ namespace Azure { namespace Core { namespace Test {
auto bodyRequest = Azure::Core::IO::MemoryBodyStream(requestBodyVector);
auto request = Azure::Core::Http::Request(
Azure::Core::Http::HttpMethod::Delete, host, &bodyRequest, true);
auto response = m_pipeline->Send(request, Azure::Core::GetApplicationContext());
auto response = m_pipeline->Send(request, Azure::Core::Context::GetApplicationContext());
checkResponseCode(response->GetStatusCode());
auto expectedResponseBodySize = std::stoull(response->GetHeaders().at("content-length"));
@ -268,7 +268,7 @@ namespace Azure { namespace Core { namespace Test {
auto bodyRequest = Azure::Core::IO::MemoryBodyStream(requestBodyVector);
auto request = Azure::Core::Http::Request(
Azure::Core::Http::HttpMethod::Patch, host, &bodyRequest, true);
auto response = m_pipeline->Send(request, Azure::Core::GetApplicationContext());
auto response = m_pipeline->Send(request, Azure::Core::Context::GetApplicationContext());
checkResponseCode(response->GetStatusCode());
auto expectedResponseBodySize = std::stoull(response->GetHeaders().at("content-length"));
@ -287,7 +287,7 @@ namespace Azure { namespace Core { namespace Test {
"sent to a client.</h5></body></html>");
auto request = Azure::Core::Http::Request(Azure::Core::Http::HttpMethod::Get, host, true);
auto response = m_pipeline->Send(request, Azure::Core::GetApplicationContext());
auto response = m_pipeline->Send(request, Azure::Core::Context::GetApplicationContext());
checkResponseCode(response->GetStatusCode());
CheckBodyFromStream(*response, expectedResponseBodySize, expectedChunkResponse);
@ -299,7 +299,7 @@ namespace Azure { namespace Core { namespace Test {
std::string expectedType("This is the Response Type");
auto request = Azure::Core::Http::Request(Azure::Core::Http::HttpMethod::Get, host, false);
auto response = m_pipeline->Send(request, Azure::Core::GetApplicationContext());
auto response = m_pipeline->Send(request, Azure::Core::Context::GetApplicationContext());
Azure::Response<std::string> responseT(expectedType, std::move(response));
auto& r = responseT.GetRawResponse();
@ -331,7 +331,7 @@ namespace Azure { namespace Core { namespace Test {
// Make transport adapter to read all stream content for uploading instead of chunks
request.SetUploadChunkSize(1024 * 1024);
{
auto response = m_pipeline->Send(request, Azure::Core::GetApplicationContext());
auto response = m_pipeline->Send(request, Azure::Core::Context::GetApplicationContext());
checkResponseCode(response->GetStatusCode());
auto expectedResponseBodySize = std::stoull(response->GetHeaders().at("content-length"));
CheckBodyFromBuffer(*response, expectedResponseBodySize);
@ -348,7 +348,7 @@ namespace Azure { namespace Core { namespace Test {
auto bodyRequest = Azure::Core::IO::MemoryBodyStream(requestBodyVector);
auto request
= Azure::Core::Http::Request(Azure::Core::Http::HttpMethod::Put, host, &bodyRequest, true);
auto response = m_pipeline->Send(request, Azure::Core::GetApplicationContext());
auto response = m_pipeline->Send(request, Azure::Core::Context::GetApplicationContext());
checkResponseCode(
response->GetStatusCode(), Azure::Core::Http::HttpStatusCode::MethodNotAllowed);
auto expectedResponseBodySize = std::stoull(response->GetHeaders().at("content-length"));
@ -410,7 +410,7 @@ namespace Azure { namespace Core { namespace Test {
auto request = Azure::Core::Http::Request(Azure::Core::Http::HttpMethod::Get, host);
EXPECT_THROW(
m_pipeline->Send(request, Azure::Core::GetApplicationContext()),
m_pipeline->Send(request, Azure::Core::Context::GetApplicationContext()),
Azure::Core::RequestFailedException);
}
@ -422,7 +422,7 @@ namespace Azure { namespace Core { namespace Test {
// test dynamic cast
try
{
auto result = m_pipeline->Send(request, Azure::Core::GetApplicationContext());
auto result = m_pipeline->Send(request, Azure::Core::Context::GetApplicationContext());
}
catch (Azure::Core::RequestFailedException& err)
{
@ -463,7 +463,7 @@ namespace Azure { namespace Core { namespace Test {
// Make transport adapter to read all stream content for uploading instead of chunks
request.SetUploadChunkSize(Azure::Core::Test::Datails::FileSize);
{
auto response = m_pipeline->Send(request, Azure::Core::GetApplicationContext());
auto response = m_pipeline->Send(request, Azure::Core::Context::GetApplicationContext());
checkResponseCode(response->GetStatusCode());
auto expectedResponseBodySize = std::stoull(response->GetHeaders().at("content-length"));
@ -501,7 +501,7 @@ namespace Azure { namespace Core { namespace Test {
Azure::Core::Http::HttpMethod::Put, host, &requestBodyStream, true);
// Make transport adapter to read default chunk size
{
auto response = m_pipeline->Send(request, Azure::Core::GetApplicationContext());
auto response = m_pipeline->Send(request, Azure::Core::Context::GetApplicationContext());
checkResponseCode(response->GetStatusCode());
auto expectedResponseBodySize = std::stoull(response->GetHeaders().at("content-length"));
@ -540,7 +540,7 @@ namespace Azure { namespace Core { namespace Test {
// Make transport adapter to read more than file size (5Mb)
request.SetUploadChunkSize(Azure::Core::Test::Datails::FileSize * 5);
{
auto response = m_pipeline->Send(request, Azure::Core::GetApplicationContext());
auto response = m_pipeline->Send(request, Azure::Core::Context::GetApplicationContext());
checkResponseCode(response->GetStatusCode());
auto expectedResponseBodySize = std::stoull(response->GetHeaders().at("content-length"));
@ -591,8 +591,8 @@ namespace Azure { namespace Core { namespace Test {
auto body = response.GetBodyStream();
EXPECT_NE(body, nullptr);
std::vector<uint8_t> bodyVector
= Azure::Core::IO::BodyStream::ReadToEnd(*body, Azure::Core::GetApplicationContext());
std::vector<uint8_t> bodyVector = Azure::Core::IO::BodyStream::ReadToEnd(
*body, Azure::Core::Context::GetApplicationContext());
int64_t bodySize = body->Length();
EXPECT_EQ(bodySize, size);
bodySize = bodyVector.size();

View File

@ -152,7 +152,7 @@ int main(int argc, char** argv)
}}};
// Call the `Run` method with a context, the tests and the application arguments to launch the program.
Azure::Perf::Program::Run(Azure::Core::GetApplicationContext(), tests, argc, argv);
Azure::Perf::Program::Run(Azure::Core::Context::GetApplicationContext(), tests, argc, argv);
return 0;
}

View File

@ -34,7 +34,7 @@ int main(int argc, char** argv)
tests.emplace_back(Azure::Perf::Test::WinHttpClientGetTest::GetTestMetadata());
#endif
Azure::Perf::Program::Run(Azure::Core::GetApplicationContext(), tests, argc, argv);
Azure::Perf::Program::Run(Azure::Core::Context::GetApplicationContext(), tests, argc, argv);
return 0;
}

View File

@ -12,7 +12,7 @@ int main(int argc, char** argv)
std::vector<Azure::Perf::TestMetadata> tests{
Azure::Identity::Test::SecretCredentialTest::GetTestMetadata()};
Azure::Perf::Program::Run(Azure::Core::GetApplicationContext(), tests, argc, argv);
Azure::Perf::Program::Run(Azure::Core::Context::GetApplicationContext(), tests, argc, argv);
return 0;
}

View File

@ -12,7 +12,7 @@ int main(int argc, char** argv)
std::vector<Azure::Perf::TestMetadata> tests{
Azure::Security::KeyVault::Keys::Test::GetKey::GetTestMetadata()};
Azure::Perf::Program::Run(Azure::Core::GetApplicationContext(), tests, argc, argv);
Azure::Perf::Program::Run(Azure::Core::Context::GetApplicationContext(), tests, argc, argv);
return 0;
}

View File

@ -59,7 +59,7 @@ TEST_F(KeyVaultClientTest, DeleteKey)
// Setting 3 min as timeout just because I like number 3. We just want to prevent test running
// for so long if something happens and no exception is thrown (paranoid scenario)
auto duration = std::chrono::system_clock::now() + std::chrono::minutes(3);
auto cancelToken = Azure::Core::GetApplicationContext().WithDeadline(duration);
auto cancelToken = Azure::Core::Context::GetApplicationContext().WithDeadline(duration);
auto keyResponseLRO = keyClient.StartDeleteKey(keyName);
auto expectedStatusToken = m_keyVaultUrl + "/"
@ -133,7 +133,7 @@ TEST_F(KeyVaultClientTest, DoubleDelete)
}
{
auto duration = std::chrono::system_clock::now() + std::chrono::minutes(3);
auto cancelToken = Azure::Core::GetApplicationContext().WithDeadline(duration);
auto cancelToken = Azure::Core::Context::GetApplicationContext().WithDeadline(duration);
auto keyResponseLRO = keyClient.StartDeleteKey(keyName);
auto keyResponse = keyResponseLRO.PollUntilDone(std::chrono::milliseconds(1000), cancelToken);
}
@ -207,7 +207,7 @@ TEST_F(KeyVaultClientTest, CreateDeletedKey)
}
{
auto duration = std::chrono::system_clock::now() + std::chrono::minutes(3);
auto cancelToken = Azure::Core::GetApplicationContext().WithDeadline(duration);
auto cancelToken = Azure::Core::Context::GetApplicationContext().WithDeadline(duration);
auto keyResponseLRO = keyClient.StartDeleteKey(keyName);
auto keyResponse = keyResponseLRO.PollUntilDone(std::chrono::milliseconds(1000), cancelToken);
}

View File

@ -12,7 +12,7 @@ int main(int argc, char** argv)
std::vector<Azure::Perf::TestMetadata> tests{
Azure::Storage::Blobs::Test::DownloadBlob::GetTestMetadata()};
Azure::Perf::Program::Run(Azure::Core::GetApplicationContext(), tests, argc, argv);
Azure::Perf::Program::Run(Azure::Core::Context::GetApplicationContext(), tests, argc, argv);
return 0;
}

View File

@ -13,7 +13,7 @@ namespace Azure { namespace Storage { namespace _detail {
static constexpr const char* SecondaryHostReplicaStatusKey
= "AzureSdkStorageSecondaryHostReplicaStatusKey";
struct SecondaryHostReplicaStatus : public Azure::Core::ValueBase
struct SecondaryHostReplicaStatus
{
bool replicated = true;
};

View File

@ -13,8 +13,7 @@ namespace Azure { namespace Storage { namespace _detail {
SecondaryHostReplicaStatus* replicaStatus = nullptr;
if (ctx.HasKey(SecondaryHostReplicaStatusKey))
{
replicaStatus = dynamic_cast<SecondaryHostReplicaStatus*>(
ctx[SecondaryHostReplicaStatusKey].Get<std::unique_ptr<Azure::Core::ValueBase>>().get());
replicaStatus = ctx.Get<SecondaryHostReplicaStatus*>(SecondaryHostReplicaStatusKey);
}
bool considerSecondary = (request.GetMethod() == Azure::Core::Http::HttpMethod::Get