OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * |
| 3 * Copyright 2015, Google Inc. |
| 4 * All rights reserved. |
| 5 * |
| 6 * Redistribution and use in source and binary forms, with or without |
| 7 * modification, are permitted provided that the following conditions are |
| 8 * met: |
| 9 * |
| 10 * * Redistributions of source code must retain the above copyright |
| 11 * notice, this list of conditions and the following disclaimer. |
| 12 * * Redistributions in binary form must reproduce the above |
| 13 * copyright notice, this list of conditions and the following disclaimer |
| 14 * in the documentation and/or other materials provided with the |
| 15 * distribution. |
| 16 * * Neither the name of Google Inc. nor the names of its |
| 17 * contributors may be used to endorse or promote products derived from |
| 18 * this software without specific prior written permission. |
| 19 * |
| 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 * |
| 32 */ |
| 33 |
| 34 'use strict'; |
| 35 |
| 36 var assert = require('assert'); |
| 37 |
| 38 var grpc = require('..'); |
| 39 var math = grpc.load(__dirname + '/../../proto/math/math.proto').math; |
| 40 |
| 41 /** |
| 42 * Client to use to make requests to a running server. |
| 43 */ |
| 44 var math_client; |
| 45 |
| 46 /** |
| 47 * Server to test against |
| 48 */ |
| 49 var getServer = require('./math/math_server.js'); |
| 50 |
| 51 var server = getServer(); |
| 52 |
| 53 describe('Math client', function() { |
| 54 before(function(done) { |
| 55 var port_num = server.bind('0.0.0.0:0', |
| 56 grpc.ServerCredentials.createInsecure()); |
| 57 server.start(); |
| 58 math_client = new math.Math('localhost:' + port_num, |
| 59 grpc.credentials.createInsecure()); |
| 60 done(); |
| 61 }); |
| 62 after(function() { |
| 63 server.forceShutdown(); |
| 64 }); |
| 65 it('should handle a single request', function(done) { |
| 66 var arg = {dividend: 7, divisor: 4}; |
| 67 math_client.div(arg, function handleDivResult(err, value) { |
| 68 assert.ifError(err); |
| 69 assert.equal(value.quotient, 1); |
| 70 assert.equal(value.remainder, 3); |
| 71 done(); |
| 72 }); |
| 73 }); |
| 74 it('should handle an error from a unary request', function(done) { |
| 75 var arg = {dividend: 7, divisor: 0}; |
| 76 math_client.div(arg, function handleDivResult(err, value) { |
| 77 assert(err); |
| 78 done(); |
| 79 }); |
| 80 }); |
| 81 it('should handle a server streaming request', function(done) { |
| 82 var call = math_client.fib({limit: 7}); |
| 83 var expected_results = [1, 1, 2, 3, 5, 8, 13]; |
| 84 var next_expected = 0; |
| 85 call.on('data', function checkResponse(value) { |
| 86 assert.equal(value.num, expected_results[next_expected]); |
| 87 next_expected += 1; |
| 88 }); |
| 89 call.on('status', function checkStatus(status) { |
| 90 assert.strictEqual(status.code, grpc.status.OK); |
| 91 done(); |
| 92 }); |
| 93 }); |
| 94 it('should handle a client streaming request', function(done) { |
| 95 var call = math_client.sum(function handleSumResult(err, value) { |
| 96 assert.ifError(err); |
| 97 assert.equal(value.num, 21); |
| 98 }); |
| 99 for (var i = 0; i < 7; i++) { |
| 100 call.write({'num': i}); |
| 101 } |
| 102 call.end(); |
| 103 call.on('status', function checkStatus(status) { |
| 104 assert.strictEqual(status.code, grpc.status.OK); |
| 105 done(); |
| 106 }); |
| 107 }); |
| 108 it('should handle a bidirectional streaming request', function(done) { |
| 109 function checkResponse(index, value) { |
| 110 assert.equal(value.quotient, index); |
| 111 assert.equal(value.remainder, 1); |
| 112 } |
| 113 var call = math_client.divMany(); |
| 114 var response_index = 0; |
| 115 call.on('data', function(value) { |
| 116 checkResponse(response_index, value); |
| 117 response_index += 1; |
| 118 }); |
| 119 for (var i = 0; i < 7; i++) { |
| 120 call.write({dividend: 2 * i + 1, divisor: 2}); |
| 121 } |
| 122 call.end(); |
| 123 call.on('status', function checkStatus(status) { |
| 124 assert.strictEqual(status.code, grpc.status.OK); |
| 125 done(); |
| 126 }); |
| 127 }); |
| 128 it('should handle an error from a bidi request', function(done) { |
| 129 var call = math_client.divMany(); |
| 130 call.on('data', function(value) { |
| 131 assert.fail(value, undefined, 'Unexpected data response on failing call', |
| 132 '!='); |
| 133 }); |
| 134 call.write({dividend: 7, divisor: 0}); |
| 135 call.end(); |
| 136 call.on('error', function checkStatus(status) { |
| 137 done(); |
| 138 }); |
| 139 }); |
| 140 }); |
OLD | NEW |