math_client_test.js 4.6 KB

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