This repository was archived by the owner on Feb 16, 2019. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathsubscribe_to_all_catching_up_should.cs
More file actions
274 lines (239 loc) · 13.2 KB
/
Copy pathsubscribe_to_all_catching_up_should.cs
File metadata and controls
274 lines (239 loc) · 13.2 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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using EventStore.ClientAPI;
using EventStore.ClientAPI.Common.Log;
using EventStore.ClientAPI.SystemData;
using EventStore.Core.Tests.ClientAPI.Helpers;
using Newtonsoft.Json.Linq;
using NUnit.Framework;
namespace EventStore.Core.Tests.ClientAPI
{
[TestFixture, Category("LongRunning")]
public class subscribe_to_all_catching_up_should
{
private static readonly ILogger Log = new ConsoleLogger();
private static readonly TimeSpan Timeout = TimeSpan.FromSeconds(60);
private IEventStoreConnection _conn;
[SetUp]
public void SetUp()
{
_conn = BuildConnection();
_conn.ConnectAsync().Wait();
_conn.SetStreamMetadataAsync("$all", ExpectedVersion.Any,
StreamMetadata.Build().SetReadRole(SystemRoles.All),
new UserCredentials(SystemUsers.Admin, SystemUsers.DefaultAdminPassword)).Wait();
}
[TearDown]
public void TearDown()
{
_conn.SetStreamMetadataAsync("$all", ExpectedVersion.Any,
StreamMetadata.Build(),
new UserCredentials(SystemUsers.Admin, SystemUsers.DefaultAdminPassword)).Wait();
_conn.Close();
}
protected IEventStoreConnection BuildConnection()
{
return TestConnection.Create(TcpType.Normal);
}
[Test, Category("LongRunning")]
public void call_dropped_callback_after_stop_method_call()
{
using (var store = BuildConnection())
{
store.ConnectAsync().Wait();
var dropped = new CountdownEvent(1);
var subscription = store.SubscribeToAllFrom(null,
CatchUpSubscriptionSettings.Default,
(x, y) => { },
_ => Log.Info("Live processing started."),
(x, y, z) => dropped.Signal());
Assert.IsFalse(dropped.Wait(0));
subscription.Stop(Timeout);
Assert.IsTrue(dropped.Wait(Timeout));
}
}
[Test, Category("LongRunning")]
public void call_dropped_callback_when_an_error_occurs_while_processing_an_event()
{
const string stream = "all_call_dropped_callback_when_an_error_occurs_while_processing_an_event";
using (var store = BuildConnection())
{
store.ConnectAsync().Wait();
store.AppendToStreamAsync(stream, ExpectedVersion.Any, new EventData(Guid.NewGuid(), "event", false, new byte[3], null)).Wait();
var dropped = new CountdownEvent(1);
store.SubscribeToAllFrom(null, CatchUpSubscriptionSettings.Default,
(x, y) => { throw new Exception("Error"); },
_ => Log.Info("Live processing started."),
(x, y, z) => dropped.Signal());
Assert.IsTrue(dropped.Wait(Timeout));
}
}
[Test, Category("LongRunning"), Ignore("No way to guarantee an empty db")]
public void be_able_to_subscribe_to_empty_db()
{
using (var store = BuildConnection())
{
store.ConnectAsync().Wait();
var appeared = new ManualResetEventSlim(false);
var dropped = new CountdownEvent(1);
var subscription = store.SubscribeToAllFrom(null,
CatchUpSubscriptionSettings.Default,
(_, x) =>
{
if (!SystemStreams.IsSystemStream(x.OriginalEvent.EventStreamId))
appeared.Set();
},
_ => Log.Info("Live processing started."),
(_, __, ___) => dropped.Signal());
Thread.Sleep(100); // give time for first pull phase
store.SubscribeToAllAsync(false, (s, x) => { }, (s, r, e) => { }).Wait();
Thread.Sleep(100);
Assert.IsFalse(appeared.Wait(0), "Some event appeared.");
Assert.IsFalse(dropped.Wait(0), "Subscription was dropped prematurely.");
subscription.Stop(Timeout);
Assert.IsTrue(dropped.Wait(Timeout));
}
}
[Test, Category("LongRunning")]
public void read_all_existing_events_and_keep_listening_to_new_ones()
{
using (var store = BuildConnection())
{
store.ConnectAsync().Wait();
var position = _conn.ReadAllEventsBackwardAsync(Position.End, 1, false).Result.NextPosition;
var events = new List<ResolvedEvent>();
var appeared = new CountdownEvent(20);
var dropped = new CountdownEvent(1);
for (int i = 0; i < 10; ++i)
{
store.AppendToStreamAsync("all_read_all_existing_events_and_keep_listening_to_new_ones-" + i.ToString(), -1, new EventData(Guid.NewGuid(), "et-" + i.ToString(), false, new byte[3], null)).Wait();
}
var subscription = store.SubscribeToAllFrom(position,
CatchUpSubscriptionSettings.Default,
(x, y) =>
{
if (!SystemStreams.IsSystemStream(y.OriginalEvent.EventStreamId))
{
events.Add(y);
appeared.Signal();
}
},
_ => Log.Info("Live processing started."),
(x, y, z) => dropped.Signal());
for (int i = 10; i < 20; ++i)
{
store.AppendToStreamAsync("all_read_all_existing_events_and_keep_listening_to_new_ones-" + i.ToString(), -1, new EventData(Guid.NewGuid(), "et-" + i.ToString(), false, new byte[3], null)).Wait();
}
if (!appeared.Wait(Timeout))
{
Assert.IsFalse(dropped.Wait(0), "Subscription was dropped prematurely.");
Assert.Fail("Could not wait for all events.");
}
Assert.AreEqual(20, events.Count);
for (int i = 0; i < 20; ++i)
{
Assert.AreEqual("et-" + i.ToString(), events[i].OriginalEvent.EventType);
}
Assert.IsFalse(dropped.Wait(0));
subscription.Stop(Timeout);
Assert.IsTrue(dropped.Wait(Timeout));
}
}
[Test, Category("LongRunning"), Ignore("Not working against single db")]
public void filter_events_and_keep_listening_to_new_ones()
{
using (var store = BuildConnection())
{
store.ConnectAsync().Wait();
var events = new List<ResolvedEvent>();
var appeared = new CountdownEvent(10);
var dropped = new CountdownEvent(1);
for (int i = 0; i < 10; ++i)
{
store.AppendToStreamAsync("all_filter_events_and_keep_listening_to_new_ones-" + i.ToString(), -1, new EventData(Guid.NewGuid(), "et-" + i.ToString(), false, new byte[3], null)).Wait();
}
var allSlice = store.ReadAllEventsForwardAsync(Position.Start, 100, false).Result;
var lastEvent = allSlice.Events.Last();
var subscription = store.SubscribeToAllFrom(lastEvent.OriginalPosition,
CatchUpSubscriptionSettings.Default,
(x, y) =>
{
events.Add(y);
appeared.Signal();
},
_ => Log.Info("Live processing started."),
(x, y, z) =>
{
Log.Info("Subscription dropped: {0}, {1}.", y, z);
dropped.Signal();
});
for (int i = 10; i < 20; ++i)
{
store.AppendToStreamAsync("all_filter_events_and_keep_listening_to_new_ones-" + i.ToString(), -1, new EventData(Guid.NewGuid(), "et-" + i.ToString(), false, new byte[3], null)).Wait();
}
Log.Info("Waiting for events...");
if (!appeared.Wait(Timeout))
{
Assert.IsFalse(dropped.Wait(0), "Subscription was dropped prematurely.");
Assert.Fail("Could not wait for all events.");
}
Log.Info("Events appeared...");
Assert.AreEqual(10, events.Count);
for (int i = 0; i < events.Count; ++i)
{
Assert.AreEqual("et-" + (10 + i).ToString(), events[i].OriginalEvent.EventType);
}
Assert.IsFalse(dropped.Wait(0));
subscription.Stop(Timeout);
Assert.IsTrue(dropped.Wait(Timeout));
Assert.AreEqual(events.Last().OriginalPosition, subscription.LastProcessedPosition);
}
}
[Test, Category("LongRunning"), Ignore("not working against single db")]
public void filter_events_and_work_if_nothing_was_written_after_subscription()
{
using (var store = BuildConnection())
{
store.ConnectAsync().Wait();
var events = new List<ResolvedEvent>();
var appeared = new CountdownEvent(1);
var dropped = new CountdownEvent(1);
for (int i = 0; i < 10; ++i)
{
store.AppendToStreamAsync("all_filter_events_and_work_if_nothing_was_written_after_subscription-" + i.ToString(), -1, new EventData(Guid.NewGuid(), "et-" + i.ToString(), false, new byte[3], null)).Wait();
}
var allSlice = store.ReadAllEventsBackwardAsync(Position.End, 2, false).Result;
var lastEvent = allSlice.Events[1];
var subscription = store.SubscribeToAllFrom(lastEvent.OriginalPosition,
CatchUpSubscriptionSettings.Default,
(x, y) =>
{
events.Add(y);
appeared.Signal();
},
_ => Log.Info("Live processing started."),
(x, y, z) =>
{
Log.Info("Subscription dropped: {0}, {1}.", y, z);
dropped.Signal();
});
Log.Info("Waiting for events...");
if (!appeared.Wait(Timeout))
{
Assert.IsFalse(dropped.Wait(0), "Subscription was dropped prematurely.");
Assert.Fail("Could not wait for all events.");
}
Log.Info("Events appeared...");
Assert.AreEqual(1, events.Count);
Assert.AreEqual("et-9", events[0].OriginalEvent.EventType);
Assert.IsFalse(dropped.Wait(0));
subscription.Stop(Timeout);
Assert.IsTrue(dropped.Wait(Timeout));
Assert.AreEqual(events.Last().OriginalPosition, subscription.LastProcessedPosition);
}
}
}
}