blob: d6694724e5498d326d5edc30ebd32a3c08a60c31 [file] [log] [blame]
murgatroid99cca5ffa2015-01-15 14:06:56 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * Copyright 2015, Google Inc.
murgatroid99cca5ffa2015-01-15 14:06:56 -08004 * 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
34var assert = require('assert');
35
murgatroid9910ac96c2015-02-12 13:28:25 -080036var surface_server = require('../src/server.js');
murgatroid99cca5ffa2015-01-15 14:06:56 -080037
murgatroid9910ac96c2015-02-12 13:28:25 -080038var surface_client = require('../src/client.js');
murgatroid9955dd2ba2015-01-26 14:11:18 -080039
murgatroid99cca5ffa2015-01-15 14:06:56 -080040var ProtoBuf = require('protobufjs');
41
42var grpc = require('..');
43
44var math_proto = ProtoBuf.loadProtoFile(__dirname + '/../examples/math.proto');
45
46var mathService = math_proto.lookup('math.Math');
47
48describe('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': {
murgatroid99fd81e702015-01-16 14:22:14 -080064 'div': function() {},
65 'divMany': function() {},
66 'fib': function() {}
murgatroid99cca5ffa2015-01-15 14:06:56 -080067 }
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});
murgatroid994d2d0f02015-01-29 11:44:46 -080078describe('Cancelling surface client', function() {
murgatroid9955dd2ba2015-01-26 14:11:18 -080079 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 Tillerce5021b2015-02-18 09:25:21 -0800128});