Skip to content

Commit

Permalink
feat(server): don't await handleCall
Browse files Browse the repository at this point in the history
  • Loading branch information
edvardchen committed Aug 9, 2019
1 parent a6300ea commit cf850cf
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 9 deletions.
52 changes: 46 additions & 6 deletions __tests__/server.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,57 @@
import { Interceptor } from '../src/server';

import { handleUnaryCall, ServiceError, status } from 'grpc';
import { Point, Feature } from './fixtures/static_codegen/route_guide_pb';
import {
ServerUnaryCall,
sendUnaryData,
handleUnaryCall,
ServerWriteableStream,
ServiceError,
status,
} from 'grpc';
import {
Point,
Feature,
Rectangle,
} from './fixtures/static_codegen/route_guide_pb';

import runTest from './helpers/runTest';

describe('server', () => {
describe('pass through', () => {
const getFeature: handleUnaryCall<Point, Feature> = (call, callback) => {
callback(null, new Feature());
};
runTest({
implementations: { getFeature },
implementations: {
getFeature(
call: ServerUnaryCall<Point>,
callback: sendUnaryData<Feature>
) {
callback(null, new Feature());
return new Promise(resolve => setTimeout(resolve, 30000));
},
listFeatures(call: ServerWriteableStream<Feature>) {
call.end();
return new Promise(resolve => setTimeout(resolve, 30000));
},
},
testcase(server, client) {
describe('should not timeout', () => {
it('getFeature', done => {
// @ts-ignore
client().getFeature(new Point(), done);
});

it('listFeatures', done => {
server().use(async (ctx, next) => {
await next();
done();
});
// @ts-ignore
client().listFeatures(new Rectangle());
});
});
},
});

runTest({
testcase(getServer, getClient) {
it('getFeature', done => {
const server = getServer();
Expand Down
8 changes: 5 additions & 3 deletions src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,11 @@ export default class ExperimentalServer extends Server {
if (!first) {
// no interceptors
// @ts-ignore
return original(call, grpcCallback);
original(call, grpcCallback);
return;
}

first(ctx, async function next() {
first(ctx, function next() {
const { done, value } = interceptors.next();
if (done) {
// unary call
Expand All @@ -92,7 +93,8 @@ export default class ExperimentalServer extends Server {
}

// @ts-ignore
return original(call);
original(call);
return;
}
// run another interceptor
return value(ctx, next);
Expand Down

0 comments on commit cf850cf

Please sign in to comment.