async_test.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 = grpc.load(__dirname + '/../../proto/math/math.proto').math;
  22. /**
  23. * Client to use to make requests to a running server.
  24. */
  25. var math_client;
  26. /**
  27. * Server to test against
  28. */
  29. var getServer = require('./math/math_server.js');
  30. var server = getServer();
  31. describe('Async functionality', function() {
  32. before(function(done) {
  33. var port_num = server.bind('0.0.0.0:0',
  34. grpc.ServerCredentials.createInsecure());
  35. server.start();
  36. math_client = new math.Math('localhost:' + port_num,
  37. grpc.credentials.createInsecure());
  38. done();
  39. });
  40. after(function() {
  41. grpc.closeClient(math_client);
  42. server.forceShutdown();
  43. });
  44. it('should not hang', function(done) {
  45. var chunkCount=0;
  46. var call = math_client.sum(function handleSumResult(err, value) {
  47. assert.ifError(err);
  48. assert.equal(value.num, chunkCount);
  49. });
  50. var path = require('path');
  51. var fs = require('fs');
  52. var fileToRead = path.join(__dirname, 'numbers.txt');
  53. var readStream = fs.createReadStream(fileToRead);
  54. readStream.once('readable', function () {
  55. readStream.on('data', function (chunk) {
  56. call.write({'num': 1});
  57. chunkCount += 1;
  58. });
  59. readStream.on('end', function () {
  60. call.end();
  61. });
  62. readStream.on('error', function (error) {
  63. });
  64. });
  65. call.on('status', function checkStatus(status) {
  66. assert.strictEqual(status.code, grpc.status.OK);
  67. done();
  68. });
  69. });
  70. });