murgatroid99 | 6cbb237 | 2015-03-09 11:16:56 -0700 | [diff] [blame] | 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'); |
yang-g | 52e4de1 | 2015-07-22 10:33:18 -0700 | [diff] [blame] | 37 | var fs = require('fs'); |
| 38 | var path = require('path'); |
murgatroid99 | 2af89e4 | 2015-10-01 11:54:00 -0700 | [diff] [blame] | 39 | var grpc = require('bindings')('grpc_node'); |
murgatroid99 | 6cbb237 | 2015-03-09 11:16:56 -0700 | [diff] [blame] | 40 | |
| 41 | describe('server', function() { |
| 42 | describe('constructor', function() { |
| 43 | it('should work with no arguments', function() { |
| 44 | assert.doesNotThrow(function() { |
| 45 | new grpc.Server(); |
| 46 | }); |
| 47 | }); |
murgatroid99 | a9172d2 | 2015-12-09 16:12:37 -0800 | [diff] [blame] | 48 | it('should work with an empty object argument', function() { |
murgatroid99 | 6cbb237 | 2015-03-09 11:16:56 -0700 | [diff] [blame] | 49 | assert.doesNotThrow(function() { |
murgatroid99 | a9172d2 | 2015-12-09 16:12:37 -0800 | [diff] [blame] | 50 | 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()}); |
murgatroid99 | 6cbb237 | 2015-03-09 11:16:56 -0700 | [diff] [blame] | 72 | }); |
| 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() { |
murgatroid99 | 1a7dcac | 2015-07-27 16:13:28 -0700 | [diff] [blame] | 83 | port = server.addHttp2Port('0.0.0.0:0', |
| 84 | grpc.ServerCredentials.createInsecure()); |
murgatroid99 | 6cbb237 | 2015-03-09 11:16:56 -0700 | [diff] [blame] | 85 | }); |
| 86 | assert(port > 0); |
| 87 | }); |
yang-g | 52e4de1 | 2015-07-22 10:33:18 -0700 | [diff] [blame] | 88 | it('should bind to an unused port with ssl credentials', function() { |
murgatroid99 | 6cbb237 | 2015-03-09 11:16:56 -0700 | [diff] [blame] | 89 | var port; |
yang-g | 52e4de1 | 2015-07-22 10:33:18 -0700 | [diff] [blame] | 90 | 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); |
murgatroid99 | c2fdfcf | 2015-08-19 14:57:19 -0700 | [diff] [blame] | 94 | var creds = grpc.ServerCredentials.createSsl(null, |
murgatroid99 | 6b3737d | 2015-08-19 15:02:15 -0700 | [diff] [blame] | 95 | [{private_key: key_data, |
| 96 | cert_chain: pem_data}]); |
murgatroid99 | 6cbb237 | 2015-03-09 11:16:56 -0700 | [diff] [blame] | 97 | assert.doesNotThrow(function() { |
murgatroid99 | 1a7dcac | 2015-07-27 16:13:28 -0700 | [diff] [blame] | 98 | port = server.addHttp2Port('0.0.0.0:0', creds); |
murgatroid99 | 6cbb237 | 2015-03-09 11:16:56 -0700 | [diff] [blame] | 99 | }); |
| 100 | assert(port > 0); |
| 101 | }); |
| 102 | }); |
murgatroid99 | 1a7dcac | 2015-07-27 16:13:28 -0700 | [diff] [blame] | 103 | describe('addSecureHttp2Port', function() { |
| 104 | var server; |
| 105 | before(function() { |
| 106 | server = new grpc.Server(); |
| 107 | }); |
| 108 | }); |
murgatroid99 | bc15a78 | 2015-08-14 11:09:04 -0700 | [diff] [blame] | 109 | describe('start', function() { |
murgatroid99 | 6cbb237 | 2015-03-09 11:16:56 -0700 | [diff] [blame] | 110 | var server; |
| 111 | before(function() { |
| 112 | server = new grpc.Server(); |
murgatroid99 | 1a7dcac | 2015-07-27 16:13:28 -0700 | [diff] [blame] | 113 | server.addHttp2Port('0.0.0.0:0', grpc.ServerCredentials.createInsecure()); |
murgatroid99 | 6cbb237 | 2015-03-09 11:16:56 -0700 | [diff] [blame] | 114 | }); |
| 115 | after(function() { |
murgatroid99 | cb951f6 | 2015-08-18 17:38:11 -0700 | [diff] [blame] | 116 | server.forceShutdown(); |
murgatroid99 | 6cbb237 | 2015-03-09 11:16:56 -0700 | [diff] [blame] | 117 | }); |
murgatroid99 | bc15a78 | 2015-08-14 11:09:04 -0700 | [diff] [blame] | 118 | it('should start without error', function() { |
murgatroid99 | 6cbb237 | 2015-03-09 11:16:56 -0700 | [diff] [blame] | 119 | assert.doesNotThrow(function() { |
| 120 | server.start(); |
| 121 | }); |
| 122 | }); |
| 123 | }); |
murgatroid99 | cb951f6 | 2015-08-18 17:38:11 -0700 | [diff] [blame] | 124 | 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 | }); |
murgatroid99 | c5dac97 | 2015-08-19 11:49:53 -0700 | [diff] [blame] | 137 | it('forceShutdown should shutdown successfully', function() { |
murgatroid99 | cb951f6 | 2015-08-18 17:38:11 -0700 | [diff] [blame] | 138 | 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 | }); |
murgatroid99 | c5dac97 | 2015-08-19 11:49:53 -0700 | [diff] [blame] | 148 | it('forceShutdown should trigger tryShutdown', function(done) { |
| 149 | server.tryShutdown(done); |
| 150 | server.forceShutdown(); |
| 151 | }); |
murgatroid99 | cb951f6 | 2015-08-18 17:38:11 -0700 | [diff] [blame] | 152 | }); |
murgatroid99 | 6cbb237 | 2015-03-09 11:16:56 -0700 | [diff] [blame] | 153 | }); |