-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RSDK-2379 Add getStream to stream api (#141)
- Loading branch information
1 parent
00d36f4
commit 49bc38d
Showing
10 changed files
with
153 additions
and
53 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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,22 +1,113 @@ | ||
// @vitest-environment happy-dom | ||
|
||
import { describe, expect, test } from 'vitest'; | ||
import { vi, beforeEach, afterEach, describe, expect, test } from 'vitest'; | ||
import { RobotClient } from '../../robot'; | ||
vi.mock('../../robot/client'); | ||
|
||
import { events } from '../../events'; | ||
import { StreamServiceClient } from '../../gen/proto/stream/v1/stream_pb_service'; | ||
vi.mock('../../gen/proto/stream/v1/stream_pb_service'); | ||
|
||
import { StreamClient } from './client'; | ||
|
||
let streamClient: StreamClient; | ||
|
||
describe('StreamClient', () => { | ||
beforeEach(() => { | ||
vi.useFakeTimers(); | ||
|
||
const fakehost = 'fakehost'; | ||
RobotClient.prototype.createServiceClient = vi | ||
.fn() | ||
.mockImplementation(() => new StreamServiceClient(fakehost)); | ||
|
||
const robotClient = new RobotClient(fakehost); | ||
streamClient = new StreamClient(robotClient); | ||
}); | ||
|
||
afterEach(() => { | ||
vi.useRealTimers(); | ||
}); | ||
|
||
test('webrtc track will cause the client to emit an event', () => | ||
new Promise<void>((done) => { | ||
const host = 'fakeServiceHost'; | ||
const client = new RobotClient(host); | ||
const streamClient = new StreamClient(client); | ||
|
||
streamClient.on('track', (data) => { | ||
expect((data as { mock: true }).mock).eq(true); | ||
done(); | ||
}); | ||
|
||
events.emit('track', { mock: true }); | ||
})); | ||
|
||
test('getStream creates and returns a new stream', async () => { | ||
const fakeCamName = 'fakecam'; | ||
const fakeStream = { id: fakeCamName }; | ||
StreamServiceClient.prototype.addStream = vi | ||
.fn() | ||
.mockImplementation((_req, _md, cb) => { | ||
cb(null, {}); | ||
streamClient.emit('track', { streams: [fakeStream] }); | ||
}); | ||
|
||
const addStream = vi.spyOn(streamClient, 'add'); | ||
await expect(streamClient.getStream(fakeCamName)).resolves.toStrictEqual( | ||
fakeStream | ||
); | ||
expect(addStream).toHaveBeenCalledOnce(); | ||
expect(addStream).toHaveBeenCalledWith(fakeCamName); | ||
}); | ||
|
||
test('getStream fails when add stream fails', async () => { | ||
const fakeCamName = 'fakecam'; | ||
const error = new Error('could not add stream'); | ||
StreamServiceClient.prototype.addStream = vi | ||
.fn() | ||
.mockImplementation((_req, _md, cb) => { | ||
cb(error); | ||
}); | ||
|
||
const addStream = vi.spyOn(streamClient, 'add'); | ||
await expect(streamClient.getStream(fakeCamName)).rejects.toThrow(error); | ||
expect(addStream).toHaveBeenCalledOnce(); | ||
expect(addStream).toHaveBeenCalledWith(fakeCamName); | ||
}); | ||
|
||
test('getStream fails when timeout exceeded', async () => { | ||
const fakeCamName = 'fakecam'; | ||
StreamServiceClient.prototype.addStream = vi | ||
.fn() | ||
.mockImplementation((_req, _md, cb) => { | ||
cb(null, {}); | ||
}); | ||
|
||
const addStream = vi.spyOn(streamClient, 'add'); | ||
const promise = streamClient.getStream(fakeCamName); | ||
vi.runAllTimers(); | ||
await expect(promise).rejects.toThrowError( | ||
'Did not receive a stream after 5000 ms' | ||
); | ||
expect(addStream).toHaveBeenCalledOnce(); | ||
expect(addStream).toHaveBeenCalledWith(fakeCamName); | ||
}); | ||
|
||
test('getStream can add the same stream twice', async () => { | ||
const fakeCamName = 'fakecam'; | ||
const fakeStream = { id: fakeCamName }; | ||
StreamServiceClient.prototype.addStream = vi | ||
.fn() | ||
.mockImplementation((_req, _md, cb) => { | ||
cb(null, {}); | ||
streamClient.emit('track', { streams: [fakeStream] }); | ||
}); | ||
|
||
const addStream = vi.spyOn(streamClient, 'add'); | ||
await expect(streamClient.getStream(fakeCamName)).resolves.toStrictEqual( | ||
fakeStream | ||
); | ||
await expect(streamClient.getStream(fakeCamName)).resolves.toStrictEqual( | ||
fakeStream | ||
); | ||
expect(addStream).toHaveBeenCalledTimes(2); | ||
expect(addStream).toHaveBeenCalledWith(fakeCamName); | ||
}); | ||
}); |
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
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