blob: c46e745116ecd2aa01540ca03121058f7b4f53cd [file] [log] [blame]
murgatroid9943a14792015-09-24 11:29:10 -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');
37
38var grpc = require('..');
Jan Tattermusch8ef43bd2015-12-07 09:55:22 -080039var math = grpc.load(__dirname + '/../../proto/math/math.proto').math;
murgatroid9943a14792015-09-24 11:29:10 -070040
41
42/**
43 * Client to use to make requests to a running server.
44 */
45var math_client;
46
47/**
48 * Server to test against
49 */
murgatroid992c641c62015-10-06 10:51:18 -070050var getServer = require('./math/math_server.js');
murgatroid9943a14792015-09-24 11:29:10 -070051
52var server = getServer();
53
54describe('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,
murgatroid99be13d812015-10-09 14:45:30 -070060 grpc.credentials.createInsecure());
murgatroid9943a14792015-09-24 11:29:10 -070061 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) {
murgatroid9943a14792015-09-24 11:29:10 -070089 });
90 });
91
92 call.on('status', function checkStatus(status) {
93 assert.strictEqual(status.code, grpc.status.OK);
94 done();
95 });
96 });
97});