-
Notifications
You must be signed in to change notification settings - Fork 10
/
OrderBookLevel2SourceTests.cs
246 lines (206 loc) · 8.51 KB
/
OrderBookLevel2SourceTests.cs
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
using System;
using System.Threading;
using System.Threading.Tasks;
using Crypto.Websocket.Extensions.Core.Models;
using Crypto.Websocket.Extensions.Core.OrderBooks;
using Crypto.Websocket.Extensions.Core.OrderBooks.Models;
using Crypto.Websocket.Extensions.Core.OrderBooks.Sources;
using Crypto.Websocket.Extensions.Core.Utils;
using Microsoft.Extensions.Logging.Abstractions;
using Xunit;
namespace Crypto.Websocket.Extensions.Tests
{
public class OrderBookLevel2SourceTests
{
#if DEBUG
[Fact]
public async Task Buffering_ShouldBeEnabledByDefault_StreamDataAfterSomeTime()
{
var now = CryptoDateUtils.ConvertFromUnixSeconds(1577575307.123451);
var snapshotLevels = new[]
{
new OrderBookLevel("1", CryptoOrderSide.Bid, 100, 5, 1, "BTCUSD"),
new OrderBookLevel("2", CryptoOrderSide.Ask, 101, 50, 2, "BTCUSD"),
};
var snapshot = new OrderBookLevelBulk(OrderBookAction.Insert, snapshotLevels, CryptoOrderBookType.L2)
{
ServerSequence = 3,
ServerTimestamp = now,
ExchangeName = "test"
};
var bulks = new[]
{
new OrderBookLevelBulk(OrderBookAction.Update, snapshotLevels, CryptoOrderBookType.L2)
{
ServerSequence = 4,
ServerTimestamp = now.AddMilliseconds(1),
ExchangeName = "test"
},
new OrderBookLevelBulk(OrderBookAction.Delete, snapshotLevels, CryptoOrderBookType.L2)
{
ServerSequence = 5,
ServerTimestamp = now.AddMilliseconds(2),
ExchangeName = "test"
}
};
var source = GetMock(snapshot, bulks);
source.BufferInterval = TimeSpan.FromMilliseconds(100);
OrderBookLevelBulk receivedSnapshot = null;
OrderBookLevelBulk[] receivedBulks = null;
source.OrderBookSnapshotStream.Subscribe(x => receivedSnapshot = x);
source.OrderBookStream.Subscribe(x => receivedBulks = x);
await source.LoadSnapshot("BTCUSD");
source.StreamData();
Assert.NotNull(receivedSnapshot);
Assert.Equal("test", receivedSnapshot.ExchangeName);
Assert.Equal(3, receivedSnapshot.ServerSequence);
Assert.Equal(now, receivedSnapshot.ServerTimestamp);
Assert.Null(receivedBulks);
await Task.Delay(500);
Assert.NotNull(receivedSnapshot);
Assert.NotNull(receivedBulks);
Assert.Equal("test", receivedBulks[1].ExchangeName);
Assert.Equal(5, receivedBulks[1].ServerSequence);
Assert.Equal("1577575307.125451", receivedBulks[1].ServerTimestamp.ToUnixSecondsString());
}
#endif
[Fact]
public async Task Buffering_Disable_ShouldWork()
{
var now = CryptoDateUtils.ConvertFromUnixSeconds(1577575307.123451);
var snapshotLevels = new[]
{
new OrderBookLevel("1", CryptoOrderSide.Bid, 100, 5, 1, "BTCUSD"),
new OrderBookLevel("2", CryptoOrderSide.Ask, 101, 50, 2, "BTCUSD"),
};
var snapshot = new OrderBookLevelBulk(OrderBookAction.Insert, snapshotLevels, CryptoOrderBookType.L2)
{
ServerSequence = 3,
ServerTimestamp = now,
ExchangeName = "test"
};
var bulks = new[]
{
new OrderBookLevelBulk(OrderBookAction.Update, snapshotLevels, CryptoOrderBookType.L2)
{
ServerSequence = 4,
ServerTimestamp = now.AddMilliseconds(1),
ExchangeName = "test"
},
new OrderBookLevelBulk(OrderBookAction.Delete, snapshotLevels, CryptoOrderBookType.L2)
{
ServerSequence = 5,
ServerTimestamp = now.AddMilliseconds(2),
ExchangeName = "test"
}
};
var source = GetMock(snapshot, bulks);
source.BufferEnabled = false;
OrderBookLevelBulk receivedSnapshot = null;
OrderBookLevelBulk[] receivedBulks = null;
source.OrderBookSnapshotStream.Subscribe(x => receivedSnapshot = x);
source.OrderBookStream.Subscribe(x => receivedBulks = x);
await source.LoadSnapshot("BTCUSD");
source.StreamData();
Assert.NotNull(receivedSnapshot);
Assert.NotNull(receivedBulks);
Assert.Equal("test", receivedSnapshot.ExchangeName);
Assert.Equal(3, receivedSnapshot.ServerSequence);
Assert.Equal(now, receivedSnapshot.ServerTimestamp);
Assert.Equal("test", receivedBulks[1].ExchangeName);
Assert.Equal(5, receivedBulks[1].ServerSequence);
Assert.Equal("1577575307.125451", receivedBulks[1].ServerTimestamp.ToUnixSecondsString());
}
#if DEBUG
[Fact]
public async Task Buffering_ShouldStreamOneByOne()
{
var now = CryptoDateUtils.ConvertFromUnixSeconds(1577575307.123456);
var snapshotLevels = new[]
{
new OrderBookLevel("1", CryptoOrderSide.Bid, 100, 5, 1, "BTCUSD"),
new OrderBookLevel("2", CryptoOrderSide.Ask, 101, 50, 2, "BTCUSD"),
};
var snapshot = new OrderBookLevelBulk(OrderBookAction.Insert, snapshotLevels, CryptoOrderBookType.L2)
{
ServerSequence = 3,
ServerTimestamp = now,
ExchangeName = "test"
};
var bulks = new[]
{
new OrderBookLevelBulk(OrderBookAction.Update, snapshotLevels, CryptoOrderBookType.L2)
{
ServerSequence = 4,
ServerTimestamp = now.AddMilliseconds(1),
ExchangeName = "test"
},
new OrderBookLevelBulk(OrderBookAction.Delete, snapshotLevels, CryptoOrderBookType.L2)
{
ServerSequence = 5,
ServerTimestamp = now.AddMilliseconds(2),
ExchangeName = "test"
}
};
var source = GetMock(snapshot, bulks);
source.BufferInterval = TimeSpan.FromMilliseconds(50);
OrderBookLevelBulk receivedSnapshot = null;
OrderBookLevelBulk[] receivedBulks = null;
var receivedCount = 0;
source.OrderBookSnapshotStream.Subscribe(x => receivedSnapshot = x);
source.OrderBookStream.Subscribe(x =>
{
receivedBulks = x;
receivedCount++;
Thread.Sleep(2000);
});
await source.LoadSnapshot("BTCUSD");
source.StreamData();
Assert.NotNull(receivedSnapshot);
Assert.Null(receivedBulks);
Assert.Equal(0, receivedCount);
await Task.Delay(100);
Assert.NotNull(receivedSnapshot);
Assert.NotNull(receivedBulks);
Assert.Equal(1, receivedCount);
source.StreamData();
source.StreamData();
await Task.Delay(100);
Assert.Equal(1, receivedCount);
source.StreamData();
await Task.Delay(2200);
Assert.Equal(2, receivedCount);
}
#endif
private MockSource GetMock(OrderBookLevelBulk snapshot, OrderBookLevelBulk[] bulks)
{
return new MockSource(snapshot, bulks);
}
}
public class MockSource : OrderBookSourceBase
{
private readonly OrderBookLevelBulk _snapshot;
private readonly OrderBookLevelBulk[] _bulks;
public MockSource(OrderBookLevelBulk snapshot, OrderBookLevelBulk[] bulks) : base(NullLogger.Instance)
{
_snapshot = snapshot;
_bulks = bulks;
}
public override string ExchangeName => "mock";
protected override Task<OrderBookLevelBulk> LoadSnapshotInternal(string pair, int count = 1000)
{
return Task.FromResult(_snapshot);
}
protected override OrderBookLevelBulk[] ConvertData(object[] data)
{
return _bulks;
}
public void StreamData()
{
foreach (var bulk in _bulks)
{
BufferData(bulk);
}
}
}
}