murgatroid99 | 43a1479 | 2015-09-24 11:29:10 -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'); |
| 37 | |
| 38 | var grpc = require('..'); |
Jan Tattermusch | 8ef43bd | 2015-12-07 09:55:22 -0800 | [diff] [blame] | 39 | var math = grpc.load(__dirname + '/../../proto/math/math.proto').math; |
murgatroid99 | 43a1479 | 2015-09-24 11:29:10 -0700 | [diff] [blame] | 40 | |
| 41 | |
| 42 | /** |
| 43 | * Client to use to make requests to a running server. |
| 44 | */ |
| 45 | var math_client; |
| 46 | |
| 47 | /** |
| 48 | * Server to test against |
| 49 | */ |
murgatroid99 | 2c641c6 | 2015-10-06 10:51:18 -0700 | [diff] [blame] | 50 | var getServer = require('./math/math_server.js'); |
murgatroid99 | 43a1479 | 2015-09-24 11:29:10 -0700 | [diff] [blame] | 51 | |
| 52 | var server = getServer(); |
| 53 | |
| 54 | describe('Async functionality', function() { |
| 55 | before(function(done) { |
| 56 | var port_num = server.bind('0.0.0.0:0', |
| 57 | grpc.ServerCredentials.createInsecure()); |
| 58 | server.start(); |
| 59 | math_client = new math.Math('localhost:' + port_num, |
murgatroid99 | be13d81 | 2015-10-09 14:45:30 -0700 | [diff] [blame] | 60 | grpc.credentials.createInsecure()); |
murgatroid99 | 43a1479 | 2015-09-24 11:29:10 -0700 | [diff] [blame] | 61 | done(); |
| 62 | }); |
| 63 | after(function() { |
| 64 | server.forceShutdown(); |
| 65 | }); |
| 66 | it('should not hang', function(done) { |
| 67 | var chunkCount=0; |
| 68 | var call = math_client.sum(function handleSumResult(err, value) { |
| 69 | assert.ifError(err); |
| 70 | assert.equal(value.num, chunkCount); |
| 71 | }); |
| 72 | |
| 73 | var path = require('path'); |
| 74 | var fs = require('fs'); |
| 75 | var fileToRead = path.join(__dirname, 'numbers.txt'); |
| 76 | var readStream = fs.createReadStream(fileToRead); |
| 77 | |
| 78 | readStream.once('readable', function () { |
| 79 | readStream.on('data', function (chunk) { |
| 80 | call.write({'num': 1}); |
| 81 | chunkCount += 1; |
| 82 | }); |
| 83 | |
| 84 | readStream.on('end', function () { |
| 85 | call.end(); |
| 86 | }); |
| 87 | |
| 88 | readStream.on('error', function (error) { |
murgatroid99 | 43a1479 | 2015-09-24 11:29:10 -0700 | [diff] [blame] | 89 | }); |
| 90 | }); |
| 91 | |
| 92 | call.on('status', function checkStatus(status) { |
| 93 | assert.strictEqual(status.code, grpc.status.OK); |
| 94 | done(); |
| 95 | }); |
| 96 | }); |
| 97 | }); |