murgatroid99 | 749666e | 2015-01-12 18:25:58 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
murgatroid99 | 1578c6a | 2015-02-11 16:19:55 -0800 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
murgatroid99 | 749666e | 2015-01-12 18:25:58 -0800 | [diff] [blame] | 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 | |
murgatroid99 | dca966d | 2015-02-19 14:37:18 -0800 | [diff] [blame] | 34 | 'use strict'; |
| 35 | |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 36 | var assert = require('assert'); |
| 37 | var grpc = require('bindings')('grpc.node'); |
| 38 | |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 39 | /** |
| 40 | * Helper function to return an absolute deadline given a relative timeout in |
| 41 | * seconds. |
| 42 | * @param {number} timeout_secs The number of seconds to wait before timing out |
| 43 | * @return {Date} A date timeout_secs in the future |
| 44 | */ |
| 45 | function getDeadline(timeout_secs) { |
| 46 | var deadline = new Date(); |
| 47 | deadline.setSeconds(deadline.getSeconds() + timeout_secs); |
| 48 | return deadline; |
| 49 | } |
| 50 | |
| 51 | describe('call', function() { |
murgatroid99 | 4b962f7 | 2015-01-26 14:31:03 -0800 | [diff] [blame] | 52 | var channel; |
| 53 | var server; |
| 54 | before(function() { |
| 55 | server = new grpc.Server(); |
| 56 | var port = server.addHttp2Port('localhost:0'); |
| 57 | server.start(); |
| 58 | channel = new grpc.Channel('localhost:' + port); |
| 59 | }); |
| 60 | after(function() { |
| 61 | server.shutdown(); |
| 62 | }); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 63 | describe('constructor', function() { |
| 64 | it('should reject anything less than 3 arguments', function() { |
| 65 | assert.throws(function() { |
| 66 | new grpc.Call(); |
| 67 | }, TypeError); |
| 68 | assert.throws(function() { |
| 69 | new grpc.Call(channel); |
| 70 | }, TypeError); |
| 71 | assert.throws(function() { |
| 72 | new grpc.Call(channel, 'method'); |
| 73 | }, TypeError); |
| 74 | }); |
| 75 | it('should succeed with a Channel, a string, and a date or number', |
| 76 | function() { |
| 77 | assert.doesNotThrow(function() { |
| 78 | new grpc.Call(channel, 'method', new Date()); |
| 79 | }); |
| 80 | assert.doesNotThrow(function() { |
| 81 | new grpc.Call(channel, 'method', 0); |
| 82 | }); |
| 83 | }); |
| 84 | it('should fail with a closed channel', function() { |
| 85 | var local_channel = new grpc.Channel('hostname'); |
| 86 | local_channel.close(); |
| 87 | assert.throws(function() { |
| 88 | new grpc.Call(channel, 'method'); |
| 89 | }); |
| 90 | }); |
| 91 | it('should fail with other types', function() { |
| 92 | assert.throws(function() { |
| 93 | new grpc.Call({}, 'method', 0); |
| 94 | }, TypeError); |
| 95 | assert.throws(function() { |
| 96 | new grpc.Call(channel, null, 0); |
| 97 | }, TypeError); |
| 98 | assert.throws(function() { |
| 99 | new grpc.Call(channel, 'method', 'now'); |
| 100 | }, TypeError); |
| 101 | }); |
| 102 | }); |
murgatroid99 | 016bb50 | 2015-02-09 15:55:10 -0800 | [diff] [blame] | 103 | describe('startBatch', function() { |
| 104 | it('should fail without an object and a function', function() { |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 105 | var call = new grpc.Call(channel, 'method', getDeadline(1)); |
murgatroid99 | 016bb50 | 2015-02-09 15:55:10 -0800 | [diff] [blame] | 106 | assert.throws(function() { |
| 107 | call.startBatch(); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 108 | }); |
murgatroid99 | 016bb50 | 2015-02-09 15:55:10 -0800 | [diff] [blame] | 109 | assert.throws(function() { |
| 110 | call.startBatch({}); |
| 111 | }); |
| 112 | assert.throws(function() { |
| 113 | call.startBatch(null, function(){}); |
murgatroid99 | 7625db4 | 2015-01-28 15:36:27 -0800 | [diff] [blame] | 114 | }); |
| 115 | }); |
murgatroid99 | c55ee61 | 2015-02-12 13:58:24 -0800 | [diff] [blame] | 116 | it('should succeed with an empty object', function(done) { |
murgatroid99 | 7625db4 | 2015-01-28 15:36:27 -0800 | [diff] [blame] | 117 | var call = new grpc.Call(channel, 'method', getDeadline(1)); |
| 118 | assert.doesNotThrow(function() { |
murgatroid99 | 016bb50 | 2015-02-09 15:55:10 -0800 | [diff] [blame] | 119 | call.startBatch({}, function(err) { |
| 120 | assert.ifError(err); |
| 121 | done(); |
| 122 | }); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 123 | }); |
murgatroid99 | 016bb50 | 2015-02-09 15:55:10 -0800 | [diff] [blame] | 124 | }); |
| 125 | }); |
| 126 | describe('startBatch with metadata', function() { |
| 127 | it('should succeed with a map of strings to string arrays', function(done) { |
| 128 | var call = new grpc.Call(channel, 'method', getDeadline(1)); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 129 | assert.doesNotThrow(function() { |
murgatroid99 | 016bb50 | 2015-02-09 15:55:10 -0800 | [diff] [blame] | 130 | var batch = {}; |
| 131 | batch[grpc.opType.SEND_INITIAL_METADATA] = {'key1': ['value1'], |
| 132 | 'key2': ['value2']}; |
| 133 | call.startBatch(batch, function(err, resp) { |
| 134 | assert.ifError(err); |
| 135 | assert.deepEqual(resp, {'send metadata': true}); |
| 136 | done(); |
| 137 | }); |
| 138 | }); |
| 139 | }); |
| 140 | it('should succeed with a map of strings to buffer arrays', function(done) { |
| 141 | var call = new grpc.Call(channel, 'method', getDeadline(1)); |
| 142 | assert.doesNotThrow(function() { |
| 143 | var batch = {}; |
| 144 | batch[grpc.opType.SEND_INITIAL_METADATA] = { |
murgatroid99 | da77151 | 2015-03-17 18:13:55 -0700 | [diff] [blame] | 145 | 'key1-bin': [new Buffer('value1')], |
| 146 | 'key2-bin': [new Buffer('value2')] |
murgatroid99 | 016bb50 | 2015-02-09 15:55:10 -0800 | [diff] [blame] | 147 | }; |
| 148 | call.startBatch(batch, function(err, resp) { |
| 149 | assert.ifError(err); |
| 150 | assert.deepEqual(resp, {'send metadata': true}); |
| 151 | done(); |
| 152 | }); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 153 | }); |
| 154 | }); |
| 155 | it('should fail with other parameter types', function() { |
| 156 | var call = new grpc.Call(channel, 'method', getDeadline(1)); |
| 157 | assert.throws(function() { |
murgatroid99 | 016bb50 | 2015-02-09 15:55:10 -0800 | [diff] [blame] | 158 | var batch = {}; |
| 159 | batch[grpc.opType.SEND_INITIAL_METADATA] = undefined; |
| 160 | call.startBatch(batch, function(){}); |
murgatroid99 | 7625db4 | 2015-01-28 15:36:27 -0800 | [diff] [blame] | 161 | }); |
| 162 | assert.throws(function() { |
murgatroid99 | 016bb50 | 2015-02-09 15:55:10 -0800 | [diff] [blame] | 163 | var batch = {}; |
| 164 | batch[grpc.opType.SEND_INITIAL_METADATA] = null; |
| 165 | call.startBatch(batch, function(){}); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 166 | }, TypeError); |
| 167 | assert.throws(function() { |
murgatroid99 | 016bb50 | 2015-02-09 15:55:10 -0800 | [diff] [blame] | 168 | var batch = {}; |
| 169 | batch[grpc.opType.SEND_INITIAL_METADATA] = 'value'; |
| 170 | call.startBatch(batch, function(){}); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 171 | }, TypeError); |
| 172 | assert.throws(function() { |
murgatroid99 | 016bb50 | 2015-02-09 15:55:10 -0800 | [diff] [blame] | 173 | var batch = {}; |
| 174 | batch[grpc.opType.SEND_INITIAL_METADATA] = 5; |
| 175 | call.startBatch(batch, function(){}); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 176 | }, TypeError); |
| 177 | }); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 178 | }); |
| 179 | describe('cancel', function() { |
| 180 | it('should succeed', function() { |
| 181 | var call = new grpc.Call(channel, 'method', getDeadline(1)); |
| 182 | assert.doesNotThrow(function() { |
| 183 | call.cancel(); |
| 184 | }); |
| 185 | }); |
| 186 | }); |
| 187 | }); |