math_client_test.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. 'use strict';
  34. var assert = require('assert');
  35. var grpc = require('..');
  36. var math = require('./math/math_pb');
  37. var MathClient = require('./math/math_grpc_pb').MathClient;
  38. /**
  39. * Client to use to make requests to a running server.
  40. */
  41. var math_client;
  42. /**
  43. * Server to test against
  44. */
  45. var getServer = require('./math/math_server.js');
  46. var server = getServer();
  47. describe('Math client', function() {
  48. before(function(done) {
  49. var port_num = server.bind('0.0.0.0:0',
  50. grpc.ServerCredentials.createInsecure());
  51. server.start();
  52. math_client = new MathClient('localhost:' + port_num,
  53. grpc.credentials.createInsecure());
  54. done();
  55. });
  56. after(function() {
  57. server.forceShutdown();
  58. });
  59. it('should handle a single request', function(done) {
  60. var arg = new math.DivArgs();
  61. arg.setDividend(7);
  62. arg.setDivisor(4);
  63. math_client.div(arg, function handleDivResult(err, value) {
  64. assert.ifError(err);
  65. assert.equal(value.getQuotient(), 1);
  66. assert.equal(value.getRemainder(), 3);
  67. done();
  68. });
  69. });
  70. it('should handle an error from a unary request', function(done) {
  71. var arg = new math.DivArgs();
  72. arg.setDividend(7);
  73. arg.setDivisor(0);
  74. math_client.div(arg, function handleDivResult(err, value) {
  75. assert(err);
  76. done();
  77. });
  78. });
  79. it('should handle a server streaming request', function(done) {
  80. var arg = new math.FibArgs();
  81. arg.setLimit(7);
  82. var call = math_client.fib(arg);
  83. var expected_results = [1, 1, 2, 3, 5, 8, 13];
  84. var next_expected = 0;
  85. call.on('data', function checkResponse(value) {
  86. assert.equal(value.getNum(), expected_results[next_expected]);
  87. next_expected += 1;
  88. });
  89. call.on('status', function checkStatus(status) {
  90. assert.strictEqual(status.code, grpc.status.OK);
  91. done();
  92. });
  93. });
  94. it('should handle a client streaming request', function(done) {
  95. var call = math_client.sum(function handleSumResult(err, value) {
  96. assert.ifError(err);
  97. assert.equal(value.getNum(), 21);
  98. });
  99. for (var i = 0; i < 7; i++) {
  100. var arg = new math.Num();
  101. arg.setNum(i);
  102. call.write(arg);
  103. }
  104. call.end();
  105. call.on('status', function checkStatus(status) {
  106. assert.strictEqual(status.code, grpc.status.OK);
  107. done();
  108. });
  109. });
  110. it('should handle a bidirectional streaming request', function(done) {
  111. function checkResponse(index, value) {
  112. assert.equal(value.getQuotient(), index);
  113. assert.equal(value.getRemainder(), 1);
  114. }
  115. var call = math_client.divMany();
  116. var response_index = 0;
  117. call.on('data', function(value) {
  118. checkResponse(response_index, value);
  119. response_index += 1;
  120. });
  121. for (var i = 0; i < 7; i++) {
  122. var arg = new math.DivArgs();
  123. arg.setDividend(2 * i + 1);
  124. arg.setDivisor(2);
  125. call.write(arg);
  126. }
  127. call.end();
  128. call.on('status', function checkStatus(status) {
  129. assert.strictEqual(status.code, grpc.status.OK);
  130. done();
  131. });
  132. });
  133. it('should handle an error from a bidi request', function(done) {
  134. var call = math_client.divMany();
  135. call.on('data', function(value) {
  136. assert.fail(value, undefined, 'Unexpected data response on failing call',
  137. '!=');
  138. });
  139. var arg = new math.DivArgs();
  140. arg.setDividend(7);
  141. arg.setDivisor(0);
  142. call.write(arg);
  143. call.end();
  144. call.on('error', function checkStatus(status) {
  145. done();
  146. });
  147. });
  148. });