Move EventsUploadContextPtr to a std::shared_ptr<T>

This commit is contained in:
Matt Koscumb 2020-07-08 13:00:16 -07:00
Родитель 99c9a846e7
Коммит b37ff55fbb
11 изменённых файлов: 49 добавлений и 50 удалений

Просмотреть файл

@ -63,7 +63,7 @@ namespace ARIASDK_NS_BEGIN {
virtual ~HttpCallback()
{
LOG_TRACE("destroy HTTP callback=%p ctx=%p", this, m_ctx);
LOG_TRACE("destroy HTTP callback=%p ctx=%p", this, m_ctx.get());
}
public:

Просмотреть файл

@ -122,7 +122,7 @@ namespace ARIASDK_NS_BEGIN {
}
};
typedef EventsUploadContext* EventsUploadContextPtr;
using EventsUploadContextPtr = std::shared_ptr<EventsUploadContext>;
//---

Просмотреть файл

@ -182,7 +182,7 @@ namespace ARIASDK_NS_BEGIN {
}
#endif
EventsUploadContextPtr ctx = new EventsUploadContext();
auto ctx = std::make_shared<EventsUploadContext>();
ctx->requestedMinLatency = m_runningLatency;
addUpload(ctx);
initiateUpload(ctx);
@ -190,10 +190,10 @@ namespace ARIASDK_NS_BEGIN {
void TransmissionPolicyManager::finishUpload(EventsUploadContextPtr ctx, int nextUploadInMs)
{
LOG_TRACE("HTTP upload finished for ctx=%p", ctx);
LOG_TRACE("HTTP upload finished for ctx=%p", ctx.get());
if (!removeUpload(ctx))
{
LOG_WARN("HTTP NOT removing non-existing ctx from active uploads ctx=%p", ctx);
LOG_WARN("HTTP NOT removing non-existing ctx from active uploads ctx=%p", ctx.get());
}
// Rescheduling upload
@ -270,7 +270,7 @@ namespace ARIASDK_NS_BEGIN {
/* This logic needs to be revised: one event in a dedicated HTTP post is wasteful! */
// Initiate upload right away
if (event->record.latency > EventLatency_RealTime) {
EventsUploadContextPtr ctx = new EventsUploadContext();
auto ctx = std::make_shared<EventsUploadContext>();
ctx->requestedMinLatency = event->record.latency;
addUpload(ctx);
initiateUpload(ctx);

Просмотреть файл

@ -106,9 +106,8 @@ namespace ARIASDK_NS_BEGIN {
auto it = m_activeUploads.find(ctx);
if (it != m_activeUploads.cend())
{
LOG_TRACE("HTTP removing from active uploads ctx=%p", ctx);
LOG_TRACE("HTTP removing from active uploads ctx=%p", ctx.get());
m_activeUploads.erase(it);
delete ctx;
return true;
}
return false;

Просмотреть файл

@ -47,7 +47,7 @@ TEST_F(HttpClientManagerTests, HandlesRequestFlow)
{
SimpleHttpRequest* req = new SimpleHttpRequest("HttpClientManagerTests");
auto ctx = new EventsUploadContext();
auto ctx = std::make_shared<EventsUploadContext>();
ctx->httpRequestId = req->GetId();
ctx->httpRequest = req;
ctx->recordIdsAndTenantIds["r1"] = "t1"; ctx->recordIdsAndTenantIds["r2"] = "t1";

Просмотреть файл

@ -74,7 +74,7 @@ static std::vector<uint8_t> testPayload = { 1, 2, 3, 3, 3, 3, 3, 3, 3, 3 };
TEST_F(HttpDeflateCompressionTests, DoesNothingWhenTurnedOff)
{
config[CFG_MAP_HTTP][CFG_BOOL_HTTP_COMPRESSION] = false;
EventsUploadContextPtr event = new EventsUploadContext();
EventsUploadContextPtr event = std::make_shared<EventsUploadContext>();
EXPECT_THAT(event->compressed, false);
event->body = testPayload;
@ -88,7 +88,7 @@ TEST_F(HttpDeflateCompressionTests, DoesNothingWhenTurnedOff)
TEST_F(HttpDeflateCompressionTests, CompressesCorrectly)
{
config[CFG_MAP_HTTP][CFG_BOOL_HTTP_COMPRESSION] = true;
EventsUploadContextPtr event = new EventsUploadContext();
EventsUploadContextPtr event = std::make_shared<EventsUploadContext>();
EXPECT_THAT(event->compressed, false);
event->body = testPayload;
@ -105,7 +105,7 @@ TEST_F(HttpDeflateCompressionTests, CompressesCorrectly)
TEST_F(HttpDeflateCompressionTests, WorksMultipleTimes)
{
config[CFG_MAP_HTTP][CFG_BOOL_HTTP_COMPRESSION] = true;
EventsUploadContextPtr event = new EventsUploadContext();
EventsUploadContextPtr event = std::make_shared<EventsUploadContext>();
EXPECT_THAT(event->compressed, false);
event->body = {};
EXPECT_CALL(*this, resultSucceeded(event)).Times(1);
@ -114,7 +114,7 @@ TEST_F(HttpDeflateCompressionTests, WorksMultipleTimes)
EXPECT_THAT(event->compressed, true);
{
EventsUploadContextPtr event2 = new EventsUploadContext();
EventsUploadContextPtr event2 = std::make_shared<EventsUploadContext>();
EXPECT_THAT(event2->compressed, false);
event2->body = testPayload;
EXPECT_CALL(*this, resultSucceeded(event2)).Times(1);
@ -128,7 +128,7 @@ TEST_F(HttpDeflateCompressionTests, WorksMultipleTimes)
{
std::vector<uint8_t> testPayload2 = {};
EventsUploadContextPtr event3 = new EventsUploadContext();
EventsUploadContextPtr event3 = std::make_shared<EventsUploadContext>();
EXPECT_THAT(event3->compressed, false);
event3->body = testPayload2;
EXPECT_CALL(*this, resultSucceeded(event3)).Times(1);
@ -164,7 +164,7 @@ TEST_F(HttpDeflateCompressionTests, HasReasonableCompressionRatio)
#pragma warning( pop )
size_t const size = sizeof(bond) - 1;
EventsUploadContextPtr event = new EventsUploadContext();
EventsUploadContextPtr event = std::make_shared<EventsUploadContext>();
EXPECT_THAT(event->compressed, false);
event->body.assign(reinterpret_cast<uint8_t const*>(bond), reinterpret_cast<uint8_t const*>(bond) + size);
EXPECT_CALL(*this, resultSucceeded(event)).Times(1);

Просмотреть файл

@ -52,7 +52,7 @@ protected:
TEST_F(HttpRequestEncoderTests, SetsAllParameters)
{
EventsUploadContextPtr ctx = new EventsUploadContext();
EventsUploadContextPtr ctx = std::make_shared<EventsUploadContext>();
ctx->compressed = false;
ctx->body = { 1, 127, 255 };
ctx->packageIds["tenant1-token"] = 0;
@ -77,7 +77,7 @@ TEST_F(HttpRequestEncoderTests, SetsAllParameters)
TEST_F(HttpRequestEncoderTests, AddsCompressionHeader)
{
EventsUploadContextPtr ctx = new EventsUploadContext();
EventsUploadContextPtr ctx = std::make_shared<EventsUploadContext>();
ctx->compressed = false;
encoder.encode(ctx);
@ -94,7 +94,7 @@ TEST_F(HttpRequestEncoderTests, AddsCompressionHeader)
TEST_F(HttpRequestEncoderTests, BuildsApiKeyCorrectly)
{
EventsUploadContextPtr ctx = new EventsUploadContext();
EventsUploadContextPtr ctx = std::make_shared<EventsUploadContext>();
encoder.encode(ctx);
ASSERT_THAT(ctx->httpRequestId, Eq("HttpRequestEncoderTests"));
@ -117,7 +117,7 @@ TEST_F(HttpRequestEncoderTests, BuildsApiKeyCorrectly)
TEST_F(HttpRequestEncoderTests, DispatchDataViewerEventCorrectly)
{
EventsUploadContextPtr ctx = new EventsUploadContext();
EventsUploadContextPtr ctx = std::make_shared<EventsUploadContext>();
ctx->body = { 1, 127, 255 };
MockHttpRequestEncoder mockEncoder(system, mockHttpClient);

Просмотреть файл

@ -42,19 +42,19 @@ class HttpResponseDecoderTests : public StrictMock<Test> {
std::atomic<unsigned> reqId;
std::unique_ptr<EventsUploadContext> createContextWith(HttpResult result, int status, std::string const& body)
std::shared_ptr<EventsUploadContext> createContextWith(HttpResult result, int status, std::string const& body)
{
SimpleHttpResponse* rsp = (new SimpleHttpResponse("HttpResponseDecoderTests"));
rsp->m_result = result;
rsp->m_statusCode = status;
rsp->m_body.assign(reinterpret_cast<uint8_t const*>(body.data()), reinterpret_cast<uint8_t const*>(body.data()) + body.size());
EventsUploadContextPtr ctx = new EventsUploadContext();
auto ctx = std::make_shared<EventsUploadContext>();
ctx->httpRequest = new SimpleHttpRequest(std::to_string(reqId++));
ctx->httpRequestId = ctx->httpRequest->GetId();
ctx->httpResponse = (rsp);
ctx->durationMs = 1234;
return std::unique_ptr <EventsUploadContext>(ctx);
return ctx;
}
};
@ -62,47 +62,47 @@ class HttpResponseDecoderTests : public StrictMock<Test> {
TEST_F(HttpResponseDecoderTests, AcceptsAccepted)
{
auto ctx = createContextWith(HttpResult_OK, 200, "");
EXPECT_CALL(*this, resultEventsAccepted(ctx.get())).WillOnce(Return());
decoder.decode(ctx.get());
EXPECT_CALL(*this, resultEventsAccepted(ctx)).WillOnce(Return());
decoder.decode(ctx);
}
TEST_F(HttpResponseDecoderTests, RejectsRejected)
{
auto ctx = createContextWith(HttpResult_OK, 404, "<h1>Service not found</h1>");
EXPECT_CALL(*this, resultEventsRejected(ctx.get())).WillOnce(Return());
decoder.decode(ctx.get());
EXPECT_CALL(*this, resultEventsRejected(ctx)).WillOnce(Return());
decoder.decode(ctx);
}
TEST_F(HttpResponseDecoderTests, UnderstandsTemporaryServerFailures)
{
auto ctx = createContextWith(HttpResult_OK, 500, "{error:500,detail:\"Bad karma\"}");
EXPECT_CALL(*this, resultTemporaryServerFailure(ctx.get()))
EXPECT_CALL(*this, resultTemporaryServerFailure(ctx))
.WillOnce(Return());
decoder.decode(ctx.get());
decoder.decode(ctx);
ctx = createContextWith(HttpResult_OK, 408, "Timeout");
EXPECT_CALL(*this, resultTemporaryServerFailure(ctx.get()))
EXPECT_CALL(*this, resultTemporaryServerFailure(ctx))
.WillOnce(Return());
decoder.decode(ctx.get());
decoder.decode(ctx);
}
TEST_F(HttpResponseDecoderTests, UnderstandsTemporaryNetworkFailures)
{
auto ctx = createContextWith(HttpResult_LocalFailure, -1, "");
EXPECT_CALL(*this, resultTemporaryNetworkFailure(ctx.get()))
EXPECT_CALL(*this, resultTemporaryNetworkFailure(ctx))
.WillOnce(Return());
decoder.decode(ctx.get());
decoder.decode(ctx);
ctx = createContextWith(HttpResult_NetworkFailure, -1, "");
EXPECT_CALL(*this, resultTemporaryNetworkFailure(ctx.get()))
EXPECT_CALL(*this, resultTemporaryNetworkFailure(ctx))
.WillOnce(Return());
decoder.decode(ctx.get());
decoder.decode(ctx);
}
TEST_F(HttpResponseDecoderTests, SkipsAbortedRequests)
{
auto ctx = createContextWith(HttpResult_Aborted, -1, "");
EXPECT_CALL(*this, resultRequestAborted(ctx.get()))
EXPECT_CALL(*this, resultRequestAborted(ctx))
.WillOnce(Return());
decoder.decode(ctx.get());
decoder.decode(ctx);
}

Просмотреть файл

@ -81,7 +81,7 @@ TEST_F(OfflineStorageTests, StoreRecordIsForwarded)
TEST_F(OfflineStorageTests, RetrieveEventsPassesRecordsThrough)
{
auto ctx = new EventsUploadContext();
auto ctx = std::make_shared<EventsUploadContext>();
ctx->requestedMinLatency = EventLatency_Normal;
ctx->requestedMaxCount = 6;
@ -115,7 +115,7 @@ TEST_F(OfflineStorageTests, RetrieveEventsPassesRecordsThrough)
TEST_F(OfflineStorageTests, RetrieveEventsFailureAborts)
{
auto ctx = new EventsUploadContext();
auto ctx = std::make_shared<EventsUploadContext>();
ctx->requestedMinLatency = EventLatency_Normal;
ctx->requestedMaxCount = 6;
@ -130,7 +130,7 @@ TEST_F(OfflineStorageTests, RetrieveEventsFailureAborts)
TEST_F(OfflineStorageTests, DeleteRecordsIsForwarded)
{
auto ctx = new EventsUploadContext();
auto ctx = std::make_shared<EventsUploadContext>();
HttpHeaders test;
bool fromMemory = false;
std::vector<std::string> recordIds;
@ -145,7 +145,7 @@ TEST_F(OfflineStorageTests, DeleteRecordsIsForwarded)
TEST_F(OfflineStorageTests, ReleaseRecordsIsForwarded)
{
auto ctx = new EventsUploadContext();
auto ctx = std::make_shared<EventsUploadContext>();
HttpHeaders test;
bool fromMemory = false;
std::vector<std::string> recordIds;

Просмотреть файл

@ -35,7 +35,7 @@ class PackagerTests : public StrictMock<Test> {
TEST_F(PackagerTests, EmptyInputResultsInEmptyPackage)
{
auto ctx = new EventsUploadContext();
auto ctx = std::make_shared<EventsUploadContext>();
EXPECT_CALL(*this, resultEmptyPackage(ctx))
.WillOnce(Return());
packager.finalizePackage(ctx);
@ -43,7 +43,7 @@ TEST_F(PackagerTests, EmptyInputResultsInEmptyPackage)
TEST_F(PackagerTests, PackagesEventsByTenant)
{
auto ctx = new EventsUploadContext();
auto ctx = std::make_shared<EventsUploadContext>();
EXPECT_CALL(runtimeConfigMock, GetMaximumUploadSizeBytes())
.WillOnce(Return(100000))
.RetiresOnSaturation();
@ -68,7 +68,7 @@ TEST_F(PackagerTests, PackagesEventsByTenant)
EXPECT_THAT(ctx->packageIds, Contains(Key("tenant1-token")));
ctx = new EventsUploadContext();
ctx = std::make_shared<EventsUploadContext>();
EXPECT_CALL(runtimeConfigMock, GetMaximumUploadSizeBytes())
.WillOnce(Return(100000))
.RetiresOnSaturation();
@ -99,7 +99,7 @@ TEST_F(PackagerTests, PackagesEventsByTenant)
TEST_F(PackagerTests, UsesPriorityOfTheFirstEvent)
{
auto ctx = new EventsUploadContext();
auto ctx = std::make_shared<EventsUploadContext>();
EXPECT_CALL(runtimeConfigMock, GetMaximumUploadSizeBytes())
.WillOnce(Return(100000))
.RetiresOnSaturation();
@ -116,7 +116,7 @@ TEST_F(PackagerTests, HonorsMaximumPackageSize)
unsigned const MaxSize = 100000;
unsigned const PartSize = (MaxSize - 200) / 3;
auto ctx = new EventsUploadContext();
auto ctx = std::make_shared<EventsUploadContext>();
EXPECT_CALL(runtimeConfigMock, GetMaximumUploadSizeBytes())
.WillOnce(Return(MaxSize))
.RetiresOnSaturation();
@ -143,7 +143,7 @@ TEST_F(PackagerTests, PackagesAtLeastOneEventEvenIfOverSizeLimit)
{
unsigned const MaxSize = 10000;
auto ctx = new EventsUploadContext();
auto ctx = std::make_shared<EventsUploadContext>();
EXPECT_CALL(runtimeConfigMock, GetMaximumUploadSizeBytes())
.WillOnce(Return(MaxSize))
.RetiresOnSaturation();
@ -162,7 +162,7 @@ TEST_F(PackagerTests, PackagesAtLeastOneEventEvenIfOverSizeLimit)
TEST_F(PackagerTests, SetsRequestBondFieldsCorrectly)
{
auto ctx = new EventsUploadContext();
auto ctx = std::make_shared<EventsUploadContext>();
EXPECT_CALL(runtimeConfigMock, GetMaximumUploadSizeBytes())
.WillOnce(Return(100000))
.RetiresOnSaturation();
@ -208,7 +208,7 @@ TEST_F(PackagerTests, ForcedTenantIsForced)
Packager packagerF(runtimeConfigMock);
packagerF.packagedEvents >> packagedEvents;
auto ctx = new EventsUploadContext();
auto ctx = std::make_shared<EventsUploadContext>();
EXPECT_CALL(runtimeConfigMock, GetMaximumUploadSizeBytes())
.WillOnce(Return(100000))
.RetiresOnSaturation();

Просмотреть файл

@ -38,8 +38,8 @@ class TransmissionPolicyManager4Test : public TransmissionPolicyManager {
void uploadScheduled(bool state) { m_isUploadScheduled = state; }
std::set<EventsUploadContextPtr> const& activeUploads() const { return m_activeUploads; }
EventsUploadContextPtr fakeActiveUpload() { auto ctx = new EventsUploadContext(); ctx->requestedMinLatency = EventLatency_RealTime; m_activeUploads.insert(ctx); return ctx; }
EventsUploadContextPtr fakeActiveUpload(EventLatency latency) { auto ctx = new EventsUploadContext(); ctx->requestedMinLatency = latency; m_activeUploads.insert(ctx); return ctx; }
EventsUploadContextPtr fakeActiveUpload() { auto ctx = std::make_shared<EventsUploadContext>(); ctx->requestedMinLatency = EventLatency_RealTime; m_activeUploads.insert(ctx); return ctx; }
EventsUploadContextPtr fakeActiveUpload(EventLatency latency) { auto ctx = std::make_shared<EventsUploadContext>(); ctx->requestedMinLatency = latency; m_activeUploads.insert(ctx); return ctx; }
bool paused() const { return m_isPaused; }
void paused(bool state) { m_isPaused = state; }