-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathlive_threaded_tests.cpp
More file actions
434 lines (413 loc) · 16.5 KB
/
Copy pathlive_threaded_tests.cpp
File metadata and controls
434 lines (413 loc) · 16.5 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <atomic>
#include <chrono>
#include <cstdint>
#include <exception>
#include <memory>
#include <thread> // this_thread
#include <variant>
#include "databento/constants.hpp"
#include "databento/datetime.hpp"
#include "databento/dbn.hpp"
#include "databento/enums.hpp"
#include "databento/exceptions.hpp"
#include "databento/live.hpp"
#include "databento/live_subscription.hpp"
#include "databento/live_threaded.hpp"
#include "databento/log.hpp"
#include "databento/record.hpp"
#include "databento/symbology.hpp"
#include "databento/timeseries.hpp"
#include "mock/mock_log_receiver.hpp"
#include "mock/mock_lsg_server.hpp"
namespace databento::tests {
class LiveThreadedTests : public testing::Test {
protected:
template <typename T>
static constexpr RecordHeader DummyHeader(RType rtype) {
return {sizeof(T) / RecordHeader::kLengthMultiplier, rtype, 1, 1, UnixNanos{}};
}
static constexpr auto kKey = "32-character-with-lots-of-filler";
static constexpr auto kTsOut = false;
static constexpr auto kLocalhost = "127.0.0.1";
mock::MockLogReceiver logger_ =
mock::MockLogReceiver::AssertNoLogs(LogLevel::Warning);
LiveBuilder builder_{LiveBuilder{}.SetLogReceiver(&logger_).SetKey(kKey)};
};
TEST_F(LiveThreadedTests, TestBasic) {
const MboMsg kRec{DummyHeader<MboMsg>(RType::Mbo),
1,
2,
3,
{},
4,
Action::Add,
Side::Bid,
UnixNanos{},
TimeDeltaNanos{},
100};
constexpr auto kHeartbeatInterval = std::chrono::seconds{5};
const mock::MockLsgServer mock_server{dataset::kGlbxMdp3, kTsOut, kHeartbeatInterval,
[&kRec](mock::MockLsgServer& self) {
self.Accept();
self.Authenticate();
self.Start();
self.SendRecord(kRec);
self.SendRecord(kRec);
}};
LiveThreaded target = builder_.SetDataset(dataset::kGlbxMdp3)
.SetSendTsOut(kTsOut)
.SetHeartbeatInterval(kHeartbeatInterval)
.SetAddress(kLocalhost, mock_server.Port())
.BuildThreaded();
std::uint32_t call_count{};
target.Start([&call_count, &kRec](const Record& rec) {
++call_count;
EXPECT_TRUE(rec.Holds<MboMsg>());
EXPECT_EQ(rec.Get<MboMsg>(), kRec);
return call_count < 2 ? KeepGoing::Continue : KeepGoing::Stop;
});
target.BlockForStop();
}
TEST_F(LiveThreadedTests, TestWithZstdCompression) {
const MboMsg kRec{DummyHeader<MboMsg>(RType::Mbo),
1,
2,
3,
{},
4,
Action::Add,
Side::Bid,
UnixNanos{},
TimeDeltaNanos{},
100};
const mock::MockLsgServer mock_server{dataset::kGlbxMdp3, kTsOut, Compression::Zstd,
[&kRec](mock::MockLsgServer& self) {
self.Accept();
self.Authenticate();
self.StartCompressed();
self.SendCompressedRecord(kRec);
self.SendCompressedRecord(kRec);
self.FlushCompression();
}};
LiveThreaded target = builder_.SetDataset(dataset::kGlbxMdp3)
.SetSendTsOut(kTsOut)
.SetCompression(Compression::Zstd)
.SetAddress(kLocalhost, mock_server.Port())
.BuildThreaded();
std::uint32_t call_count{};
target.Start([&call_count, &kRec](const Record& rec) {
++call_count;
EXPECT_TRUE(rec.Holds<MboMsg>());
EXPECT_EQ(rec.Get<MboMsg>(), kRec);
return call_count < 2 ? KeepGoing::Continue : KeepGoing::Stop;
});
target.BlockForStop();
}
TEST_F(LiveThreadedTests, TestTimeoutRecovery) {
const MboMsg kRec{DummyHeader<MboMsg>(RType::Mbo),
1,
2,
3,
{},
4,
Action::Add,
Side::Bid,
UnixNanos{},
TimeDeltaNanos{},
100};
std::atomic<std::uint32_t> call_count{};
const mock::MockLsgServer mock_server{
dataset::kXnasItch, kTsOut, [&kRec, &call_count](mock::MockLsgServer& self) {
self.Accept();
self.Authenticate();
self.Start();
self.SendRecord(kRec);
while (call_count < 1) {
std::this_thread::yield();
}
// 150% of live threaded timeout
std::this_thread::sleep_for(std::chrono::milliseconds{75});
self.SendRecord(kRec);
}};
LiveThreaded target = builder_.SetDataset(dataset::kXnasItch)
.SetSendTsOut(kTsOut)
.SetAddress(kLocalhost, mock_server.Port())
.BuildThreaded();
target.Start([](Metadata&& metadata) { EXPECT_FALSE(metadata.schema.has_value()); },
[&call_count, &kRec](const Record& rec) {
++call_count;
EXPECT_TRUE(rec.Holds<MboMsg>());
EXPECT_EQ(rec.Get<MboMsg>(), kRec);
return databento::KeepGoing::Continue;
});
while (call_count < 2) {
std::this_thread::yield();
}
}
TEST_F(LiveThreadedTests, TestTimeoutRecoveryWithZstdCompression) {
const MboMsg kRec{DummyHeader<MboMsg>(RType::Mbo),
1,
2,
3,
{},
4,
Action::Add,
Side::Bid,
UnixNanos{},
TimeDeltaNanos{},
100};
std::atomic<std::uint32_t> call_count{};
const mock::MockLsgServer mock_server{
dataset::kXnasItch, kTsOut, Compression::Zstd,
[&kRec, &call_count](mock::MockLsgServer& self) {
self.Accept();
self.Authenticate();
self.StartCompressed();
self.SendCompressedRecord(kRec);
while (call_count < 1) {
std::this_thread::yield();
}
// 150% of live threaded timeout
std::this_thread::sleep_for(std::chrono::milliseconds{75});
self.SendCompressedRecord(kRec);
self.FlushCompression();
}};
LiveThreaded target = builder_.SetDataset(dataset::kXnasItch)
.SetSendTsOut(kTsOut)
.SetCompression(Compression::Zstd)
.SetAddress(kLocalhost, mock_server.Port())
.BuildThreaded();
target.Start([](Metadata&& metadata) { EXPECT_FALSE(metadata.schema.has_value()); },
[&call_count, &kRec](const Record& rec) {
++call_count;
EXPECT_TRUE(rec.Holds<MboMsg>());
EXPECT_EQ(rec.Get<MboMsg>(), kRec);
return databento::KeepGoing::Continue;
});
while (call_count < 2) {
std::this_thread::yield();
}
}
TEST_F(LiveThreadedTests, TestStop) {
const MboMsg kRec{DummyHeader<MboMsg>(RType::Mbo),
1,
2,
3,
{},
4,
Action::Add,
Side::Bid,
UnixNanos{},
TimeDeltaNanos{},
100};
std::atomic<std::uint32_t> call_count{};
auto mock_server = std::make_unique<mock::MockLsgServer>(
dataset::kXnasItch, kTsOut, [&kRec, &call_count](mock::MockLsgServer& self) {
self.Accept();
self.Authenticate();
self.Start();
self.SendRecord(kRec);
self.SendRecord(kRec);
while (call_count < 1) {
std::this_thread::yield();
}
const std::string rec_str{reinterpret_cast<const char*>(&kRec), sizeof(kRec)};
while (self.UncheckedSend(rec_str) == static_cast<::ssize_t>(rec_str.size())) {
std::this_thread::yield();
}
});
LiveThreaded target = builder_.SetDataset(dataset::kXnasItch)
.SetSendTsOut(kTsOut)
.SetAddress(kLocalhost, mock_server->Port())
.BuildThreaded();
target.Start([](Metadata&& metadata) { EXPECT_FALSE(metadata.schema.has_value()); },
[&call_count, &kRec](const Record& rec) {
++call_count;
EXPECT_EQ(call_count, 1) << "Record callback called more than once";
EXPECT_TRUE(rec.Holds<MboMsg>());
EXPECT_EQ(rec.Get<MboMsg>(), kRec);
return databento::KeepGoing::Stop;
});
// kill mock server and join thread before client goes out of scope
// to ensure Stop is killing the connection, not the client's destructor
mock_server.reset();
}
TEST_F(LiveThreadedTests, TestExceptionCallbackReconnectAndResubscribe) {
constexpr auto kSchema = Schema::Trades;
constexpr auto kSType = SType::RawSymbol;
constexpr TradeMsg kRec{DummyHeader<TradeMsg>(RType::Mbp0),
1,
2,
Action::Add,
Side::Ask,
{},
1,
{},
{},
2};
const auto kUseSnapshot = true;
bool should_close{};
std::mutex should_close_mutex;
std::condition_variable should_close_cv;
const mock::MockLsgServer mock_server{
dataset::kXnasItch, kTsOut,
[&should_close, &should_close_mutex, &should_close_cv, kRec, kSchema, kSType,
kUseSnapshot](mock::MockLsgServer& self) {
self.Accept();
self.Authenticate();
self.Subscribe(kAllSymbols, kSchema, kSType, "0", true);
self.Start();
self.SendRecord(kRec);
{
std::unique_lock<std::mutex> shutdown_lock{should_close_mutex};
should_close_cv.wait(shutdown_lock, [&should_close] { return should_close; });
}
self.Close();
self.Accept();
self.Authenticate();
self.Subscribe(kAllSymbols, kSchema, kSType, true);
self.Start();
self.SendRecord(kRec);
}};
logger_ = mock::MockLogReceiver{
LogLevel::Warning,
[](auto count, databento::LogLevel level, const std::string& msg) {
EXPECT_THAT(msg,
testing::EndsWith(
"Gateway closed the session. Attempting to restart session."));
}};
LiveThreaded target = builder_.SetDataset(dataset::kXnasItch)
.SetSendTsOut(kTsOut)
.SetAddress(kLocalhost, mock_server.Port())
.BuildThreaded();
std::atomic<std::int32_t> metadata_calls{};
const auto metadata_cb = [&metadata_calls](Metadata&& metadata) {
++metadata_calls;
EXPECT_FALSE(metadata.schema.has_value());
};
std::atomic<std::int32_t> record_calls{};
const auto record_cb = [&record_calls, kRec, &should_close_mutex, &should_close,
&should_close_cv](const Record& record) {
++record_calls;
EXPECT_EQ(record.Get<TradeMsg>(), kRec);
if (record_calls == 1) { // close server
const std::lock_guard<std::mutex> _lock{should_close_mutex};
should_close = true;
should_close_cv.notify_one();
return KeepGoing::Continue;
}
return KeepGoing::Stop;
};
std::atomic<std::int32_t> exception_calls{};
const auto exception_cb = [&exception_calls, &target, kSchema,
kSType](const std::exception& exc) {
++exception_calls;
if (exception_calls == 1) {
EXPECT_NE(dynamic_cast<const databento::LiveApiError*>(&exc), nullptr)
<< "Unexpected exception type";
target.Reconnect();
target.Resubscribe();
EXPECT_EQ(target.Subscriptions().size(), 1);
EXPECT_TRUE(std::holds_alternative<LiveSubscription::NoStart>(
target.Subscriptions()[0].start));
return LiveThreaded::ExceptionAction::Restart;
} else {
GTEST_NONFATAL_FAILURE_("Exception callback called more than expected");
return LiveThreaded::ExceptionAction::Stop;
}
};
ASSERT_TRUE(target.Subscriptions().empty());
target.Subscribe(kAllSymbols, kSchema, kSType, "0");
ASSERT_EQ(target.Subscriptions().size(), 1);
target.Start(metadata_cb, record_cb, exception_cb);
target.BlockForStop();
EXPECT_EQ(metadata_calls, 2);
EXPECT_EQ(exception_calls, 1);
EXPECT_EQ(record_calls, 2);
EXPECT_EQ(logger_.CallCount(), 1);
}
TEST_F(LiveThreadedTests, TestDeadlockPrevention) {
const auto kSchema = Schema::Trades;
const auto kSType = SType::Parent;
const std::vector<std::string> kSymbols = {"LO.OPT", "6E.FUT"};
bool should_close{};
std::mutex should_close_mutex;
std::condition_variable should_close_cv;
logger_ = mock::MockLogReceiver{
LogLevel::Warning,
[](auto count, databento::LogLevel level, const std::string& msg) {
if (count == 0) {
EXPECT_THAT(msg, testing::HasSubstr("which would cause a deadlock"))
<< "Got unexpected log message " << level << ": " << msg;
}
}};
const mock::MockLsgServer mock_server{
dataset::kXnasItch, kTsOut,
[&kSymbols, &should_close, &should_close_mutex, &should_close_cv, kSchema,
kSType](mock::MockLsgServer& self) {
self.Accept();
self.Authenticate();
self.Start();
{
std::unique_lock<std::mutex> shutdown_lock{should_close_mutex};
should_close_cv.wait(shutdown_lock, [&should_close] { return should_close; });
}
self.Close();
self.Accept();
self.Authenticate();
self.Subscribe(kSymbols, kSchema, kSType, true);
}};
LiveThreaded target = builder_.SetDataset(dataset::kXnasItch)
.SetSendTsOut(kTsOut)
.SetAddress(kLocalhost, mock_server.Port())
.BuildThreaded();
std::atomic<std::int32_t> metadata_calls{};
const auto metadata_cb = [&metadata_calls, &should_close, &should_close_cv,
&should_close_mutex](Metadata&&) {
++metadata_calls;
// close server
const std::lock_guard<std::mutex> _lock{should_close_mutex};
should_close = true;
should_close_cv.notify_one();
};
std::atomic<std::int32_t> record_calls{};
const auto record_cb = [&record_calls](const Record&) {
++record_calls;
return KeepGoing::Continue;
};
const auto exception_cb = [&target, &metadata_cb, &record_cb, &kSymbols, kSchema,
kSType](const std::exception& exc) {
EXPECT_NE(dynamic_cast<const databento::LiveApiError*>(&exc), nullptr)
<< "Unexpected exception type";
target.Reconnect();
target.Subscribe(kSymbols, kSchema, kSType);
// Not supposed to do this
target.Start(metadata_cb, record_cb, [](const std::exception&) {
GTEST_NONFATAL_FAILURE_("Unexpectedly called exception callback");
return LiveThreaded::ExceptionAction::Stop;
});
return LiveThreaded::ExceptionAction::Stop;
};
target.Start(metadata_cb, record_cb, exception_cb);
target.BlockForStop();
EXPECT_GE(logger_.CallCount(), 1);
}
TEST_F(LiveThreadedTests, TestBlockForStopTimeout) {
constexpr OhlcvMsg kRec{DummyHeader<OhlcvMsg>(RType::Ohlcv1S), 1, 2, 3, 4, 5};
const mock::MockLsgServer mock_server{dataset::kXnasItch, kTsOut,
[&kRec](mock::MockLsgServer& self) {
self.Accept();
self.Authenticate();
self.Start();
self.SendRecord(kRec);
}};
LiveThreaded target = builder_.SetDataset(dataset::kXnasItch)
.SetSendTsOut(kTsOut)
.SetAddress(kLocalhost, mock_server.Port())
.BuildThreaded();
target.Start([](const Record&) { return KeepGoing::Continue; });
ASSERT_EQ(target.BlockForStop(std::chrono::milliseconds{100}), KeepGoing::Continue);
}
} // namespace databento::tests