-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: ryjiang <[email protected]>
- Loading branch information
1 parent
791ee74
commit fcf36ff
Showing
2 changed files
with
39 additions
and
45 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 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 |
---|---|---|
@@ -1,56 +1,49 @@ | ||
import { promisify } from '../../milvus'; | ||
import { promisify } from '../../milvus/utils'; | ||
|
||
describe(`utils/function`, () => { | ||
it('should resolve with the result of the target function call', async () => { | ||
const obj = { | ||
target: (params: any, options: any, callback: any) => { | ||
callback(null, 'result'); | ||
}, | ||
describe('promisify', () => { | ||
let pool: any; | ||
let client: any; | ||
|
||
beforeEach(() => { | ||
client = { | ||
testFunction: jest.fn((params, options, callback) => | ||
callback(null, 'success') | ||
), | ||
}; | ||
pool = { | ||
acquire: jest.fn().mockResolvedValue(client), | ||
release: jest.fn(), | ||
}; | ||
const target = 'target'; | ||
const params = {}; | ||
const timeout = 1000; | ||
const result = await promisify(obj, target, params, timeout); | ||
expect(result).toEqual('result'); | ||
}); | ||
|
||
it('should reject with the error if there was an error', async () => { | ||
const obj = { | ||
target: (params: any, options: any, callback: any) => { | ||
callback(new Error('error')); | ||
}, | ||
}; | ||
const target = 'target'; | ||
const params = {}; | ||
const timeout = 1000; | ||
await expect(promisify(obj, target, params, timeout)).rejects.toThrow( | ||
it('should resolve with the result of the function call', async () => { | ||
const result = await promisify(pool, 'testFunction', {}, 1000); | ||
expect(result).toBe('success'); | ||
expect(client.testFunction).toHaveBeenCalled(); | ||
expect(pool.acquire).toHaveBeenCalled(); | ||
expect(pool.release).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should reject if the function call results in an error', async () => { | ||
client.testFunction = jest.fn((params, options, callback) => | ||
callback('error') | ||
); | ||
await expect(promisify(pool, 'testFunction', {}, 1000)).rejects.toBe( | ||
'error' | ||
); | ||
expect(client.testFunction).toHaveBeenCalled(); | ||
expect(pool.acquire).toHaveBeenCalled(); | ||
expect(pool.release).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should reject with the error if there was an exception', async () => { | ||
const obj = { | ||
target: () => { | ||
throw new Error('exception'); | ||
}, | ||
}; | ||
const target = 'target'; | ||
const params = {}; | ||
const timeout = 1000; | ||
await expect(promisify(obj, target, params, timeout)).rejects.toThrow( | ||
it('should reject if the function call throws an exception', async () => { | ||
client.testFunction = jest.fn(() => { | ||
throw new Error('exception'); | ||
}); | ||
await expect(promisify(pool, 'testFunction', {}, 1000)).rejects.toThrow( | ||
'exception' | ||
); | ||
}); | ||
|
||
it('should use the default timeout if no timeout is provided', async () => { | ||
const obj = { | ||
target: (params: any, options: any, callback: any) => { | ||
callback(null, 'result'); | ||
}, | ||
}; | ||
const target = 'target'; | ||
const params = {}; | ||
const result = await promisify(obj, target, params, 0); | ||
expect(result).toEqual('result'); | ||
expect(pool.acquire).toHaveBeenCalled(); | ||
expect(pool.release).toHaveBeenCalled(); | ||
}); | ||
}); |