select.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* jshint loopfunc:true */
  2. /* global describe:true, beforeEach:true, afterEach:true, it:true */
  3. var chan = require('..')
  4. var expect = require('expect.js')
  5. describe('Channel select', function () {
  6. var random
  7. beforeEach(function (done) {
  8. // save Math.random
  9. random = Math.random
  10. done()
  11. })
  12. afterEach(function (done) {
  13. // restore Math.random
  14. Math.random = random
  15. done()
  16. })
  17. it(
  18. 'should be able to select on channels',
  19. function (done) {
  20. var ch1 = chan()
  21. var ch2 = chan()
  22. chan.select(ch1, ch2)(function (err, ch) {
  23. expect(ch).to.equal(ch2)
  24. ch2.selected(function (err, val) {
  25. expect(val).to.equal(42)
  26. done()
  27. })
  28. })
  29. ch2(42)
  30. }
  31. )
  32. it(
  33. 'should be able to select on multiple channels',
  34. function (done) {
  35. var chs = [chan(), chan()]
  36. var remaining = chs.length
  37. chs.forEach(function (needle, i) {
  38. chan.select.apply(null, chs)(function (err, ch) {
  39. expect(ch).to.equal(needle)
  40. ch.selected(function (err, val) {
  41. expect(val).to.equal(i*10)
  42. if (--remaining === 0) {
  43. done()
  44. }
  45. })
  46. })
  47. })
  48. chs.forEach(function (ch, i) {
  49. ch(i*10)
  50. })
  51. }
  52. )
  53. it(
  54. 'should be able to select with queued messages',
  55. function (done) {
  56. var chs = [chan(), chan()]
  57. var remaining = chs.length
  58. var i = -1
  59. while (++i < 10) {
  60. (function (i) {
  61. chan.select.apply(null, chs)(function (err, ch) {
  62. expect(ch).to.equal(chs[0])
  63. ch.selected(function (err, val) {
  64. expect(val).to.equal(i * 10)
  65. if (--remaining === 0) {
  66. done()
  67. }
  68. })
  69. })
  70. })(i)
  71. }
  72. var j = -1
  73. while (++j < 10) {
  74. chs[0](j * 10)
  75. }
  76. }
  77. )
  78. it(
  79. 'should be able to select with existing messages on the channels',
  80. function (done) {
  81. var ch1 = chan()
  82. var ch2 = chan()
  83. ch2(42)
  84. chan.select(ch1, ch2)(function (err, ch) {
  85. expect(ch).to.equal(ch2)
  86. ch2.selected(function (err, val) {
  87. expect(val).to.equal(42)
  88. done()
  89. })
  90. })
  91. }
  92. )
  93. it(
  94. 'should randomly choose a channel to return with multiple full channels',
  95. function (done) {
  96. var ch1 = chan()
  97. var ch2 = chan()
  98. // force the random selection to be the second channel
  99. Math.random = function () { return 0.5 }
  100. // fill up both the channels
  101. ch1(21)
  102. ch2(42)
  103. // random selection should choose the second channel "randomly"
  104. chan.select(ch1, ch2)(function (err, ch) {
  105. expect(ch).to.equal(ch2)
  106. ch2.selected(function (err, val) {
  107. expect(val).to.equal(42)
  108. done()
  109. })
  110. })
  111. }
  112. )
  113. it (
  114. 'should wait for previously queued callbacks before selecting',
  115. function (done) {
  116. var ch1 = chan()
  117. var ch2 = chan()
  118. // queue a callback for ch1
  119. ch1(function () {})
  120. chan.select(ch1, ch2)(function (err, ch) {
  121. expect(ch).to.be(ch2)
  122. done()
  123. })
  124. ch1(74)
  125. ch2(47)
  126. }
  127. )
  128. })