From 9a3a0cf0448408d76c9dd11a3c3f01d2515ddbcf Mon Sep 17 00:00:00 2001 From: kwasniew Date: Wed, 18 Oct 2023 14:54:52 +0200 Subject: [PATCH 1/3] feat: default session id in frontend api --- src/lib/services/proxy-service.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/lib/services/proxy-service.ts b/src/lib/services/proxy-service.ts index b9e9086fbda6..324b1de210a0 100644 --- a/src/lib/services/proxy-service.ts +++ b/src/lib/services/proxy-service.ts @@ -74,12 +74,19 @@ export class ProxyService { const client = await this.clientForProxyToken(token); const definitions = client.getFeatureToggleDefinitions() || []; + const sessionId = context.sessionId || String(Math.random()); + return definitions - .filter((feature) => client.isEnabled(feature.name, context)) + .filter((feature) => + client.isEnabled(feature.name, { ...context, sessionId }), + ) .map((feature) => ({ name: feature.name, enabled: Boolean(feature.enabled), - variant: client.forceGetVariant(feature.name, context), + variant: client.getVariant(feature.name, { + ...context, + sessionId, + }), impressionData: Boolean(feature.impressionData), })); } From ea3381be68603361d739564352a6f43c110522c8 Mon Sep 17 00:00:00 2001 From: kwasniew Date: Wed, 18 Oct 2023 16:09:36 +0200 Subject: [PATCH 2/3] test: verify consistent stickiness with poor man's property test --- src/test/e2e/api/proxy/proxy.e2e.test.ts | 39 ++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/test/e2e/api/proxy/proxy.e2e.test.ts b/src/test/e2e/api/proxy/proxy.e2e.test.ts index 1f9c8955e332..34deee7daa0e 100644 --- a/src/test/e2e/api/proxy/proxy.e2e.test.ts +++ b/src/test/e2e/api/proxy/proxy.e2e.test.ts @@ -1236,3 +1236,42 @@ test('should return 204 if metrics are disabled', async () => { }) .expect(204); }); + +test('should resolve variable rollout percentage consistently', async () => { + const frontendToken = await createApiToken(ApiTokenType.FRONTEND); + await createFeatureToggle({ + name: 'randomFeature', + enabled: true, + strategies: [ + { + name: 'flexibleRollout', + constraints: [], + parameters: { + rollout: '50', + stickiness: 'default', + groupId: 'some-new', + }, + variants: [ + { + name: 'a', + stickiness: 'default', + weightType: 'variable', + weight: 1000, + }, + ], + }, + ], + }); + + for (let i = 0; i < 10; ++i) { + const { body } = await app.request + .get('/api/frontend') + .set('Authorization', frontendToken.secret) + .expect('Content-Type', /json/) + .expect(200); + + if (body.toggles.length > 0) { + expect(body.toggles[0].variant.name).toBe('a'); + } + } +}); From aa99b324424a980ca9734552904b365acdf08adc Mon Sep 17 00:00:00 2001 From: kwasniew Date: Wed, 18 Oct 2023 16:10:50 +0200 Subject: [PATCH 3/3] test: verify consistent stickiness with poor man's property test --- src/test/e2e/api/proxy/proxy.e2e.test.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/e2e/api/proxy/proxy.e2e.test.ts b/src/test/e2e/api/proxy/proxy.e2e.test.ts index 34deee7daa0e..531ce811157c 100644 --- a/src/test/e2e/api/proxy/proxy.e2e.test.ts +++ b/src/test/e2e/api/proxy/proxy.e2e.test.ts @@ -1271,6 +1271,7 @@ test('should resolve variable rollout percentage consistently', async () => { .expect(200); if (body.toggles.length > 0) { + // disabled variant should not be possible for enabled toggles expect(body.toggles[0].variant.name).toBe('a'); } }