murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -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 | |
| 34 | var assert = require('assert'); |
| 35 | |
murgatroid99 | 10ac96c | 2015-02-12 13:28:25 -0800 | [diff] [blame] | 36 | var surface_server = require('../src/server.js'); |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 37 | |
murgatroid99 | 10ac96c | 2015-02-12 13:28:25 -0800 | [diff] [blame] | 38 | var surface_client = require('../src/client.js'); |
murgatroid99 | 55dd2ba | 2015-01-26 14:11:18 -0800 | [diff] [blame] | 39 | |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 40 | var ProtoBuf = require('protobufjs'); |
| 41 | |
| 42 | var grpc = require('..'); |
| 43 | |
| 44 | var math_proto = ProtoBuf.loadProtoFile(__dirname + '/../examples/math.proto'); |
| 45 | |
| 46 | var mathService = math_proto.lookup('math.Math'); |
| 47 | |
| 48 | describe('Surface server constructor', function() { |
| 49 | it('Should fail with conflicting method names', function() { |
| 50 | assert.throws(function() { |
| 51 | grpc.buildServer([mathService, mathService]); |
| 52 | }); |
| 53 | }); |
| 54 | it('Should succeed with a single service', function() { |
| 55 | assert.doesNotThrow(function() { |
| 56 | grpc.buildServer([mathService]); |
| 57 | }); |
| 58 | }); |
| 59 | it('Should fail with missing handlers', function() { |
| 60 | var Server = grpc.buildServer([mathService]); |
| 61 | assert.throws(function() { |
| 62 | new Server({ |
| 63 | 'math.Math': { |
murgatroid99 | fd81e70 | 2015-01-16 14:22:14 -0800 | [diff] [blame] | 64 | 'div': function() {}, |
| 65 | 'divMany': function() {}, |
| 66 | 'fib': function() {} |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 67 | } |
| 68 | }); |
| 69 | }, /math.Math.Sum/); |
| 70 | }); |
| 71 | it('Should fail with no handlers for the service', function() { |
| 72 | var Server = grpc.buildServer([mathService]); |
| 73 | assert.throws(function() { |
| 74 | new Server({}); |
| 75 | }, /math.Math/); |
| 76 | }); |
| 77 | }); |
murgatroid99 | 4d2d0f0 | 2015-01-29 11:44:46 -0800 | [diff] [blame] | 78 | describe('Cancelling surface client', function() { |
murgatroid99 | 55dd2ba | 2015-01-26 14:11:18 -0800 | [diff] [blame] | 79 | var client; |
| 80 | var server; |
| 81 | before(function() { |
| 82 | var Server = grpc.buildServer([mathService]); |
| 83 | server = new Server({ |
| 84 | 'math.Math': { |
| 85 | 'div': function(stream) {}, |
| 86 | 'divMany': function(stream) {}, |
| 87 | 'fib': function(stream) {}, |
| 88 | 'sum': function(stream) {} |
| 89 | } |
| 90 | }); |
| 91 | var port = server.bind('localhost:0'); |
| 92 | var Client = surface_client.makeClientConstructor(mathService); |
| 93 | client = new Client('localhost:' + port); |
| 94 | }); |
| 95 | after(function() { |
| 96 | server.shutdown(); |
| 97 | }); |
| 98 | it('Should correctly cancel a unary call', function(done) { |
| 99 | var call = client.div({'divisor': 0, 'dividend': 0}, function(err, resp) { |
| 100 | assert.strictEqual(err.code, surface_client.status.CANCELLED); |
| 101 | done(); |
| 102 | }); |
| 103 | call.cancel(); |
| 104 | }); |
| 105 | it('Should correctly cancel a client stream call', function(done) { |
| 106 | var call = client.sum(function(err, resp) { |
| 107 | assert.strictEqual(err.code, surface_client.status.CANCELLED); |
| 108 | done(); |
| 109 | }); |
| 110 | call.cancel(); |
| 111 | }); |
| 112 | it('Should correctly cancel a server stream call', function(done) { |
| 113 | var call = client.fib({'limit': 5}); |
| 114 | call.on('status', function(status) { |
| 115 | assert.strictEqual(status.code, surface_client.status.CANCELLED); |
| 116 | done(); |
| 117 | }); |
| 118 | call.cancel(); |
| 119 | }); |
| 120 | it('Should correctly cancel a bidi stream call', function(done) { |
| 121 | var call = client.divMany(); |
| 122 | call.on('status', function(status) { |
| 123 | assert.strictEqual(status.code, surface_client.status.CANCELLED); |
| 124 | done(); |
| 125 | }); |
| 126 | call.cancel(); |
| 127 | }); |
Craig Tiller | ce5021b | 2015-02-18 09:25:21 -0800 | [diff] [blame^] | 128 | }); |