You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
2.7 KiB

  1. var assert = require('assert'),
  2. cb = require('../');
  3. function invokeAsync(callback) {
  4. setTimeout(function() {
  5. callback(null, 'foo');
  6. }, 100);
  7. }
  8. function invokeAsyncError(callback) {
  9. setTimeout(function() {
  10. callback(new Error());
  11. }, 100);
  12. }
  13. function invokeAsyncTwice(callback) {
  14. setTimeout(function() {
  15. callback(null, 'foo');
  16. callback(null, 'foo');
  17. }, 100);
  18. }
  19. describe('cb(callback)', function() {
  20. it('should invoke the provided callback', function(done) {
  21. invokeAsync(cb(function(err, res) {
  22. assert.strictEqual(res, 'foo');
  23. done();
  24. }));
  25. });
  26. it('shouldn\'t mess with errors', function(done) {
  27. invokeAsyncError(cb(function(err, res) {
  28. assert(err);
  29. done();
  30. }));
  31. });
  32. it('should allow multiple executions', function(done) {
  33. var count = 0;
  34. invokeAsyncTwice(cb(function(err, res) {
  35. count++;
  36. if (count === 2) done();
  37. }));
  38. });
  39. });
  40. describe('cb(callback).timeout(ms)', function() {
  41. it('should complete successfully within timeout period', function(done) {
  42. invokeAsync(cb(function(err, res) {
  43. assert.strictEqual(res, 'foo');
  44. done();
  45. }).timeout(200));
  46. });
  47. it('should complete with an error after timeout period', function(done) {
  48. invokeAsync(cb(function(err, res) {
  49. assert(err);
  50. done();
  51. }).timeout(50));
  52. });
  53. it('error resulting from a timeout should be instanceof cb.TimeoutError', function(done) {
  54. invokeAsync(cb(function(err, res) {
  55. assert(err instanceof cb.TimeoutError);
  56. done();
  57. }).timeout(50));
  58. });
  59. });
  60. describe('cb(callback).error(errback)', function() {
  61. it('should skip the err argument when invoking callback', function(done) {
  62. invokeAsync(cb(function(res) {
  63. assert.strictEqual(res, 'foo');
  64. done();
  65. }).error(assert.ifError));
  66. });
  67. it('should pass errors to provided errback', function(done) {
  68. invokeAsyncError(cb(function(res) {
  69. throw new Error('should not be invoked');
  70. }).error(function(err) {
  71. assert(err);
  72. done();
  73. }));
  74. });
  75. });
  76. describe('cb(callback).error(errback).timeout(ms)', function() {
  77. it('should skip the err argument when invoking callback', function(done) {
  78. invokeAsync(cb(function(res) {
  79. assert.strictEqual(res, 'foo');
  80. done();
  81. }).error(assert.ifError).timeout(200));
  82. });
  83. it('should pass timeout error to provided errback', function(done) {
  84. invokeAsyncError(cb(function(res) {
  85. throw new Error('should not be invoked');
  86. }).error(function(err) {
  87. assert(err);
  88. done();
  89. }).timeout(50));
  90. });
  91. });
  92. describe('cb(callback).once()', function() {
  93. it('should allow multiple executions', function(done) {
  94. var count = 0;
  95. invokeAsyncTwice(cb(function(err, res) {
  96. count++;
  97. assert.notEqual(count, 2);
  98. setTimeout(done, 100);
  99. }).once());
  100. });
  101. });