-
Notifications
You must be signed in to change notification settings - Fork 31
#3 Implement additional LDClient tests. #36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
44 commits
Select commit
Hold shift + click to select a range
4b51907
Lint
kinyoklion f208ff4
Write unit tests for file data source
kinyoklion ca829ca
Remove docs folder.
kinyoklion 67fdde3
Merge main.
kinyoklion f0a5080
Merge branch 'main' into rlamb/sc-154357/file-data-source
kinyoklion 8daa0bb
Start adding top level client tests.
kinyoklion 7835b32
Progress on tests.
kinyoklion 1b96fad
Start implementation of LDClientContext.
kinyoklion 171ebd4
Merge branch 'rlamb/sc-154357/file-data-source' into rlamb/sc-162616/…
kinyoklion 18590c4
Change file data source factory to be LDClientContext based.
kinyoklion bbc4b9f
Fix open handles in tests.
kinyoklion 5986d0b
Update organization to allow for LDDataSourceUpdates to be used with …
kinyoklion d8b9565
First pass DataSourceUpdates.
kinyoklion 5bfc3da
Add DataSourceUpdates tests.
kinyoklion b4b69ce
Add client level individual eval tests.
kinyoklion 01ca674
Progress on events.
kinyoklion 32e6eba
Remove upsert clone because we need to retain prototypes.
kinyoklion 8c5ee0b
Add more events tests.
kinyoklion 516df56
Start big segments tests.
kinyoklion d427d1c
Finish big segments test. Linting.
kinyoklion 2164dc3
Ensure we close the big segments manager. Updated tests to prevent op…
kinyoklion 57c6a9f
Remove commented jest spy.
kinyoklion c08491b
Linting cleanup.
kinyoklion 76ec9a3
File data source tests that use the node filesystem implementation.
kinyoklion de3313b
Add test for node level big segments. Most things are covered by lowe…
kinyoklion 4d23a6d
Fix TLS parsing.
kinyoklion 7c38752
Add TLS tests.
kinyoklion f929aee
Lint
kinyoklion 8618392
Move tests to match directory structure. Add tests for the header wra…
kinyoklion 3f09fbc
Merge main.
kinyoklion ea0d8a2
Fixes issues from end to end testing.
kinyoklion 8f25e12
merge main
kinyoklion 454311b
Merge fixes branch.
kinyoklion eeb064e
Remove the logging destination from node level tests.
kinyoklion 94a25e2
Merge main,
kinyoklion 22661fe
Diagnostic events error handling.
kinyoklion 39a0eea
Merge branch 'rlamb/diagnostic-event-error-handling' into rlamb/fixes…
kinyoklion f199f0a
Merge branch 'rlamb/fixes-from-testing' into rlamb/sc-162616/implemen…
kinyoklion 53707d4
Add missing describe name.
kinyoklion 745ea4b
Linting
kinyoklion c005500
Change callbacks to be an object for LDClientImpl.
kinyoklion 9a2b10e
Merge
kinyoklion 86ad7d3
Linting
kinyoklion 2720d7a
Merge main.
kinyoklion File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,4 +3,5 @@ dist/ | |
| node_modules/ | ||
| **/*.tsbuildinfo | ||
| coverage/ | ||
| tmp/ | ||
| docs/ | ||
153 changes: 153 additions & 0 deletions
153
platform-node/__tests__/LDClientNode.bigSegments.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| import { | ||
| integrations, interfaces, LDBigSegmentsOptions, LDLogger, | ||
| } from '@launchdarkly/js-server-sdk-common'; | ||
| import { basicLogger, LDClientImpl } from '../src'; | ||
| import { LDClient } from '../src/api/LDClient'; | ||
|
|
||
| describe('given test data with big segments', () => { | ||
| // To use the public interfaces to create a client which doesn't use the | ||
| // network. (Versus being offline, or a null update processor.) | ||
| let td: integrations.TestData; | ||
| let logger: LDLogger; | ||
|
|
||
| beforeEach(() => { | ||
| td = new integrations.TestData(); | ||
| logger = basicLogger({ | ||
| destination: () => {}, | ||
| }); | ||
| }); | ||
|
|
||
| describe('given a healthy big segment store', () => { | ||
| let client: LDClient; | ||
| const bigSegmentsConfig: LDBigSegmentsOptions = { | ||
| statusPollInterval: 0.1, | ||
| store(): interfaces.BigSegmentStore { | ||
| return { | ||
| getMetadata: async () => ({ lastUpToDate: Date.now() }), | ||
| getUserMembership: async () => undefined, | ||
| close: () => { }, | ||
| }; | ||
| }, | ||
| }; | ||
|
|
||
| beforeEach(() => { | ||
| client = new LDClientImpl('sdk-key', { | ||
| updateProcessor: td.getFactory(), | ||
| sendEvents: false, | ||
| bigSegments: bigSegmentsConfig, | ||
| }); | ||
| }); | ||
|
|
||
| it('can get status', () => { | ||
| const status = client.bigSegmentStoreStatusProvider.getStatus(); | ||
| expect(status).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('can require status', async () => { | ||
| const status = await client.bigSegmentStoreStatusProvider.requireStatus(); | ||
| expect(status.available).toEqual(true); | ||
| expect(status.stale).toEqual(false); | ||
| }); | ||
|
|
||
| it('Can listen to the event emitter for the status', (done) => { | ||
| client.bigSegmentStoreStatusProvider.on('change', (status: interfaces.BigSegmentStoreStatus) => { | ||
| expect(status.stale).toEqual(false); | ||
| expect(status.available).toEqual(true); | ||
|
|
||
| const status2 = client.bigSegmentStoreStatusProvider.getStatus(); | ||
| expect(status2!.stale).toEqual(false); | ||
| expect(status2!.available).toEqual(true); | ||
| done(); | ||
| }); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| client.close(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('given a stale store', () => { | ||
| let client: LDClient; | ||
| const bigSegmentsConfig: LDBigSegmentsOptions = { | ||
| store(): interfaces.BigSegmentStore { | ||
| return { | ||
| getMetadata: async () => ({ lastUpToDate: 1000 }), | ||
| getUserMembership: async () => undefined, | ||
| close: () => { }, | ||
| }; | ||
| }, | ||
| }; | ||
|
|
||
| beforeEach(async () => { | ||
| client = new LDClientImpl('sdk-key', { | ||
| updateProcessor: td.getFactory(), | ||
| sendEvents: false, | ||
| bigSegments: bigSegmentsConfig, | ||
| }); | ||
|
|
||
| await client.waitForInitialization(); | ||
| }); | ||
|
|
||
| it('can require status', async () => { | ||
| const status = await client.bigSegmentStoreStatusProvider.requireStatus(); | ||
| expect(status.available).toEqual(true); | ||
| expect(status.stale).toEqual(true); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| client.close(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('given a store that can produce an error', () => { | ||
| let client: LDClient; | ||
| let error: boolean; | ||
| const bigSegmentsConfig: LDBigSegmentsOptions = { | ||
| statusPollInterval: 0.1, | ||
| store(): interfaces.BigSegmentStore { | ||
| return { | ||
| getMetadata: async () => { | ||
| if (error) { | ||
| throw new Error('sorry'); | ||
| } | ||
| return { lastUpToDate: Date.now() }; | ||
| }, | ||
| getUserMembership: async () => undefined, | ||
| close: () => { }, | ||
| }; | ||
| }, | ||
| }; | ||
|
|
||
| beforeEach(async () => { | ||
| error = false; | ||
| client = new LDClientImpl('sdk-key', { | ||
| updateProcessor: td.getFactory(), | ||
| sendEvents: false, | ||
| bigSegments: bigSegmentsConfig, | ||
| logger, | ||
| }); | ||
|
|
||
| await client.waitForInitialization(); | ||
| }); | ||
|
|
||
| it('Can observe the status change', (done) => { | ||
| let message = 0; | ||
| client.bigSegmentStoreStatusProvider.on('change', (status: interfaces.BigSegmentStoreStatus) => { | ||
| if (message === 0) { | ||
| expect(status.stale).toEqual(false); | ||
| expect(status.available).toEqual(true); | ||
| error = true; | ||
| message += 1; | ||
| } else { | ||
| expect(status.stale).toEqual(false); | ||
| expect(status.available).toEqual(false); | ||
| done(); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| client.close(); | ||
| }); | ||
| }); | ||
| }); |
164 changes: 164 additions & 0 deletions
164
platform-node/__tests__/LDClientNode.fileDataSource.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| import { integrations } from '@launchdarkly/js-server-sdk-common'; | ||
| import * as fs from 'node:fs'; | ||
| import LDClientNode from '../src/LDClientNode'; | ||
|
|
||
| const flag1Key = 'flag1'; | ||
| const flag2Key = 'flag2'; | ||
| const flag2Value = 'value2'; | ||
| const segment1Key = 'seg1'; | ||
|
|
||
| const flag1 = { | ||
| key: flag1Key, | ||
| on: true, | ||
| rules: [ | ||
| { clauses: [{ op: 'segmentMatch', values: [segment1Key] }], variation: 1 }, | ||
| ], | ||
| fallthrough: { | ||
| variation: 2, | ||
| }, | ||
| variations: ['fall', 'off', 'on'], | ||
| }; | ||
|
|
||
| const segment1 = { | ||
| key: segment1Key, | ||
| included: ['user1'], | ||
| }; | ||
|
|
||
| const flagOnlyJson = ` | ||
| { | ||
| "flags": { | ||
| "${flag1Key}": ${JSON.stringify(flag1)} | ||
| } | ||
| }`; | ||
|
|
||
| const segmentOnlyJson = ` | ||
| { | ||
| "segments": { | ||
| "${segment1Key}": ${JSON.stringify(segment1)} | ||
| } | ||
| }`; | ||
|
|
||
| const allPropertiesJson = ` | ||
| { | ||
| "flags": { | ||
| "${flag1Key}": ${JSON.stringify(flag1)} | ||
| }, | ||
| "flagValues": { | ||
| "${flag2Key}": "${flag2Value}" | ||
| }, | ||
| "segments": { | ||
| "${segment1Key}": ${JSON.stringify(segment1)} | ||
| } | ||
| }`; | ||
|
|
||
| const tmpFiles: string[] = []; | ||
|
|
||
| function makeTempFile(content: string): string { | ||
| const fileName = (Math.random() + 1).toString(36).substring(7); | ||
| if (!fs.existsSync('./tmp')) { | ||
| fs.mkdirSync('./tmp'); | ||
| } | ||
| const fullPath = `./tmp/${fileName}`; | ||
| fs.writeFileSync(fullPath, content); | ||
| tmpFiles.push(fullPath); | ||
| return fullPath; | ||
| } | ||
|
|
||
| function replaceFileContent(filePath: string, content: string) { | ||
| fs.writeFileSync(filePath, content); | ||
| } | ||
|
|
||
| describe('When using a file data source', () => { | ||
| afterAll(() => { | ||
| tmpFiles.forEach((filePath) => { | ||
| fs.unlinkSync(filePath); | ||
| }); | ||
| }); | ||
|
|
||
| it('loads flags on start from JSON', async () => { | ||
| const path = makeTempFile(allPropertiesJson); | ||
| const fds = new integrations.FileDataSourceFactory({ | ||
| paths: [path], | ||
| }); | ||
|
|
||
| const client = new LDClientNode('sdk-key', { | ||
| updateProcessor: fds.getFactory(), | ||
| sendEvents: false, | ||
| }); | ||
|
|
||
| await client.waitForInitialization(); | ||
|
|
||
| const f1Var = await client.variation(flag1Key, { key: 'user1' }, 'default'); | ||
| expect(f1Var).toEqual('off'); | ||
| const f1VarNoSeg = await client.variation(flag1Key, { key: 'user2' }, 'default'); | ||
| expect(f1VarNoSeg).toEqual('on'); | ||
| const f2Var = await client.variation(flag2Key, { key: 'user1' }, 'default'); | ||
| expect(f2Var).toEqual('value2'); | ||
|
|
||
| client.close(); | ||
| }); | ||
|
|
||
| it('it can load multiple files', async () => { | ||
| const path1 = makeTempFile(flagOnlyJson); | ||
| const path2 = makeTempFile(segmentOnlyJson); | ||
| const fds = new integrations.FileDataSourceFactory({ | ||
| paths: [path1, path2], | ||
| }); | ||
|
|
||
| const client = new LDClientNode('sdk-key', { | ||
| updateProcessor: fds.getFactory(), | ||
| sendEvents: false, | ||
| }); | ||
|
|
||
| await client.waitForInitialization(); | ||
|
|
||
| const f1Var = await client.variation(flag1Key, { key: 'user1' }, 'default'); | ||
| expect(f1Var).toEqual('off'); | ||
| const f1VarNoSeg = await client.variation(flag1Key, { key: 'user2' }, 'default'); | ||
| expect(f1VarNoSeg).toEqual('on'); | ||
|
|
||
| client.close(); | ||
| }); | ||
|
|
||
| it('reloads the file if the content changes', async () => { | ||
| const path = makeTempFile(allPropertiesJson); | ||
| const fds = new integrations.FileDataSourceFactory({ | ||
| paths: [path], | ||
| autoUpdate: true, | ||
| }); | ||
|
|
||
| const client = new LDClientNode('sdk-key', { | ||
| updateProcessor: fds.getFactory(), | ||
| sendEvents: false, | ||
| }); | ||
|
|
||
| await client.waitForInitialization(); | ||
|
|
||
| const f1Var = await client.variation(flag1Key, { key: 'user1' }, 'default'); | ||
| expect(f1Var).toEqual('off'); | ||
| const f1VarNoSeg = await client.variation(flag1Key, { key: 'user2' }, 'default'); | ||
| expect(f1VarNoSeg).toEqual('on'); | ||
| const f2Var = await client.variation(flag2Key, { key: 'user1' }, 'default'); | ||
| expect(f2Var).toEqual('value2'); | ||
|
|
||
| replaceFileContent(path, flagOnlyJson); | ||
|
|
||
| await new Promise<void>((resolve) => { | ||
| client.once('update', () => { | ||
| // After the file reloads we get changes, so we know we can move onto | ||
| // evaluation. | ||
| resolve(); | ||
| }); | ||
| replaceFileContent(path, flagOnlyJson); | ||
| }); | ||
|
|
||
| const f1VarB = await client.variation(flag1Key, { key: 'user1' }, 'default'); | ||
| expect(f1VarB).toEqual('on'); // Segment doesn't exist anymore. | ||
| const f1VarNoSegB = await client.variation(flag1Key, { key: 'user2' }, 'default'); | ||
| expect(f1VarNoSegB).toEqual('on'); | ||
| const f2VarB = await client.variation(flag2Key, { key: 'user1' }, 'default'); | ||
| expect(f2VarB).toEqual('default'); | ||
|
|
||
| client.close(); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Most of the file data source testing is at the common level, but here we make sure it works with the actual node filesystem APIs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it's just the I/O implementation that's distinctive here, I feel like you might be able to make the tests a bit simpler by omitting things like the segment. I mean, the fact that there's a flag referencing a segment is a detail of the data that's in the file, which should be orthogonal to how we read the file or detected changes to the file.