Skip to content

Commit

Permalink
fix(nice-grpc-client-middleware-deadline): Skip deadline when signal …
Browse files Browse the repository at this point in the history
…is already aborted (#472)
  • Loading branch information
s0meone authored Oct 22, 2023
1 parent 99262e5 commit 2c5cd0b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
36 changes: 36 additions & 0 deletions packages/nice-grpc-client-middleware-deadline/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,39 @@ test('relative deadline', async () => {

await server.shutdown();
});

test('already aborted', async () => {
const server = createServer();

server.add(TestService, {
async testUnary(request: TestRequest, {signal}) {
return new TestResponse();
},
testServerStream: throwUnimplemented,
testClientStream: throwUnimplemented,
testBidiStream: throwUnimplemented,
});

const port = await server.listen('127.0.0.1:0');

const channel = createChannel(`127.0.0.1:${port}`);
const client = createClientFactory()
.use(deadlineMiddleware)
.create(TestService, channel);

const abortController = new AbortController();
abortController.abort();

const promise = client.testUnary(new TestRequest(), {
deadline: new Date(Date.now() + 500),
signal: abortController.signal,
});

await expect(promise).rejects.toMatchInlineSnapshot(
`[AbortError: The operation has been aborted]`,
);

channel.close();

await server.shutdown();
});
2 changes: 1 addition & 1 deletion packages/nice-grpc-client-middleware-deadline/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export type DeadlineOptions = {
*/
export const deadlineMiddleware: ClientMiddleware<DeadlineOptions> =
async function* deadlineMiddleware(call, options) {
if (options.deadline == null) {
if (options.deadline == null || options.signal?.aborted) {
return yield* call.next(call.request, options);
}

Expand Down

0 comments on commit 2c5cd0b

Please sign in to comment.