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.

53 lines
1.4 KiB

  1. var t = require('assert');
  2. var RefCounter = require('../../lib/tools/ref-counter');
  3. function noop() {}
  4. module.exports = {
  5. 'RefCounter': {
  6. 'is an object': function() {
  7. t.equal(typeof(RefCounter), 'function');
  8. },
  9. 'should become active when incremented': function(next) {
  10. var refCounter = new RefCounter(function() { next(); }, noop);
  11. refCounter.increment();
  12. },
  13. 'should become inactive when incremented and decremented': function(next) {
  14. var refCounter = new RefCounter(noop, function() { next(); });
  15. refCounter.increment();
  16. setImmediate(function() {
  17. refCounter.decrement();
  18. });
  19. },
  20. 'should support multiple accesses': function(next) {
  21. var refCounter = new RefCounter(noop, function() { next(); });
  22. refCounter.increment();
  23. refCounter.increment();
  24. refCounter.decrement();
  25. setImmediate(function() {
  26. refCounter.decrement();
  27. });
  28. },
  29. 'should be reusable': function(next) {
  30. var numActives = 0;
  31. var numPassives = 0;
  32. var refCounter = new RefCounter(function() {
  33. numActives += 1;
  34. }, function() {
  35. numPassives += 1;
  36. if (numActives === 2 && numPassives === 2) {
  37. next();
  38. }
  39. });
  40. refCounter.increment();
  41. refCounter.decrement();
  42. refCounter.increment();
  43. refCounter.decrement();
  44. }
  45. }
  46. };