-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#56 add test code for promise rejections
- Loading branch information
Showing
4 changed files
with
156 additions
and
0 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
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import {ChannelCredentials} from "@grpc/grpc-js"; | ||
import {GrpcTransport} from "@protobuf-ts/grpc-transport"; | ||
import {ChatServiceClient} from "./service-chat.client"; | ||
import AbortController from "abort-controller"; | ||
|
||
|
||
main().catch(e => console.error(e)).finally(() => process.exit()); | ||
|
||
|
||
async function main() { | ||
|
||
const client = new ChatServiceClient(new GrpcTransport({ | ||
host: "localhost:5000", | ||
channelCredentials: ChannelCredentials.createInsecure(), | ||
})); | ||
|
||
const abortController = new AbortController(); | ||
|
||
const call = client.join({ | ||
username: 'xxx' | ||
}, { | ||
abort: abortController.signal | ||
}); | ||
|
||
await call.headers; | ||
|
||
// this rejects all call promises. | ||
// but we are not catching them, so we get an UnhandledPromiseRejectionWarning | ||
abortController.abort(); | ||
|
||
// TODO UnhandledPromiseRejectionWarning: RpcError: 1 CANCELLED: Cancelled on client | ||
// DeprecationWarning: Unhandled promise rejections are deprecated. In the future, | ||
// promise rejections that are not handled will terminate the Node.js process with | ||
// a non-zero exit code. | ||
|
||
// this catches all promise rejections, but it looks messy | ||
// call.then(null, reason => { | ||
// console.log('caught call reject') | ||
// }); | ||
|
||
|
||
await delay(1400); | ||
|
||
console.log('done.'); | ||
} | ||
|
||
|
||
function delay(ms: number) { | ||
return new Promise(resolve => setTimeout(resolve, ms)); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import * as grpc from '@grpc/grpc-js'; | ||
import {ChatEvent, JoinRequest, PostRequest, PostResponse} from "./service-chat"; | ||
import {chatServiceDefinition, IChatService} from "./service-chat.grpc-server"; | ||
|
||
|
||
// TODO UnhandledPromiseRejectionWarning - see client-alt.ts | ||
|
||
|
||
const host = '0.0.0.0:5000'; | ||
|
||
|
||
const service: IChatService = { | ||
|
||
join(call: grpc.ServerWritableStream<JoinRequest, ChatEvent>) { | ||
let e: any = new Error(); | ||
e.code = grpc.status.UNIMPLEMENTED; | ||
e.details = 'not implemented'; | ||
call.destroy(e); | ||
}, | ||
|
||
post(call: grpc.ServerUnaryCall<PostRequest, ChatEvent>, callback: grpc.sendUnaryData<PostResponse>) { | ||
callback({ | ||
code: grpc.status.UNIMPLEMENTED, | ||
details: 'not implemented', | ||
}); | ||
}, | ||
|
||
}; | ||
|
||
|
||
const server = new grpc.Server(); | ||
server.addService(chatServiceDefinition, service); | ||
server.bindAsync( | ||
host, | ||
grpc.ServerCredentials.createInsecure(), | ||
(err: Error | null, port: number) => { | ||
if (err) { | ||
console.error(`Server error: ${err.message}`); | ||
} else { | ||
console.log(`Server bound on port: ${port}`); | ||
server.start(); | ||
} | ||
} | ||
); |
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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// @generated by protobuf-ts 2.0.0-alpha.9 with parameters server_grpc,optimize_code_size | ||
// @generated from protobuf file "service-chat.proto" (package "spec", syntax proto3) | ||
// tslint:disable | ||
import { PostResponse } from "./service-chat"; | ||
import { PostRequest } from "./service-chat"; | ||
import { ChatEvent } from "./service-chat"; | ||
import { JoinRequest } from "./service-chat"; | ||
import * as grpc from "@grpc/grpc-js"; | ||
/** | ||
* @generated from protobuf service spec.ChatService | ||
*/ | ||
export interface IChatService extends grpc.UntypedServiceImplementation { | ||
/** | ||
* returns a response header "x-token" to authorize Post() calls. | ||
* streams all chat messages. | ||
* | ||
* @generated from protobuf rpc: Join(spec.JoinRequest) returns (stream spec.ChatEvent); | ||
*/ | ||
join: grpc.handleServerStreamingCall<JoinRequest, ChatEvent>; | ||
/** | ||
* post a message to the chat. | ||
* must set "x-token" request header obtained from Join(). | ||
* | ||
* @generated from protobuf rpc: Post(spec.PostRequest) returns (spec.PostResponse); | ||
*/ | ||
post: grpc.handleUnaryCall<PostRequest, PostResponse>; | ||
} | ||
/** | ||
* @grpc/grpc-js definition for the protobuf service spec.ChatService. | ||
* | ||
* Usage: Implement the interface IChatService and add to a grpc server. | ||
* | ||
* ```typescript | ||
* const server = new grpc.Server(); | ||
* const service: IChatService = ... | ||
* server.addService(chatServiceDefinition, service); | ||
* ``` | ||
*/ | ||
export const chatServiceDefinition: grpc.ServiceDefinition<IChatService> = { | ||
join: { | ||
path: "/spec.ChatService/Join", | ||
originalName: "Join", | ||
requestStream: false, | ||
responseStream: true, | ||
responseDeserialize: bytes => ChatEvent.fromBinary(bytes), | ||
requestDeserialize: bytes => JoinRequest.fromBinary(bytes), | ||
responseSerialize: value => Buffer.from(ChatEvent.toBinary(value)), | ||
requestSerialize: value => Buffer.from(JoinRequest.toBinary(value)) | ||
}, | ||
post: { | ||
path: "/spec.ChatService/Post", | ||
originalName: "Post", | ||
requestStream: false, | ||
responseStream: false, | ||
responseDeserialize: bytes => PostResponse.fromBinary(bytes), | ||
requestDeserialize: bytes => PostRequest.fromBinary(bytes), | ||
responseSerialize: value => Buffer.from(PostResponse.toBinary(value)), | ||
requestSerialize: value => Buffer.from(PostRequest.toBinary(value)) | ||
} | ||
}; |