-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathOfflineStorageTests.cpp
164 lines (138 loc) · 6.91 KB
/
OfflineStorageTests.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright (c) Microsoft Corporation. All rights reserved.
#include "common/Common.hpp"
#include "common/MockIOfflineStorage.hpp"
#include "offline/StorageObserver.hpp"
using namespace testing;
using namespace MAT;
class OfflineStorageTests : public StrictMock<Test> {
protected:
MockIOfflineStorage offlineStorageMock;
StorageObserver offlineStorage;
RouteSink<OfflineStorageTests, IncomingEventContextPtr const&> storeRecordFailed{ this, &OfflineStorageTests::resultStoreRecordFailed };
RouteSink<OfflineStorageTests, EventsUploadContextPtr const&, StorageRecord const&, bool&> retrievedEvent{ this, &OfflineStorageTests::resultRetrievedEvent };
RouteSink<OfflineStorageTests, EventsUploadContextPtr const&> retrievalFinished{ this, &OfflineStorageTests::resultRetrievalFinished };
RouteSink<OfflineStorageTests, EventsUploadContextPtr const&> retrievalFailed{ this, &OfflineStorageTests::resultRetrievalFailed };
RouteSink<OfflineStorageTests, StorageNotificationContext const*> opened{ this, &OfflineStorageTests::notifOpened };
RouteSink<OfflineStorageTests, StorageNotificationContext const*> failed{ this, &OfflineStorageTests::notifFailed };
RouteSink<OfflineStorageTests, StorageNotificationContext const*> trimmed{ this, &OfflineStorageTests::notifTrimmed };
RouteSink<OfflineStorageTests, StorageNotificationContext const*> recordsDropped{ this, &OfflineStorageTests::notifRecordsDropped };
protected:
OfflineStorageTests()
: offlineStorage(testing::getSystem(), offlineStorageMock)
{
offlineStorage.storeRecordFailed >> storeRecordFailed;
offlineStorage.retrievedEvent >> retrievedEvent;
offlineStorage.retrievalFinished >> retrievalFinished;
offlineStorage.retrievalFailed >> retrievalFailed;
offlineStorage.opened >> opened;
offlineStorage.failed >> failed;
offlineStorage.trimmed >> trimmed;
offlineStorage.recordsDropped >> recordsDropped;
}
MOCK_METHOD1(resultStoreRecordFailed, void(IncomingEventContextPtr const &));
MOCK_METHOD3(resultRetrievedEvent, void(EventsUploadContextPtr const &, StorageRecord const &, bool&));
MOCK_METHOD1(resultRetrievalFinished, void(EventsUploadContextPtr const &));
MOCK_METHOD1(resultRetrievalFailed, void(EventsUploadContextPtr const &));
MOCK_METHOD1(notifOpened, void(StorageNotificationContext const*));
MOCK_METHOD1(notifFailed, void(StorageNotificationContext const*));
MOCK_METHOD1(notifTrimmed, void(StorageNotificationContext const*));
MOCK_METHOD1(notifRecordsDropped, void(StorageNotificationContext const*));
};
TEST_F(OfflineStorageTests, StartInitializes)
{
EXPECT_CALL(offlineStorageMock, Initialize(Ref(offlineStorage)))
.WillOnce(Return());
EXPECT_THAT(offlineStorage.start(), true);
}
TEST_F(OfflineStorageTests, StopShutsDown)
{
EXPECT_CALL(offlineStorageMock, Shutdown())
.WillOnce(Return());
EXPECT_THAT(offlineStorage.stop(), true);
}
TEST_F(OfflineStorageTests, StoreRecordIsForwarded)
{
auto ctx = new IncomingEventContext();
EXPECT_CALL(offlineStorageMock, StoreRecord(Ref(ctx->record)))
.WillOnce(Return(true));
EXPECT_THAT(offlineStorage.storeRecord(ctx), true);
EXPECT_THAT(ctx->record.timestamp, Near(PAL::getUtcSystemTimeMs(), 1000));
EXPECT_CALL(offlineStorageMock, StoreRecord(Ref(ctx->record)))
.WillOnce(Return(false));
EXPECT_CALL(*this, resultStoreRecordFailed(ctx))
.WillOnce(Return());
EXPECT_THAT(offlineStorage.storeRecord(ctx), false);
}
TEST_F(OfflineStorageTests, RetrieveEventsPassesRecordsThrough)
{
auto ctx = std::make_shared<EventsUploadContext>();
ctx->requestedMinLatency = EventLatency_Normal;
ctx->requestedMaxCount = 6;
StorageRecord record1("r1", "tenant1-token", EventLatency_Normal, EventPersistence_Normal, 1234567890, std::vector<uint8_t>{1, 127, 255});
StorageRecord record2("r2", "tenant2-token", EventLatency_Normal, EventPersistence_Normal, 1234567891, std::vector<uint8_t>{2, 128, 0});
EXPECT_CALL(offlineStorageMock, GetAndReserveRecords(_, Gt(1000u), ctx->requestedMinLatency, ctx->requestedMaxCount))
.WillOnce(DoAll(
Invoke([&record1, &record2](std::function<bool(StorageRecord&&)> const& consumer, unsigned, EventLatency, unsigned) {
EXPECT_THAT(consumer(std::move(record1)), true);
EXPECT_THAT(consumer(std::move(record2)), false);
}),
Return(true)))
.RetiresOnSaturation();
EXPECT_CALL(offlineStorageMock, IsLastReadFromMemory())
.WillOnce(Return(false));
EXPECT_CALL(*this, resultRetrievedEvent(ctx, Ref(record1), _))
.WillOnce(SetArgReferee<2>(true))
.RetiresOnSaturation();
EXPECT_CALL(*this, resultRetrievedEvent(ctx, Ref(record2), _))
.WillOnce(SetArgReferee<2>(false))
.RetiresOnSaturation();
EXPECT_CALL(*this, resultRetrievalFinished(ctx))
.WillOnce(Return());
offlineStorage.retrieveEvents(ctx);
}
TEST_F(OfflineStorageTests, RetrieveEventsFailureAborts)
{
auto ctx = std::make_shared<EventsUploadContext>();
ctx->requestedMinLatency = EventLatency_Normal;
ctx->requestedMaxCount = 6;
EXPECT_CALL(offlineStorageMock, GetAndReserveRecords(_, Gt(1000u), ctx->requestedMinLatency, ctx->requestedMaxCount))
.WillOnce(Return(false));
EXPECT_CALL(offlineStorageMock, IsLastReadFromMemory())
.WillOnce(Return(false));
EXPECT_CALL(*this, resultRetrievalFailed(ctx))
.WillOnce(Return());
offlineStorage.retrieveEvents(ctx);
}
TEST_F(OfflineStorageTests, DeleteRecordsIsForwarded)
{
auto ctx = std::make_shared<EventsUploadContext>();
HttpHeaders test;
bool fromMemory = false;
std::vector<std::string> recordIds;
for (const auto& element : ctx->recordIdsAndTenantIds)
{
recordIds.push_back(element.first);
}
ctx->fromMemory = fromMemory;
EXPECT_CALL(offlineStorageMock, DeleteRecords(recordIds, test, fromMemory)).WillOnce(Return());
EXPECT_THAT(offlineStorage.deleteRecords(ctx), true);
}
TEST_F(OfflineStorageTests, ReleaseRecordsIsForwarded)
{
auto ctx = std::make_shared<EventsUploadContext>();
HttpHeaders test;
bool fromMemory = false;
std::vector<std::string> recordIds;
for (const auto& element : ctx->recordIdsAndTenantIds)
{
recordIds.push_back(element.first);
}
ctx->fromMemory = fromMemory;
EXPECT_CALL(offlineStorageMock, ReleaseRecords(recordIds, false, test, fromMemory))
.WillOnce(Return());
EXPECT_THAT(offlineStorage.releaseRecords(ctx), true);
fromMemory = false;
EXPECT_CALL(offlineStorageMock, ReleaseRecords(recordIds, true, test, fromMemory))
.WillOnce(Return());
EXPECT_THAT(offlineStorage.releaseRecordsIncRetryCount(ctx), true);
}