-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor multi agents execution (#376)
* refactor(t2viz): introduced pipeline to run async operations Signed-off-by: Yulong Ruan <[email protected]> * add tests Signed-off-by: Yulong Ruan <[email protected]> * update changelog entry Signed-off-by: Yulong Ruan <[email protected]> * rename Operator -> Task Signed-off-by: Yulong Ruan <[email protected]> * cleanup console.log Signed-off-by: Yulong Ruan <[email protected]> --------- Signed-off-by: Yulong Ruan <[email protected]>
- Loading branch information
Showing
9 changed files
with
348 additions
and
186 deletions.
There are no files selected for viewing
This file contains 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 was deleted.
Oops, something went wrong.
This file contains 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 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,76 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { Pipeline } from './pipeline'; | ||
|
||
describe('pipeline', () => { | ||
it('should run pipeline', (done) => { | ||
const fn1 = jest.fn().mockImplementation((input) => { | ||
return Promise.resolve(Array<string>().concat(input).concat('fn1')); | ||
}); | ||
const fn2 = jest.fn().mockImplementation((input) => { | ||
return Promise.resolve(Array<string>().concat(input).concat('fn2')); | ||
}); | ||
const pipeline = new Pipeline([{ execute: fn1 }, { execute: fn2 }]); | ||
pipeline.getResult$().subscribe((result) => { | ||
expect(result).toEqual(['input', 'fn1', 'fn2']); | ||
expect(pipeline.status$.value).toBe('STOPPED'); | ||
done(); | ||
}); | ||
|
||
expect(pipeline.status$.value).toBe('STOPPED'); | ||
pipeline.run('input'); | ||
expect(pipeline.status$.value).toBe('RUNNING'); | ||
}); | ||
|
||
it('should run pipeline with the latest input', (done) => { | ||
const fn1 = jest.fn().mockImplementation((input) => { | ||
return Promise.resolve(Array<string>().concat(input).concat('fn1')); | ||
}); | ||
const fn2 = jest.fn().mockImplementation((input) => { | ||
return Promise.resolve(Array<string>().concat(input).concat('fn2')); | ||
}); | ||
const pipeline = new Pipeline([{ execute: fn1 }, { execute: fn2 }]); | ||
pipeline.getResult$().subscribe((result) => { | ||
expect(result).toEqual(['input2', 'fn1', 'fn2']); | ||
expect(fn1).toHaveBeenCalledTimes(2); | ||
// The fn2 should only be called onece because the first pipeline run should already be canceled | ||
expect(fn2).toHaveBeenCalledTimes(1); | ||
done(); | ||
}); | ||
// the pipeline run twice with different inputs | ||
// the second pipeline.run should be make it to cancel the first pipeline run | ||
pipeline.run('input1'); | ||
pipeline.run('input2'); | ||
}); | ||
|
||
it('should run pipeline once synchronously', async () => { | ||
const fn1 = jest.fn().mockImplementation((input) => { | ||
return Promise.resolve(Array<string>().concat(input).concat('fn1')); | ||
}); | ||
const fn2 = jest.fn().mockImplementation((input) => { | ||
return Promise.resolve(Array<string>().concat(input).concat('fn2')); | ||
}); | ||
const pipeline = new Pipeline([{ execute: fn1 }, { execute: fn2 }]); | ||
const result = await pipeline.runOnce('input'); | ||
expect(result).toEqual(['input', 'fn1', 'fn2']); | ||
}); | ||
|
||
it('should catch error', (done) => { | ||
const fn1 = jest.fn().mockImplementation((input) => { | ||
return Promise.resolve(Array<string>().concat(input).concat('fn1')); | ||
}); | ||
const fn2 = jest.fn().mockImplementation(() => { | ||
throw new Error('test'); | ||
}); | ||
|
||
const pipeline = new Pipeline([{ execute: fn1 }, { execute: fn2 }]); | ||
pipeline.getResult$().subscribe((result) => { | ||
expect(result).toEqual({ error: new Error('test') }); | ||
done(); | ||
}); | ||
pipeline.run('input'); | ||
}); | ||
}); |
Oops, something went wrong.