-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-lib.js
200 lines (158 loc) · 4.16 KB
/
test-lib.js
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
const defaults = require('transactional-db').Test;
const Long = require('long');
const client = require('node-eventstore-client');
/* Mock subscription */
class Subscription {
constructor(onEvent, onDone, onLive) {
this.onEvent = onEvent;
this.onDone = onDone;
this.onLive = onLive;
this.live = false;
}
push(event, live = true) {
if (live && this.onLive && !this.live) {
this.live = true;
this.onLive();
}
if (!this.stopped && (live || this.onLive)) {
this.onEvent(null, event);
}
}
pushOld(events) {
events.forEach(event => this.push(event, false));
}
stop() {
this.stopped = true;
this.onDone();
}
}
/* Mock transaction */
class Transaction {
constructor(conn, sid) {
this.conn = conn;
this.sid = sid;
this.tape = [];
}
write(events) {
events = events instanceof Array ? events : [events];
events = events.map(event => this.conn.eventAdapter(event));
this.tape = this.tape.concat(events);
return Promise.resolve();
}
commit() {
if (!this.conn.events[this.sid]) {
this.conn.events[this.sid] = [];
}
this.tape.forEach(event => this.conn.notify(this.sid, event));
//good for catch ups
this.tape.forEach(event => this.conn.events[this.sid].push(event));
return Promise.resolve();
}
}
/* Mock connection */
class Connection {
constructor(eventAdapter = (x => x)) {
this.subscriptions = {};
this.all = [];
this.events = {};
this.eventAdapter = eventAdapter;
}
notify(sid, event) {
//live notifications for each subscription
(this.subscriptions[sid] || []).forEach(sub => sub.push(event));
this.all.forEach(sub => sub.push(event));
}
subscribeToStream(sid, links, onEvent, onDone, userCredentials) {
if (!this.subscriptions[sid]) {
this.subscriptions[sid] = [];
}
const s = new Subscription(onEvent, onDone);
this.subscriptions[sid].push(s);
return Promise.resolve(s);
}
subscribeToStreamFrom(sid, pos, links, onEvent, onLive, onDone, credentials, batch) {
pos = pos || 0;
if (!this.subscriptions[sid]) {
this.subscriptions[sid] = [];
}
const s = new Subscription(onEvent, onDone, onLive);
this.subscriptions[sid].push(s);
setTimeout(() => {
const events = (this.events[sid] || []).slice(pos);
events.forEach(event => s.push(event, false));
onLive();
s.live = true;
}, 100)
return s;
}
subscribeToAll(links, onEvent, onDone, credentials) {
const s = new Subscription(onEvent, onDone);
this.all.push(s);
return Promise.resolve(s);
}
subscribeToAllFrom(pos, links, onEvent, onLive, onDone, credentials, batch) {
const s = new Subscription(onEvent, onDone, onLive);
this.all.push(s);
setImmediate(() => {
const events = Object.keys(this.events).map(k => this.events[k]);
const all = [].concat(...events);
all.forEach(event => s.push(event, false));
});
return s;
}
startTransaction(sid, version, credentials) {
return Promise.resolve(new Transaction(this, sid));
}
readStreamEventsForward(sid, start, count, b, credentials) {
const res = (this.events[sid] || []).slice(start).slice(0, count);
return Promise.resolve({events: res});
}
readStreamEventsBackward(sid, start, count, b, credentials) {
function get(events, start, end) {
events = start > 0 ? events : [].concat(events).reverse();
start = start > 0 ? start : start * -1 - 1;
return events.slice(start, start + end);
}
const res = get(this.events[sid] || [], start, count);
return Promise.resolve({events: res});
}
}
function makeEvent(type, data) {
return {
originalEvent: {
eventType: type,
data: JSON.stringify(data),
isJson: true,
},
originalPosition: new client.Position(
new Long(0, 0),
new Long(0, 0)
)
};
}
function wrapEvent(event) {
return {
originalEvent: Object.assign({}, event, {
eventType: event.type,
eventNumber: 0
}),
originalPosition: new client.Position(
new Long(0, 0),
new Long(0, 0)
)
}
};
function writeEvent(conn, stream, type, data) {
const event = makeEvent(type, data);
return conn.startTransaction(stream, 0, null)
.then(trans =>
trans.write(event)
.then(_ => trans.commit())
);
}
module.exports = Object.assign({}, defaults, {
Connection,
makeEvent,
writeEvent,
wrapEvent
});