This repository has been archived by the owner on May 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathfeature_store_event_wrapper-test.js
182 lines (156 loc) · 6.78 KB
/
feature_store_event_wrapper-test.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
const EventEmitter = require('events').EventEmitter;
const FeatureStoreEventWrapper = require('../feature_store_event_wrapper');
const InMemoryFeatureStore = require('../feature_store');
const dataKind = require('../versioned_data_kind');
const { AsyncQueue, promisifySingle } = require('launchdarkly-js-test-helpers');
describe('FeatureStoreEventWrapper', () => {
function listenAndStoreEvents(emitter, queue, eventName) {
emitter.on(eventName, arg => {
queue.add([eventName, arg]);
});
}
it('sends events for init of empty store', async () => {
const store = InMemoryFeatureStore();
const allData = {
features: {
a: { key: 'a', version: 1 },
b: { key: 'b', version: 1 }
},
segments: {}
};
const emitter = new EventEmitter();
const queue = new AsyncQueue();
listenAndStoreEvents(emitter, queue, 'update');
listenAndStoreEvents(emitter, queue, 'update:a');
listenAndStoreEvents(emitter, queue, 'update:b');
const wrapper = FeatureStoreEventWrapper(store, emitter);
await promisifySingle(wrapper.init)(allData);
expect(await queue.take()).toEqual(['update', { key: 'a' }]);
expect(await queue.take()).toEqual(['update:a', { key: 'a' }]);
expect(await queue.take()).toEqual(['update', { key: 'b' }]);
expect(await queue.take()).toEqual(['update:b', { key: 'b' }]);
expect(queue.isEmpty()).toEqual(true);
});
it('sends events for reinit of non-empty store', async () => {
const store = InMemoryFeatureStore();
const allData0 = {
features: {
a: { key: 'a', version: 1 },
b: { key: 'b', version: 1 },
c: { key: 'c', version: 1 }
},
segments: {}
};
const allData1 = {
features: {
a: { key: 'a', version: 1 },
b: { key: 'b', version: 2 }
},
segments: {}
};
const emitter = new EventEmitter();
const queue = new AsyncQueue();
listenAndStoreEvents(emitter, queue, 'update');
listenAndStoreEvents(emitter, queue, 'update:a');
listenAndStoreEvents(emitter, queue, 'update:b');
listenAndStoreEvents(emitter, queue, 'update:c');
const wrapper = FeatureStoreEventWrapper(store, emitter);
await promisifySingle(wrapper.init)(allData0);
expect(await queue.take()).toEqual(['update', { key: 'a' }]);
expect(await queue.take()).toEqual(['update:a', { key: 'a' }]);
expect(await queue.take()).toEqual(['update', { key: 'b' }]);
expect(await queue.take()).toEqual(['update:b', { key: 'b' }]);
expect(await queue.take()).toEqual(['update', { key: 'c' }]);
expect(await queue.take()).toEqual(['update:c', { key: 'c' }]);
expect(queue.isEmpty()).toEqual(true);
await promisifySingle(wrapper.init)(allData1);
expect(await queue.take()).toEqual(['update', { key: 'b' }]); // b was updated to version 2
expect(await queue.take()).toEqual(['update:b', { key: 'b' }]);
expect(await queue.take()).toEqual(['update', { key: 'c' }]); // c was deleted
expect(await queue.take()).toEqual(['update:c', { key: 'c' }]);
expect(queue.isEmpty()).toEqual(true);
});
it('sends event for update', async () => {
const store = InMemoryFeatureStore();
const allData = {
features: {
a: { key: 'a', version: 1 }
},
segments: {}
};
const emitter = new EventEmitter();
const queue = new AsyncQueue();
listenAndStoreEvents(emitter, queue, 'update');
listenAndStoreEvents(emitter, queue, 'update:a');
const wrapper = FeatureStoreEventWrapper(store, emitter);
await promisifySingle(wrapper.init)(allData);
expect(await queue.take()).toEqual(['update', { key: 'a' }]);
expect(await queue.take()).toEqual(['update:a', { key: 'a' }]);
expect(queue.isEmpty()).toEqual(true);
await promisifySingle(wrapper.upsert)(dataKind.features, { key: 'a', version: 2 });
await promisifySingle(wrapper.upsert)(dataKind.features, { key: 'a', version: 2 }); // no event for this one
expect(await queue.take()).toEqual(['update', { key: 'a' }]);
expect(await queue.take()).toEqual(['update:a', { key: 'a' }]);
expect(queue.isEmpty()).toEqual(true);
});
it('sends event for delete', async () => {
const store = InMemoryFeatureStore();
const allData = {
features: {
a: { key: 'a', version: 1 }
},
segments: {}
};
const emitter = new EventEmitter();
const queue = new AsyncQueue();
listenAndStoreEvents(emitter, queue, 'update');
listenAndStoreEvents(emitter, queue, 'update:a');
const wrapper = FeatureStoreEventWrapper(store, emitter);
await promisifySingle(wrapper.init)(allData);
expect(await queue.take()).toEqual(['update', { key: 'a' }]);
expect(await queue.take()).toEqual(['update:a', { key: 'a' }]);
expect(queue.isEmpty()).toEqual(true);
await promisifySingle(wrapper.delete)(dataKind.features, 'a', 2);
expect(await queue.take()).toEqual(['update', { key: 'a' }]);
expect(await queue.take()).toEqual(['update:a', { key: 'a' }]);
expect(queue.isEmpty()).toEqual(true);
});
it('sends update events for transitive dependencies', async () => {
const store = InMemoryFeatureStore();
const allData = {
features: {
a: { key: 'a', version: 1 },
b: { key: 'b', version: 1, prerequisites: [ { key: 'c' }, { key: 'e' } ] },
c: { key: 'c', version: 1, prerequisites: [ { key: 'd' } ],
rules: [
{ clauses: [ { op: 'segmentMatch', values: [ 's0' ] } ] }
]
},
d: { key: 'd', version: 1, prerequisites: [ { key: 'e' } ] },
e: { key: 'e', version: 1 }
},
segments: {
s0: { key: 's0', version: 1 }
}
};
const emitter = new EventEmitter();
const queue = new AsyncQueue();
listenAndStoreEvents(emitter, queue, 'update');
const wrapper = FeatureStoreEventWrapper(store, emitter);
await promisifySingle(wrapper.init)(allData);
expect(await queue.take()).toEqual(['update', { key: 'a' }]);
expect(await queue.take()).toEqual(['update', { key: 'b' }]);
expect(await queue.take()).toEqual(['update', { key: 'c' }]);
expect(await queue.take()).toEqual(['update', { key: 'd' }]);
expect(await queue.take()).toEqual(['update', { key: 'e' }]);
expect(queue.isEmpty()).toEqual(true);
await promisifySingle(wrapper.upsert)(dataKind.features,
{ key: 'd', version: 2, prerequisites: [ { key: 'e' } ] });
expect(await queue.take()).toEqual(['update', { key: 'b' }]);
expect(await queue.take()).toEqual(['update', { key: 'c' }]);
expect(await queue.take()).toEqual(['update', { key: 'd' }]);
await promisifySingle(wrapper.upsert)(dataKind.segments, { key: 's0', version: 2 });
expect(await queue.take()).toEqual(['update', { key: 'b' }]);
expect(await queue.take()).toEqual(['update', { key: 'c' }]);
});
});