blob: 40bb5f3bbde27aed6c096cb0fc5b78ed01234838 [file] [log] [blame]
murgatroid99749666e2015-01-12 18:25:58 -08001/*
2 *
3 * Copyright 2014, 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
murgatroid99e5061512015-01-12 18:14:35 -080034var assert = require('assert');
35var grpc = require('bindings')('grpc.node');
36var port_picker = require('../port_picker');
37
38/**
39 * This is used for testing functions with multiple asynchronous calls that
40 * can happen in different orders. This should be passed the number of async
41 * function invocations that can occur last, and each of those should call this
42 * function's return value
43 * @param {function()} done The function that should be called when a test is
44 * complete.
45 * @param {number} count The number of calls to the resulting function if the
46 * test passes.
47 * @return {function()} The function that should be called at the end of each
48 * sequence of asynchronous functions.
49 */
50function multiDone(done, count) {
51 return function() {
52 count -= 1;
53 if (count <= 0) {
54 done();
55 }
56 };
57}
58
59describe('end-to-end', function() {
60 it('should start and end a request without error', function(complete) {
61 port_picker.nextAvailablePort(function(port) {
62 var server = new grpc.Server();
63 var done = multiDone(function() {
64 complete();
65 server.shutdown();
66 }, 2);
67 server.addHttp2Port(port);
68 var channel = new grpc.Channel(port);
69 var deadline = new Date();
70 deadline.setSeconds(deadline.getSeconds() + 3);
71 var status_text = 'xyz';
72 var call = new grpc.Call(channel,
73 'dummy_method',
74 deadline);
75 call.startInvoke(function(event) {
76 assert.strictEqual(event.type,
77 grpc.completionType.INVOKE_ACCEPTED);
78
79 call.writesDone(function(event) {
80 assert.strictEqual(event.type,
81 grpc.completionType.FINISH_ACCEPTED);
82 assert.strictEqual(event.data, grpc.opError.OK);
83 });
84 },function(event) {
85 assert.strictEqual(event.type,
86 grpc.completionType.CLIENT_METADATA_READ);
87 },function(event) {
88 assert.strictEqual(event.type, grpc.completionType.FINISHED);
89 var status = event.data;
90 assert.strictEqual(status.code, grpc.status.OK);
91 assert.strictEqual(status.details, status_text);
92 done();
93 }, 0);
94
95 server.start();
96 server.requestCall(function(event) {
97 assert.strictEqual(event.type, grpc.completionType.SERVER_RPC_NEW);
98 var server_call = event.call;
99 assert.notEqual(server_call, null);
100 server_call.serverAccept(function(event) {
101 assert.strictEqual(event.type, grpc.completionType.FINISHED);
102 }, 0);
103 server_call.serverEndInitialMetadata(0);
104 server_call.startWriteStatus(
105 grpc.status.OK,
106 status_text,
107 function(event) {
108 assert.strictEqual(event.type,
109 grpc.completionType.FINISH_ACCEPTED);
110 assert.strictEqual(event.data, grpc.opError.OK);
111 done();
112 });
113 });
114 });
115 });
116
117 it('should send and receive data without error', function(complete) {
118 port_picker.nextAvailablePort(function(port) {
119 var req_text = 'client_request';
120 var reply_text = 'server_response';
121 var server = new grpc.Server();
122 var done = multiDone(function() {
123 complete();
124 server.shutdown();
125 }, 6);
126 server.addHttp2Port(port);
127 var channel = new grpc.Channel(port);
128 var deadline = new Date();
129 deadline.setSeconds(deadline.getSeconds() + 3);
130 var status_text = 'success';
131 var call = new grpc.Call(channel,
132 'dummy_method',
133 deadline);
134 call.startInvoke(function(event) {
135 assert.strictEqual(event.type,
136 grpc.completionType.INVOKE_ACCEPTED);
137 call.startWrite(
138 new Buffer(req_text),
139 function(event) {
140 assert.strictEqual(event.type,
141 grpc.completionType.WRITE_ACCEPTED);
142 assert.strictEqual(event.data, grpc.opError.OK);
143 call.writesDone(function(event) {
144 assert.strictEqual(event.type,
145 grpc.completionType.FINISH_ACCEPTED);
146 assert.strictEqual(event.data, grpc.opError.OK);
147 done();
148 });
149 }, 0);
150 call.startRead(function(event) {
151 assert.strictEqual(event.type, grpc.completionType.READ);
152 assert.strictEqual(event.data.toString(), reply_text);
153 done();
154 });
155 },function(event) {
156 assert.strictEqual(event.type,
157 grpc.completionType.CLIENT_METADATA_READ);
158 done();
159 },function(event) {
160 assert.strictEqual(event.type, grpc.completionType.FINISHED);
161 var status = event.data;
162 assert.strictEqual(status.code, grpc.status.OK);
163 assert.strictEqual(status.details, status_text);
164 done();
165 }, 0);
166
167 server.start();
168 server.requestCall(function(event) {
169 assert.strictEqual(event.type, grpc.completionType.SERVER_RPC_NEW);
170 var server_call = event.call;
171 assert.notEqual(server_call, null);
172 server_call.serverAccept(function(event) {
173 assert.strictEqual(event.type, grpc.completionType.FINISHED);
174 done();
175 });
176 server_call.serverEndInitialMetadata(0);
177 server_call.startRead(function(event) {
178 assert.strictEqual(event.type, grpc.completionType.READ);
179 assert.strictEqual(event.data.toString(), req_text);
180 server_call.startWrite(
181 new Buffer(reply_text),
182 function(event) {
183 assert.strictEqual(event.type,
184 grpc.completionType.WRITE_ACCEPTED);
185 assert.strictEqual(event.data,
186 grpc.opError.OK);
187 server_call.startWriteStatus(
188 grpc.status.OK,
189 status_text,
190 function(event) {
191 assert.strictEqual(event.type,
192 grpc.completionType.FINISH_ACCEPTED);
193 assert.strictEqual(event.data, grpc.opError.OK);
194 done();
195 });
196 }, 0);
197 });
198 });
199 });
200 });
201});