|
1 | 1 | import * as utils from '../utils'; |
2 | 2 |
|
3 | | -import { eventSink, sleepAsync, withCloseable } from 'launchdarkly-js-test-helpers'; |
| 3 | +import { AsyncQueue, eventSink, sleepAsync, withCloseable } from 'launchdarkly-js-test-helpers'; |
4 | 4 |
|
5 | 5 | import EventSource from './EventSource-mock'; |
6 | 6 | import { respondJson } from './mockHttp'; |
@@ -250,6 +250,52 @@ describe('LDClient streaming', () => { |
250 | 250 | }); |
251 | 251 | }); |
252 | 252 |
|
| 253 | + it("poll request triggered by stream ping can't overwrite another user's flags", async () => { |
| 254 | + const otherUser = { key: 'otherUser' }; |
| 255 | + const initUserBase64 = utils.base64URLEncode(JSON.stringify(user)); |
| 256 | + const otherUserBase64 = utils.base64URLEncode(JSON.stringify(otherUser)); |
| 257 | + |
| 258 | + await withClientAndServer({}, async (client, server) => { |
| 259 | + const reqRespQueue = new AsyncQueue(); |
| 260 | + server.byDefault((req, resp) => { |
| 261 | + reqRespQueue.add({ req: req, resp: resp }); |
| 262 | + }); |
| 263 | + |
| 264 | + const initPromise = client.waitForInitialization(); |
| 265 | + const poll1 = await reqRespQueue.take(); |
| 266 | + expect(poll1.req.path).toContain(initUserBase64); |
| 267 | + respondJson({ flagKey: { value: 1 } })(poll1.req, poll1.resp); |
| 268 | + await initPromise; |
| 269 | + |
| 270 | + // The flag value is now 1, from the initial poll |
| 271 | + expect(client.variation('flagKey')).toEqual(1); |
| 272 | + |
| 273 | + client.setStreaming(true); |
| 274 | + const stream = await expectStreamConnecting(fullStreamUrlWithUser); |
| 275 | + |
| 276 | + stream.eventSource.mockEmit('ping'); |
| 277 | + const poll2 = await reqRespQueue.take(); |
| 278 | + // poll2 is the poll request that was triggered by the ping; don't respond to it yet |
| 279 | + expect(poll2.req.path).toContain(initUserBase64); |
| 280 | + |
| 281 | + const identifyPromise = client.identify(otherUser); |
| 282 | + const poll3 = await reqRespQueue.take(); |
| 283 | + // poll3 is the poll request for the identify |
| 284 | + expect(poll3.req.path).toContain(otherUserBase64); |
| 285 | + |
| 286 | + // Now let's say poll3 completes first, setting the flag value to 3 for the new user |
| 287 | + respondJson({ flagKey: { value: 3 } })(poll3.req, poll3.resp); |
| 288 | + |
| 289 | + // And then poll2, which was for the previous user, completes with a flag value of 2 |
| 290 | + respondJson({ flagKey: { value: 2 } })(poll2.req, poll2.resp); |
| 291 | + |
| 292 | + await identifyPromise; |
| 293 | + |
| 294 | + // The flag value should now be 3, not 2 |
| 295 | + expect(client.variation('flagKey')).toEqual(3); |
| 296 | + }); |
| 297 | + }); |
| 298 | + |
253 | 299 | it('handles stream put message by updating flags', async () => { |
254 | 300 | await withClientAndServer({}, async client => { |
255 | 301 | await client.waitForInitialization(); |
|
0 commit comments