test.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. var test = require('tap').test
  2. var inf = require('./inflight.js')
  3. function req (key, cb) {
  4. cb = inf(key, cb)
  5. if (cb) setTimeout(function () {
  6. cb(key)
  7. cb(key)
  8. })
  9. return cb
  10. }
  11. test('basic', function (t) {
  12. var calleda = false
  13. var a = req('key', function (k) {
  14. t.notOk(calleda)
  15. calleda = true
  16. t.equal(k, 'key')
  17. if (calledb) t.end()
  18. })
  19. t.ok(a, 'first returned cb function')
  20. var calledb = false
  21. var b = req('key', function (k) {
  22. t.notOk(calledb)
  23. calledb = true
  24. t.equal(k, 'key')
  25. if (calleda) t.end()
  26. })
  27. t.notOk(b, 'second should get falsey inflight response')
  28. })
  29. test('timing', function (t) {
  30. var expect = [
  31. 'method one',
  32. 'start one',
  33. 'end one',
  34. 'two',
  35. 'tick',
  36. 'three'
  37. ]
  38. var i = 0
  39. function log (m) {
  40. t.equal(m, expect[i], m + ' === ' + expect[i])
  41. ++i
  42. if (i === expect.length)
  43. t.end()
  44. }
  45. function method (name, cb) {
  46. log('method ' + name)
  47. process.nextTick(cb)
  48. }
  49. var one = inf('foo', function () {
  50. log('start one')
  51. var three = inf('foo', function () {
  52. log('three')
  53. })
  54. if (three) method('three', three)
  55. log('end one')
  56. })
  57. method('one', one)
  58. var two = inf('foo', function () {
  59. log('two')
  60. })
  61. if (two) method('one', two)
  62. process.nextTick(log.bind(null, 'tick'))
  63. })
  64. test('parameters', function (t) {
  65. t.plan(8)
  66. var a = inf('key', function (first, second, third) {
  67. t.equal(first, 1)
  68. t.equal(second, 2)
  69. t.equal(third, 3)
  70. })
  71. t.ok(a, 'first returned cb function')
  72. var b = inf('key', function (first, second, third) {
  73. t.equal(first, 1)
  74. t.equal(second, 2)
  75. t.equal(third, 3)
  76. })
  77. t.notOk(b, 'second should get falsey inflight response')
  78. setTimeout(function () {
  79. a(1, 2, 3)
  80. })
  81. })