math_client_test.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. *
  3. * Copyright 2015 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. 'use strict';
  19. var assert = require('assert');
  20. var grpc = require('..');
  21. var math = require('./math/math_pb');
  22. var MathClient = require('./math/math_grpc_pb').MathClient;
  23. /**
  24. * Client to use to make requests to a running server.
  25. */
  26. var math_client;
  27. /**
  28. * Server to test against
  29. */
  30. var getServer = require('./math/math_server.js');
  31. var server = getServer();
  32. describe('Math client', function() {
  33. before(function(done) {
  34. var port_num = server.bind('0.0.0.0:0',
  35. grpc.ServerCredentials.createInsecure());
  36. server.start();
  37. math_client = new MathClient('localhost:' + port_num,
  38. grpc.credentials.createInsecure());
  39. done();
  40. });
  41. after(function() {
  42. server.forceShutdown();
  43. });
  44. it('should handle a single request', function(done) {
  45. var arg = new math.DivArgs();
  46. arg.setDividend(7);
  47. arg.setDivisor(4);
  48. math_client.div(arg, function handleDivResult(err, value) {
  49. assert.ifError(err);
  50. assert.equal(value.getQuotient(), 1);
  51. assert.equal(value.getRemainder(), 3);
  52. done();
  53. });
  54. });
  55. it('should handle an error from a unary request', function(done) {
  56. var arg = new math.DivArgs();
  57. arg.setDividend(7);
  58. arg.setDivisor(0);
  59. math_client.div(arg, function handleDivResult(err, value) {
  60. assert(err);
  61. done();
  62. });
  63. });
  64. it('should handle a server streaming request', function(done) {
  65. var arg = new math.FibArgs();
  66. arg.setLimit(7);
  67. var call = math_client.fib(arg);
  68. var expected_results = [1, 1, 2, 3, 5, 8, 13];
  69. var next_expected = 0;
  70. call.on('data', function checkResponse(value) {
  71. assert.equal(value.getNum(), expected_results[next_expected]);
  72. next_expected += 1;
  73. });
  74. call.on('status', function checkStatus(status) {
  75. assert.strictEqual(status.code, grpc.status.OK);
  76. done();
  77. });
  78. });
  79. it('should handle a client streaming request', function(done) {
  80. var call = math_client.sum(function handleSumResult(err, value) {
  81. assert.ifError(err);
  82. assert.equal(value.getNum(), 21);
  83. });
  84. for (var i = 0; i < 7; i++) {
  85. var arg = new math.Num();
  86. arg.setNum(i);
  87. call.write(arg);
  88. }
  89. call.end();
  90. call.on('status', function checkStatus(status) {
  91. assert.strictEqual(status.code, grpc.status.OK);
  92. done();
  93. });
  94. });
  95. it('should handle a bidirectional streaming request', function(done) {
  96. function checkResponse(index, value) {
  97. assert.equal(value.getQuotient(), index);
  98. assert.equal(value.getRemainder(), 1);
  99. }
  100. var call = math_client.divMany();
  101. var response_index = 0;
  102. call.on('data', function(value) {
  103. checkResponse(response_index, value);
  104. response_index += 1;
  105. });
  106. for (var i = 0; i < 7; i++) {
  107. var arg = new math.DivArgs();
  108. arg.setDividend(2 * i + 1);
  109. arg.setDivisor(2);
  110. call.write(arg);
  111. }
  112. call.end();
  113. call.on('status', function checkStatus(status) {
  114. assert.strictEqual(status.code, grpc.status.OK);
  115. done();
  116. });
  117. });
  118. it('should handle an error from a bidi request', function(done) {
  119. var call = math_client.divMany();
  120. call.on('data', function(value) {
  121. assert.fail(value, undefined, 'Unexpected data response on failing call',
  122. '!=');
  123. });
  124. var arg = new math.DivArgs();
  125. arg.setDividend(7);
  126. arg.setDivisor(0);
  127. call.write(arg);
  128. call.end();
  129. call.on('error', function checkStatus(status) {
  130. done();
  131. });
  132. });
  133. });