From ac99f3a471163ff26b70e9c7ae32a8a17e408e30 Mon Sep 17 00:00:00 2001 From: addievo Date: Fri, 20 Oct 2023 15:32:32 +1100 Subject: [PATCH] chore: add jests for tests where values handlerTimeout sets values greater and lesser than predefined value --- tests/RPCServer.test.ts | 85 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 82 insertions(+), 3 deletions(-) diff --git a/tests/RPCServer.test.ts b/tests/RPCServer.test.ts index c45054a..25fc41d 100644 --- a/tests/RPCServer.test.ts +++ b/tests/RPCServer.test.ts @@ -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(); @@ -961,7 +961,7 @@ describe(`${RPCServer.name}`, () => { } const ctxLongProm = promise(); class TestMethodLongTimeout extends UnaryHandler { - timeout = 100; + handlerTimeout = 26; public handle = async ( input: JSONValue, _cancel, @@ -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(); + class TestMethodShortTimeout extends UnaryHandler { + handlerTimeout = 250; + public handle = async ( + input: JSONValue, + _cancel, + _meta, + ctx_, + ): Promise => { + ctxShortProm.resolveP(ctx_); + await waitProm.p; + return input; + }; + } + const ctxLongProm = promise(); + class TestMethodLongTimeout extends UnaryHandler { + handlerTimeout = 261; + public handle = async ( + input: JSONValue, + _cancel, + _meta, + ctx_, + ): Promise => { + 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 = { + 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 = { + 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 }); }