Skip to content

Commit 3cc428e

Browse files
prepare 3.2.10 release (#30)
1 parent 19af02f commit 3cc428e

File tree

4 files changed

+60
-55
lines changed

4 files changed

+60
-55
lines changed

package-lock.json

Lines changed: 4 additions & 51 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
"@rollup/plugin-replace": "^2.2.0",
4747
"babel-eslint": "^10.1.0",
4848
"babel-jest": "^25.1.0",
49-
"chai": "^4.1.2",
5049
"cross-env": "^5.1.4",
5150
"eslint": "^6.8.0",
5251
"eslint-config-prettier": "^2.9.0",

src/__tests__/LDClient-streaming-test.js

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as utils from '../utils';
22

3-
import { eventSink, sleepAsync, withCloseable } from 'launchdarkly-js-test-helpers';
3+
import { AsyncQueue, eventSink, sleepAsync, withCloseable } from 'launchdarkly-js-test-helpers';
44

55
import EventSource from './EventSource-mock';
66
import { respondJson } from './mockHttp';
@@ -250,6 +250,52 @@ describe('LDClient streaming', () => {
250250
});
251251
});
252252

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+
253299
it('handles stream put message by updating flags', async () => {
254300
await withClientAndServer({}, async client => {
255301
await client.waitForInitialization();

src/index.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,9 +334,16 @@ export function initialize(env, user, specifiedOptions, platform, extraOptionDef
334334
stream.connect(ident.getUser(), hash, {
335335
ping: function() {
336336
logger.debug(messages.debugStreamPing());
337+
const userAtTimeOfPingEvent = ident.getUser();
337338
requestor
338-
.fetchFlagSettings(ident.getUser(), hash)
339-
.then(requestedFlags => replaceAllFlags(requestedFlags || {}))
339+
.fetchFlagSettings(userAtTimeOfPingEvent, hash)
340+
.then(requestedFlags => {
341+
// Check whether the current user is still the same - we don't want to overwrite the flags if
342+
// the application has called identify() while this request was in progress
343+
if (utils.deepEquals(userAtTimeOfPingEvent, ident.getUser())) {
344+
replaceAllFlags(requestedFlags || {});
345+
}
346+
})
340347
.catch(err => {
341348
emitter.maybeReportError(new errors.LDFlagFetchError(messages.errorFetchingFlags(err)));
342349
});

0 commit comments

Comments
 (0)