|
| 1 | +var test = require('tape') |
| 2 | +var Semaphore = require('../') |
| 3 | + |
| 4 | +function Phone () { |
| 5 | + this.state = 'free' |
| 6 | +} |
| 7 | + |
| 8 | +Phone.prototype.dial = function (callback) { |
| 9 | + if (this.state === 'busy') return callback(new Error('The phone is busy')) |
| 10 | + this.state = 'busy' |
| 11 | + setTimeout(function () { callback(null) }, 100) |
| 12 | +} |
| 13 | + |
| 14 | +Phone.prototype.hangup = function () { |
| 15 | + if (this.state === 'free') return callback(new Error('The phone is not in use')) |
| 16 | + this.state = 'free' |
| 17 | +} |
| 18 | + |
| 19 | +test('should not be using a bad example', function (t) { |
| 20 | + var phone = new Phone() |
| 21 | + |
| 22 | + // Call Bob |
| 23 | + phone.dial(function (err) { |
| 24 | + t.error(err) |
| 25 | + phone.hangup() |
| 26 | + }) |
| 27 | + |
| 28 | + // Cannot call Bret, because the phone is already busy with Bob. |
| 29 | + phone.dial(function (err) { |
| 30 | + t.same(err.message, 'The phone is busy') |
| 31 | + t.end() |
| 32 | + }) |
| 33 | +}) |
| 34 | + |
| 35 | +test('should not break the phone', function (t) { |
| 36 | + var phone = new Phone() |
| 37 | + var sem = new Semaphore(1) |
| 38 | + |
| 39 | + // Call Jane |
| 40 | + sem.take(function () { |
| 41 | + phone.dial(function (err) { |
| 42 | + t.error(err) |
| 43 | + phone.hangup() |
| 44 | + sem.leave() |
| 45 | + }) |
| 46 | + }) |
| 47 | + |
| 48 | + // Call Jon (will need to wait for call with Jane to complete) |
| 49 | + sem.take(function () { |
| 50 | + phone.dial(function (err) { |
| 51 | + t.error(err) |
| 52 | + phone.hangup() |
| 53 | + sem.leave() |
| 54 | + t.end() |
| 55 | + }) |
| 56 | + }) |
| 57 | +}) |
| 58 | + |
| 59 | +test('should not be slow', function (t) { |
| 60 | + var s = new Semaphore(3); |
| 61 | + var values = []; |
| 62 | + |
| 63 | + function push (n) { |
| 64 | + values.push(n) |
| 65 | + s.leave |
| 66 | + } |
| 67 | + |
| 68 | + push(1) |
| 69 | + push(2) |
| 70 | + push(3) |
| 71 | + push(4) |
| 72 | + push(5) |
| 73 | + |
| 74 | + t.same(values, [1, 2, 3, 4, 5]) |
| 75 | + t.end() |
| 76 | +}) |
| 77 | + |
| 78 | +test('should not let past more than capacity', function (t) { |
| 79 | + var s = new Semaphore(3) |
| 80 | + var values = [] |
| 81 | + var speed = 50 |
| 82 | + |
| 83 | + s.take(function () { values.push(1); setTimeout(function () { s.leave(); }, speed * 1); }) |
| 84 | + s.take(function () { values.push(2); setTimeout(function () { s.leave(); }, speed * 2); }) |
| 85 | + s.take(function () { values.push(3); setTimeout(function () { s.leave(); }, speed * 3); }) |
| 86 | + s.take(function () { values.push(4); }) |
| 87 | + s.take(function () { values.push(5); }) |
| 88 | + |
| 89 | + ;(function tick (n) { |
| 90 | + switch (n) { |
| 91 | + case 0: // After 0 sec |
| 92 | + console.log("0 seconds passed.") |
| 93 | + t.same(s._current, s._capacity) |
| 94 | + t.same(s._queue.length, 2) |
| 95 | + t.same(values, [1, 2, 3]) |
| 96 | + break |
| 97 | + case 1: // After 1 sec |
| 98 | + console.log("1 seconds passed.") |
| 99 | + t.same(s._current, s._capacity) |
| 100 | + t.same(s._queue.length, 1) |
| 101 | + t.same(values, [1, 2, 3, 4]) |
| 102 | + break |
| 103 | + case 2: // After 2 sec |
| 104 | + console.log("2 seconds passed.") |
| 105 | + t.same(s._current, s._capacity) |
| 106 | + t.same(s._queue.length, 0) |
| 107 | + t.same(values, [1, 2, 3, 4, 5]) |
| 108 | + break |
| 109 | + case 3: // After 3 sec |
| 110 | + console.log("3 seconds passed.") |
| 111 | + t.same(s._current, s._capacity - 1) |
| 112 | + t.same(s._queue.length, 0) |
| 113 | + t.same(values, [1, 2, 3, 4, 5]) |
| 114 | + t.end() |
| 115 | + break |
| 116 | + } |
| 117 | + |
| 118 | + if (n < 3) setTimeout(tick.bind(null, n + 1), speed * 1.1) |
| 119 | + })(0) |
| 120 | +}) |
| 121 | + |
| 122 | +test('should respect number', function (t) { |
| 123 | + t.test('should fail when taking more than the capacity allows', function (t) { |
| 124 | + var s = new Semaphore(1) |
| 125 | + t.throws(function () { |
| 126 | + s.take(2, function () {}) |
| 127 | + }, /^RangeError: expected number in \[1, 1\], got 2$/) |
| 128 | + t.end() |
| 129 | + }) |
| 130 | + |
| 131 | + t.test('should work fine with correct input values', function (t) { |
| 132 | + t.plan(3) |
| 133 | + |
| 134 | + var s = new Semaphore(10) // 10 |
| 135 | + s.take(5, function (leave) { // 5 |
| 136 | + t.pass() |
| 137 | + s.take(4, function () { // 1 |
| 138 | + leave(4) // 5 |
| 139 | + t.pass() |
| 140 | + |
| 141 | + s.take(5, function () { |
| 142 | + t.pass() |
| 143 | + }) // 0 |
| 144 | + }) |
| 145 | + }) |
| 146 | + |
| 147 | + setTimeout(t.end, 25) |
| 148 | + }) |
| 149 | +}) |
0 commit comments