timeAsyncMethodTests.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. (function() {
  2. var SandboxedModule, chai, expect, modulePath, path, should, sinon;
  3. require('coffee-script');
  4. chai = require('chai');
  5. should = chai.should();
  6. expect = chai.expect;
  7. path = require('path');
  8. modulePath = path.join(__dirname, '../../../timeAsyncMethod.coffee');
  9. SandboxedModule = require('sandboxed-module');
  10. sinon = require("sinon");
  11. describe('timeAsyncMethod', function() {
  12. beforeEach(function() {
  13. this.Timer = {
  14. done: sinon.stub()
  15. };
  16. this.TimerConstructor = sinon.stub().returns(this.Timer);
  17. this.metrics = {
  18. Timer: this.TimerConstructor
  19. };
  20. this.timeAsyncMethod = SandboxedModule.require(modulePath, {
  21. requires: {
  22. './metrics': this.metrics
  23. }
  24. });
  25. return this.testObject = {
  26. nextNumber: function(n, callback) {
  27. if (callback == null) {
  28. callback = function(err, result) {};
  29. }
  30. return setTimeout(function() {
  31. return callback(null, n + 1);
  32. }, 100);
  33. }
  34. };
  35. });
  36. it('should have the testObject behave correctly before wrapping', function(done) {
  37. return this.testObject.nextNumber(2, function(err, result) {
  38. expect(err).to.not.exist;
  39. expect(result).to.equal(3);
  40. return done();
  41. });
  42. });
  43. it('should wrap method without error', function(done) {
  44. this.timeAsyncMethod(this.testObject, 'nextNumber', 'test.nextNumber');
  45. return done();
  46. });
  47. it('should transparently wrap method invocation in timer', function(done) {
  48. this.timeAsyncMethod(this.testObject, 'nextNumber', 'test.nextNumber');
  49. return this.testObject.nextNumber(2, (function(_this) {
  50. return function(err, result) {
  51. expect(err).to.not.exist;
  52. expect(result).to.equal(3);
  53. expect(_this.TimerConstructor.callCount).to.equal(1);
  54. expect(_this.Timer.done.callCount).to.equal(1);
  55. return done();
  56. };
  57. })(this));
  58. });
  59. describe('when base method produces an error', function() {
  60. beforeEach(function() {
  61. return this.testObject.nextNumber = function(n, callback) {
  62. if (callback == null) {
  63. callback = function(err, result) {};
  64. }
  65. return setTimeout(function() {
  66. return callback(new Error('woops'));
  67. }, 100);
  68. };
  69. });
  70. return it('should propagate the error transparently', function(done) {
  71. this.timeAsyncMethod(this.testObject, 'nextNumber', 'test.nextNumber');
  72. return this.testObject.nextNumber(2, (function(_this) {
  73. return function(err, result) {
  74. expect(err).to.exist;
  75. expect(err).to.be["instanceof"](Error);
  76. expect(result).to.not.exist;
  77. return done();
  78. };
  79. })(this));
  80. });
  81. });
  82. describe('when a logger is supplied', function() {
  83. beforeEach(function() {
  84. return this.logger = {
  85. log: sinon.stub()
  86. };
  87. });
  88. return it('should also call logger.log', function(done) {
  89. this.timeAsyncMethod(this.testObject, 'nextNumber', 'test.nextNumber', this.logger);
  90. return this.testObject.nextNumber(2, (function(_this) {
  91. return function(err, result) {
  92. expect(err).to.not.exist;
  93. expect(result).to.equal(3);
  94. expect(_this.TimerConstructor.callCount).to.equal(1);
  95. expect(_this.Timer.done.callCount).to.equal(1);
  96. expect(_this.logger.log.callCount).to.equal(1);
  97. return done();
  98. };
  99. })(this));
  100. });
  101. });
  102. return describe('when the wrapper cannot be applied', function() {
  103. beforeEach(function() {});
  104. return it('should raise an error', function() {
  105. var badWrap;
  106. badWrap = (function(_this) {
  107. return function() {
  108. return _this.timeAsyncMethod(_this.testObject, 'DEFINITELY_NOT_A_REAL_METHOD', 'test.nextNumber');
  109. };
  110. })(this);
  111. return expect(badWrap).to["throw"](/^.*expected object property 'DEFINITELY_NOT_A_REAL_METHOD' to be a function.*$/);
  112. });
  113. });
  114. });
  115. }).call(this);