countDownLatch.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. var CountDownLatch = require('../../lib/util/countDownLatch');
  2. var should = require('should');
  3. var cbCreator = (function() {
  4. var count =0;
  5. return {
  6. callback: function() {
  7. count++;
  8. },
  9. getCount: function() {
  10. return count;
  11. },
  12. count: count
  13. };
  14. })();
  15. describe('countdown latch test', function() {
  16. var countDownLatch1;
  17. var countDownLatch2;
  18. describe('#count down', function() {
  19. it('should invoke the callback after the done method was invoked the specified times', function(done) {
  20. var n = 3, doneCount = 0;
  21. var cdl = CountDownLatch.createCountDownLatch(n, function() {
  22. doneCount.should.equal(n);
  23. done();
  24. });
  25. for(var i=0; i<n; i++) {
  26. doneCount++;
  27. cdl.done();
  28. }
  29. });
  30. it('should throw exception if pass a negative or zero to the create method', function() {
  31. (function() {
  32. CountDownLatch.createCountDownLatch(-1, function() {});
  33. }).should.throw();
  34. (function() {
  35. CountDownLatch.createCountDownLatch(0, function() {});
  36. }).should.throw();
  37. });
  38. it('should throw exception if pass illegal cb to the create method', function() {
  39. (function() {
  40. CountDownLatch.createCountDownLatch(1, null);
  41. }).should.throw();
  42. });
  43. it('should throw exception if try to invoke done metho of a latch that has fired cb', function() {
  44. var n = 3;
  45. var cdl = CountDownLatch.createCountDownLatch(n, function() {});
  46. for(var i=0; i<n; i++) {
  47. cdl.done();
  48. }
  49. (function() {
  50. cdl.done();
  51. }).should.throw();
  52. });
  53. it('should invoke the callback if timeout', function() {
  54. var n = 3;
  55. var cdl = CountDownLatch.createCountDownLatch(n, {timeout: 3000}, function(isTimeout) {
  56. isTimeout.should.equal(true);
  57. });
  58. for(var i=0; i<n-1; i++) {
  59. cdl.done();
  60. }
  61. });
  62. });
  63. });