_.barrier + tests

This commit is contained in:
Sam Gentle
2011-04-14 07:19:05 +10:00
parent 1fc7d4b049
commit 217a6a6a63
5 changed files with 95 additions and 64 deletions

View File

@@ -137,4 +137,22 @@ $(document).ready(function() {
equals(composed('moe'), 'hi: moe!', 'in this case, the functions are also commutative');
});
test("functions: barrier", function() {
var testBarrier = function(barrierAmount, timesCalled) {
var barrierCalled = 0;
var barrier = _.barrier(function(){ barrierCalled++; }, barrierAmount);
while (timesCalled--) {
barrier();
}
return barrierCalled;
}
equals(testBarrier(5, 5), 1, "barrier(N) should fire after being called N times");
equals(testBarrier(5, 4), 0, "barrier(N) should not fire unless called N times");
equals(testBarrier(5, 6), 1, "barrier(N) should fire only once even if called more than N times");
});
});