|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright 2023 gRPC authors. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + * |
| 17 | + */ |
| 18 | + |
| 19 | +const grpc = require('@grpc/grpc-js'); |
| 20 | +const protoLoader = require('@grpc/proto-loader'); |
| 21 | +const parseArgs = require('minimist'); |
| 22 | + |
| 23 | +const PROTO_PATH = __dirname + '/../protos/echo.proto'; |
| 24 | + |
| 25 | +const packageDefinition = protoLoader.loadSync( |
| 26 | + PROTO_PATH, |
| 27 | + {keepCase: true, |
| 28 | + longs: String, |
| 29 | + enums: String, |
| 30 | + defaults: true, |
| 31 | + oneofs: true |
| 32 | + }); |
| 33 | +const echoProto = grpc.loadPackageDefinition(packageDefinition).grpc.examples.echo; |
| 34 | + |
| 35 | +function unaryCall(client, requestId, message, expectedCode) { |
| 36 | + return new Promise((resolve, reject) => { |
| 37 | + const deadline = new Date(); |
| 38 | + deadline.setSeconds(deadline.getSeconds() + 1); |
| 39 | + client.unaryEcho({message: message}, {deadline}, (error, value) => { |
| 40 | + let code; |
| 41 | + if (error) { |
| 42 | + code = error.code; |
| 43 | + } else { |
| 44 | + code = grpc.status.OK; |
| 45 | + } |
| 46 | + console.log(`[${requestId}] wanted = ${grpc.status[expectedCode]} got = ${grpc.status[code]}`); |
| 47 | + resolve(); |
| 48 | + }); |
| 49 | + }); |
| 50 | +} |
| 51 | + |
| 52 | +function streamingCall(client, requestId, message, expectedCode) { |
| 53 | + return new Promise((resolve, reject) => { |
| 54 | + const deadline = new Date(); |
| 55 | + deadline.setSeconds(deadline.getSeconds() + 1); |
| 56 | + const call = client.bidirectionalStreamingEcho({deadline}); |
| 57 | + call.on('data', () => { |
| 58 | + // Consume all response messages |
| 59 | + }); |
| 60 | + call.on('status', status => { |
| 61 | + console.log(`[${requestId}] wanted = ${grpc.status[expectedCode]} got = ${grpc.status[status.code]}`); |
| 62 | + resolve(); |
| 63 | + }); |
| 64 | + call.on('error', () => { |
| 65 | + // Ignore error event |
| 66 | + }); |
| 67 | + call.write({message}); |
| 68 | + call.end(); |
| 69 | + }); |
| 70 | +} |
| 71 | + |
| 72 | +async function main() { |
| 73 | + let argv = parseArgs(process.argv.slice(2), { |
| 74 | + string: 'target', |
| 75 | + default: {target: 'localhost:50052'} |
| 76 | + }); |
| 77 | + const client = new echoProto.Echo(argv.target, grpc.credentials.createInsecure()); |
| 78 | + // A successful request |
| 79 | + await unaryCall(client, 1, 'world', grpc.status.OK); |
| 80 | + // Exceeds deadline |
| 81 | + await unaryCall(client, 2, 'delay', grpc.status.DEADLINE_EXCEEDED); |
| 82 | + // A successful request with propagated deadline |
| 83 | + await unaryCall(client, 3, '[propagate me]world', grpc.status.OK); |
| 84 | + // Exceeds propagated deadline |
| 85 | + await unaryCall(client, 4, '[propagate me][propagate me]world', grpc.status.DEADLINE_EXCEEDED); |
| 86 | + // Receives a response from the stream successfully |
| 87 | + await streamingCall(client, 5, '[propagate me]world', grpc.status.OK); |
| 88 | + // Exceeds propagated deadline before receiving a response |
| 89 | + await streamingCall(client, 6, '[propagate me][propagate me]world', grpc.status.DEADLINE_EXCEEDED); |
| 90 | +} |
| 91 | + |
| 92 | +main(); |
0 commit comments