-
Notifications
You must be signed in to change notification settings - Fork 83
[FSSDK-11898] serialize concurrent cmab service calls #1086
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
2 commits
Select commit
Hold shift + click to select a range
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
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
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,143 @@ | ||
/** | ||
* Copyright 2025, Optimizely | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import { describe, it, expect, beforeEach } from 'vitest'; | ||
|
||
import { SerialRunner } from './serial_runner'; | ||
import { resolvablePromise } from '../promise/resolvablePromise'; | ||
import { exhaustMicrotasks } from '../../tests/testUtils'; | ||
|
||
describe('SerialRunner', () => { | ||
let serialRunner: SerialRunner; | ||
|
||
beforeEach(() => { | ||
serialRunner = new SerialRunner(); | ||
}); | ||
|
||
it('should return result from a single async function', async () => { | ||
const fn = () => Promise.resolve('result'); | ||
|
||
const result = await serialRunner.run(fn); | ||
|
||
expect(result).toBe('result'); | ||
}); | ||
|
||
it('should reject with same error when the passed function rejects', async () => { | ||
const error = new Error('test error'); | ||
const fn = () => Promise.reject(error); | ||
|
||
await expect(serialRunner.run(fn)).rejects.toThrow(error); | ||
}); | ||
|
||
it('should execute multiple async functions in order', async () => { | ||
const executionOrder: number[] = []; | ||
const promises = [resolvablePromise(), resolvablePromise(), resolvablePromise()]; | ||
|
||
const createTask = (id: number) => async () => { | ||
executionOrder.push(id); | ||
await promises[id]; | ||
return id; | ||
}; | ||
|
||
const results = [serialRunner.run(createTask(0)), serialRunner.run(createTask(1)), serialRunner.run(createTask(2))]; | ||
|
||
// only first task should have started | ||
await exhaustMicrotasks(); | ||
expect(executionOrder).toEqual([0]); | ||
|
||
// Resolve first task - second should start | ||
promises[0].resolve(''); | ||
await exhaustMicrotasks(); | ||
expect(executionOrder).toEqual([0, 1]); | ||
|
||
// Resolve second task - third should start | ||
promises[1].resolve(''); | ||
await exhaustMicrotasks(); | ||
expect(executionOrder).toEqual([0, 1, 2]); | ||
|
||
// Resolve third task - all done | ||
promises[2].resolve(''); | ||
|
||
// Verify all results are correct | ||
expect(await results[0]).toBe(0); | ||
expect(await results[1]).toBe(1); | ||
expect(await results[2]).toBe(2); | ||
}); | ||
|
||
it('should continue execution even if one function throws an error', async () => { | ||
const executionOrder: number[] = []; | ||
const promises = [resolvablePromise(), resolvablePromise(), resolvablePromise()]; | ||
|
||
const createTask = (id: number) => async () => { | ||
executionOrder.push(id); | ||
await promises[id]; | ||
return id; | ||
}; | ||
|
||
const results = [serialRunner.run(createTask(0)), serialRunner.run(createTask(1)), serialRunner.run(createTask(2))]; | ||
|
||
// only first task should have started | ||
await exhaustMicrotasks(); | ||
expect(executionOrder).toEqual([0]); | ||
|
||
// reject first task - second should still start | ||
promises[0].reject(new Error('first error')); | ||
await exhaustMicrotasks(); | ||
expect(executionOrder).toEqual([0, 1]); | ||
|
||
// reject second task - third should still start | ||
promises[1].reject(new Error('second error')); | ||
await exhaustMicrotasks(); | ||
expect(executionOrder).toEqual([0, 1, 2]); | ||
|
||
// Resolve third task - all done | ||
promises[2].resolve(''); | ||
|
||
// Verify results - first and third succeed, second fails | ||
await expect(results[0]).rejects.toThrow('first error'); | ||
await expect(results[1]).rejects.toThrow('second error'); | ||
await expect(results[2]).resolves.toBe(2); | ||
}); | ||
|
||
it('should handle functions that return different types', async () => { | ||
const numberFn = () => Promise.resolve(42); | ||
const stringFn = () => Promise.resolve('hello'); | ||
const objectFn = () => Promise.resolve({ key: 'value' }); | ||
const arrayFn = () => Promise.resolve([1, 2, 3]); | ||
const booleanFn = () => Promise.resolve(true); | ||
const nullFn = () => Promise.resolve(null); | ||
const undefinedFn = () => Promise.resolve(undefined); | ||
|
||
const results = await Promise.all([ | ||
serialRunner.run(numberFn), | ||
serialRunner.run(stringFn), | ||
serialRunner.run(objectFn), | ||
serialRunner.run(arrayFn), | ||
serialRunner.run(booleanFn), | ||
serialRunner.run(nullFn), | ||
serialRunner.run(undefinedFn), | ||
]); | ||
|
||
expect(results).toEqual([42, 'hello', { key: 'value' }, [1, 2, 3], true, null, undefined]); | ||
}); | ||
|
||
it('should handle empty function that returns undefined', async () => { | ||
const emptyFn = () => Promise.resolve(undefined); | ||
|
||
const result = await serialRunner.run(emptyFn); | ||
|
||
expect(result).toBeUndefined(); | ||
}); | ||
}); |
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,36 @@ | ||
/** | ||
* Copyright 2025, Optimizely | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { AsyncProducer } from "../type"; | ||
|
||
class SerialRunner { | ||
private waitPromise: Promise<unknown> = Promise.resolve(); | ||
|
||
// each call to serialize adds a new function to the end of the promise chain | ||
// the function is called when the previous promise resolves | ||
// if the function throws, the error is caught and ignored to allow the chain to continue | ||
// the result of the function is returned as a promise | ||
// if multiple calls to serialize are made, they will be executed in order | ||
// even if some of them throw errors | ||
|
||
run<T>(fn: AsyncProducer<T>): Promise<T> { | ||
const resultPromise = this.waitPromise.then(fn); | ||
this.waitPromise = resultPromise.catch(() => {}); | ||
return resultPromise; | ||
} | ||
} | ||
|
||
export { SerialRunner }; |
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.
Uh oh!
There was an error while loading. Please reload this page.