blob: 592f47e1456db6d4f99e34668b89c2440ae034ec [file] [log] [blame]
murgatroid996cbb2372015-03-09 11:16:56 -07001/*
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
36var assert = require('assert');
yang-g52e4de12015-07-22 10:33:18 -070037var fs = require('fs');
38var path = require('path');
murgatroid992af89e42015-10-01 11:54:00 -070039var grpc = require('bindings')('grpc_node');
murgatroid996cbb2372015-03-09 11:16:56 -070040
41describe('server', function() {
42 describe('constructor', function() {
43 it('should work with no arguments', function() {
44 assert.doesNotThrow(function() {
45 new grpc.Server();
46 });
47 });
murgatroid99a9172d22015-12-09 16:12:37 -080048 it('should work with an empty object argument', function() {
murgatroid996cbb2372015-03-09 11:16:56 -070049 assert.doesNotThrow(function() {
murgatroid99a9172d22015-12-09 16:12:37 -080050 new grpc.Server({});
51 });
52 });
53 it('should work without the new keyword', function() {
54 var server;
55 assert.doesNotThrow(function() {
56 server = grpc.Server();
57 });
58 assert(server instanceof grpc.Server);
59 });
60 it('should only accept objects with string or int values', function() {
61 assert.doesNotThrow(function() {
62 new grpc.Server({'key' : 'value'});
63 });
64 assert.doesNotThrow(function() {
65 new grpc.Server({'key' : 5});
66 });
67 assert.throws(function() {
68 new grpc.Server({'key' : null});
69 });
70 assert.throws(function() {
71 new grpc.Server({'key' : new Date()});
murgatroid996cbb2372015-03-09 11:16:56 -070072 });
73 });
74 });
75 describe('addHttp2Port', function() {
76 var server;
77 before(function() {
78 server = new grpc.Server();
79 });
80 it('should bind to an unused port', function() {
81 var port;
82 assert.doesNotThrow(function() {
murgatroid991a7dcac2015-07-27 16:13:28 -070083 port = server.addHttp2Port('0.0.0.0:0',
84 grpc.ServerCredentials.createInsecure());
murgatroid996cbb2372015-03-09 11:16:56 -070085 });
86 assert(port > 0);
87 });
yang-g52e4de12015-07-22 10:33:18 -070088 it('should bind to an unused port with ssl credentials', function() {
murgatroid996cbb2372015-03-09 11:16:56 -070089 var port;
yang-g52e4de12015-07-22 10:33:18 -070090 var key_path = path.join(__dirname, '../test/data/server1.key');
91 var pem_path = path.join(__dirname, '../test/data/server1.pem');
92 var key_data = fs.readFileSync(key_path);
93 var pem_data = fs.readFileSync(pem_path);
murgatroid99c2fdfcf2015-08-19 14:57:19 -070094 var creds = grpc.ServerCredentials.createSsl(null,
murgatroid996b3737d2015-08-19 15:02:15 -070095 [{private_key: key_data,
96 cert_chain: pem_data}]);
murgatroid996cbb2372015-03-09 11:16:56 -070097 assert.doesNotThrow(function() {
murgatroid991a7dcac2015-07-27 16:13:28 -070098 port = server.addHttp2Port('0.0.0.0:0', creds);
murgatroid996cbb2372015-03-09 11:16:56 -070099 });
100 assert(port > 0);
101 });
102 });
murgatroid991a7dcac2015-07-27 16:13:28 -0700103 describe('addSecureHttp2Port', function() {
104 var server;
105 before(function() {
106 server = new grpc.Server();
107 });
108 });
murgatroid99bc15a782015-08-14 11:09:04 -0700109 describe('start', function() {
murgatroid996cbb2372015-03-09 11:16:56 -0700110 var server;
111 before(function() {
112 server = new grpc.Server();
murgatroid991a7dcac2015-07-27 16:13:28 -0700113 server.addHttp2Port('0.0.0.0:0', grpc.ServerCredentials.createInsecure());
murgatroid996cbb2372015-03-09 11:16:56 -0700114 });
115 after(function() {
murgatroid99cb951f62015-08-18 17:38:11 -0700116 server.forceShutdown();
murgatroid996cbb2372015-03-09 11:16:56 -0700117 });
murgatroid99bc15a782015-08-14 11:09:04 -0700118 it('should start without error', function() {
murgatroid996cbb2372015-03-09 11:16:56 -0700119 assert.doesNotThrow(function() {
120 server.start();
121 });
122 });
123 });
murgatroid99cb951f62015-08-18 17:38:11 -0700124 describe('shutdown', function() {
125 var server;
126 beforeEach(function() {
127 server = new grpc.Server();
128 server.addHttp2Port('0.0.0.0:0', grpc.ServerCredentials.createInsecure());
129 server.start();
130 });
131 afterEach(function() {
132 server.forceShutdown();
133 });
134 it('tryShutdown should shutdown successfully', function(done) {
135 server.tryShutdown(done);
136 });
murgatroid99c5dac972015-08-19 11:49:53 -0700137 it('forceShutdown should shutdown successfully', function() {
murgatroid99cb951f62015-08-18 17:38:11 -0700138 server.forceShutdown();
139 });
140 it('tryShutdown should be idempotent', function(done) {
141 server.tryShutdown(done);
142 server.tryShutdown(function() {});
143 });
144 it('forceShutdown should be idempotent', function() {
145 server.forceShutdown();
146 server.forceShutdown();
147 });
murgatroid99c5dac972015-08-19 11:49:53 -0700148 it('forceShutdown should trigger tryShutdown', function(done) {
149 server.tryShutdown(done);
150 server.forceShutdown();
151 });
murgatroid99cb951f62015-08-18 17:38:11 -0700152 });
murgatroid996cbb2372015-03-09 11:16:56 -0700153});