No Description

test.js 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. 'use strict'
  2. var test = require('tape')
  3. var reusify = require('./')
  4. test('reuse objects', function (t) {
  5. t.plan(6)
  6. function MyObject () {
  7. t.pass('constructor called')
  8. this.next = null
  9. }
  10. var instance = reusify(MyObject)
  11. var obj = instance.get()
  12. t.notEqual(obj, instance.get(), 'two instance created')
  13. t.notOk(obj.next, 'next must be null')
  14. instance.release(obj)
  15. // the internals keeps a hot copy ready for reuse
  16. // putting this one back in the queue
  17. instance.release(instance.get())
  18. // comparing the old one with the one we got
  19. // never do this in real code, after release you
  20. // should never reuse that instance
  21. t.equal(obj, instance.get(), 'instance must be reused')
  22. })
  23. test('reuse more than 2 objects', function (t) {
  24. function MyObject () {
  25. t.pass('constructor called')
  26. this.next = null
  27. }
  28. var instance = reusify(MyObject)
  29. var obj = instance.get()
  30. var obj2 = instance.get()
  31. var obj3 = instance.get()
  32. t.notOk(obj.next, 'next must be null')
  33. t.notOk(obj2.next, 'next must be null')
  34. t.notOk(obj3.next, 'next must be null')
  35. t.notEqual(obj, obj2)
  36. t.notEqual(obj, obj3)
  37. t.notEqual(obj3, obj2)
  38. instance.release(obj)
  39. instance.release(obj2)
  40. instance.release(obj3)
  41. // skip one
  42. instance.get()
  43. var obj4 = instance.get()
  44. var obj5 = instance.get()
  45. var obj6 = instance.get()
  46. t.equal(obj4, obj)
  47. t.equal(obj5, obj2)
  48. t.equal(obj6, obj3)
  49. t.end()
  50. })