Skip to content

Commit

Permalink
chore: add jests for tests where values handlerTimeout sets values gr…
Browse files Browse the repository at this point in the history
…eater and lesser than predefined value
  • Loading branch information
addievo committed Oct 20, 2023
1 parent da531d9 commit ac99f3a
Showing 1 changed file with 82 additions and 3 deletions.
85 changes: 82 additions & 3 deletions tests/RPCServer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -942,7 +942,7 @@ describe(`${RPCServer.name}`, () => {
}
await rpcServer.stop({ force: true });
});
test('handler overrides timeout', async () => {
test('handler overrides timeout - lesser value', async () => {
{
const waitProm = promise();
const ctxShortProm = promise<ContextTimed>();
Expand All @@ -961,7 +961,7 @@ describe(`${RPCServer.name}`, () => {
}
const ctxLongProm = promise<ContextTimed>();
class TestMethodLongTimeout extends UnaryHandler {
timeout = 100;
handlerTimeout = 26;
public handle = async (
input: JSONValue,
_cancel,
Expand Down Expand Up @@ -1016,7 +1016,86 @@ describe(`${RPCServer.name}`, () => {

// Longer timeout is set to server's default
const ctxLong = await ctxLongProm.p;
expect(ctxLong.timer.delay).toEqual(50);
expect(ctxLong.timer.delay).toEqual(26);
waitProm.resolveP();
await rpcServer.stop({ force: true });
}
});
test('handler overrides timeout - greater value', async () => {
{
const waitProm = promise();
const ctxShortProm = promise<ContextTimed>();
class TestMethodShortTimeout extends UnaryHandler {
handlerTimeout = 250;
public handle = async (
input: JSONValue,
_cancel,
_meta,
ctx_,
): Promise<JSONValue> => {
ctxShortProm.resolveP(ctx_);
await waitProm.p;
return input;
};
}
const ctxLongProm = promise<ContextTimed>();
class TestMethodLongTimeout extends UnaryHandler {
handlerTimeout = 261;
public handle = async (
input: JSONValue,
_cancel,
_meta,
ctx_,
): Promise<JSONValue> => {
ctxLongProm.resolveP(ctx_);
await waitProm.p;
return input;
};
}
const rpcServer = new RPCServer({
handlerTimeoutTime: 50,
logger,
idGen,
});
await rpcServer.start({
manifest: {
testShort: new TestMethodShortTimeout({}),
testLong: new TestMethodLongTimeout({}),
},
});
const streamShort = rpcTestUtils.messagesToReadableStream([
{
jsonrpc: '2.0',
method: 'testShort',
params: null,
},
]);
const readWriteStreamShort: RPCStream<Uint8Array, Uint8Array> = {
cancel: () => {},
readable: streamShort,
writable: new WritableStream(),
};
rpcServer.handleStream(readWriteStreamShort);
// Shorter timeout is updated
const ctxShort = await ctxShortProm.p;
expect(ctxShort.timer.delay).toEqual(250);
const streamLong = rpcTestUtils.messagesToReadableStream([
{
jsonrpc: '2.0',
method: 'testLong',
params: null,
},
]);
const readWriteStreamLong: RPCStream<Uint8Array, Uint8Array> = {
cancel: () => {},
readable: streamLong,
writable: new WritableStream(),
};
rpcServer.handleStream(readWriteStreamLong);

// Longer timeout is set to server's default
const ctxLong = await ctxLongProm.p;
expect(ctxLong.timer.delay).toEqual(261);
waitProm.resolveP();
await rpcServer.stop({ force: true });
}
Expand Down

0 comments on commit ac99f3a

Please sign in to comment.