Skip to content

Commit ceed16f

Browse files
committed
Update tests
1 parent e4c8a25 commit ceed16f

File tree

4 files changed

+155
-168
lines changed

4 files changed

+155
-168
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
;(function(global) {
1+
;(function (global) {
22
'use strict'
33

44
var nextTick = function (fn) { setTimeout(fn, 0) }

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,10 @@
2626
"nyc": "^6.4.3",
2727
"standard": "^7.0.1",
2828
"tape": "^4.5.1"
29+
},
30+
"standard": {
31+
"globals": [
32+
"define"
33+
]
2934
}
3035
}

test/index.js

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
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 (callback) {
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 (leave) { values.push(1); setTimeout(leave, speed * 1) })
84+
s.take(function (leave) { values.push(2); setTimeout(leave, speed * 2) })
85+
s.take(function (leave) { values.push(3); setTimeout(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+
})

test/semaphore.js

Lines changed: 0 additions & 167 deletions
This file was deleted.

0 commit comments

Comments
 (0)