root / HServer / 00.Server / 00.Program / node_modules / pend / test.js
이력 | 보기 | 이력해설 | 다운로드 (2.39 KB)
| 1 | 39 | HKM | var assert = require('assert'); |
|---|---|---|---|
| 2 | var Pend = require('./'); |
||
| 3 | |||
| 4 | var tests = [
|
||
| 5 | {
|
||
| 6 | name: "basic", |
||
| 7 | fn: testBasic,
|
||
| 8 | }, |
||
| 9 | {
|
||
| 10 | name: "max", |
||
| 11 | fn: testWithMax,
|
||
| 12 | }, |
||
| 13 | {
|
||
| 14 | name: "callback twice", |
||
| 15 | fn: testCallbackTwice,
|
||
| 16 | }, |
||
| 17 | {
|
||
| 18 | name: "calling wait twice", |
||
| 19 | fn: testCallingWaitTwice,
|
||
| 20 | }, |
||
| 21 | {
|
||
| 22 | name: "hold()", |
||
| 23 | fn: testHoldFn,
|
||
| 24 | }, |
||
| 25 | ]; |
||
| 26 | var testCount = tests.length;
|
||
| 27 | |||
| 28 | doOneTest(); |
||
| 29 | |||
| 30 | function doOneTest() { |
||
| 31 | var test = tests.shift();
|
||
| 32 | if (!test) {
|
||
| 33 | console.log(testCount + " tests passed.");
|
||
| 34 | return;
|
||
| 35 | } |
||
| 36 | process.stdout.write(test.name + "...");
|
||
| 37 | test.fn(function() {
|
||
| 38 | process.stdout.write("OK\n");
|
||
| 39 | doOneTest(); |
||
| 40 | }); |
||
| 41 | } |
||
| 42 | |||
| 43 | function testBasic(cb) { |
||
| 44 | var pend = new Pend(); |
||
| 45 | var results = [];
|
||
| 46 | pend.go(function(cb) {
|
||
| 47 | results.push(1);
|
||
| 48 | setTimeout(function() {
|
||
| 49 | results.push(3);
|
||
| 50 | cb(); |
||
| 51 | }, 500);
|
||
| 52 | }); |
||
| 53 | pend.go(function(cb) {
|
||
| 54 | results.push(2);
|
||
| 55 | setTimeout(function() {
|
||
| 56 | results.push(4);
|
||
| 57 | cb(); |
||
| 58 | }, 1000);
|
||
| 59 | }); |
||
| 60 | pend.wait(function(err) {
|
||
| 61 | assert.deepEqual(results, [1,2,3,4]); |
||
| 62 | cb(); |
||
| 63 | }); |
||
| 64 | assert.deepEqual(results, [1, 2]); |
||
| 65 | } |
||
| 66 | |||
| 67 | function testWithMax(cb) { |
||
| 68 | var pend = new Pend(); |
||
| 69 | var results = [];
|
||
| 70 | pend.max = 2;
|
||
| 71 | pend.go(function(cb) {
|
||
| 72 | results.push('a');
|
||
| 73 | setTimeout(function() {
|
||
| 74 | results.push(1);
|
||
| 75 | cb(); |
||
| 76 | }, 500);
|
||
| 77 | }); |
||
| 78 | pend.go(function(cb) {
|
||
| 79 | results.push('b');
|
||
| 80 | setTimeout(function() {
|
||
| 81 | results.push(1);
|
||
| 82 | cb(); |
||
| 83 | }, 500);
|
||
| 84 | }); |
||
| 85 | pend.go(function(cb) {
|
||
| 86 | results.push('c');
|
||
| 87 | setTimeout(function() {
|
||
| 88 | results.push(2);
|
||
| 89 | cb(); |
||
| 90 | }, 100);
|
||
| 91 | }); |
||
| 92 | pend.wait(function(err) {
|
||
| 93 | assert.deepEqual(results, ['a', 'b', 1, 'c', 1, 2]); |
||
| 94 | cb(); |
||
| 95 | }); |
||
| 96 | assert.deepEqual(results, ['a', 'b']); |
||
| 97 | } |
||
| 98 | |||
| 99 | function testCallbackTwice(cb) { |
||
| 100 | var pend = new Pend(); |
||
| 101 | pend.go(function(cb) {
|
||
| 102 | setTimeout(cb, 100);
|
||
| 103 | }); |
||
| 104 | pend.go(function(cb) {
|
||
| 105 | cb(); |
||
| 106 | assert.throws(cb, /callback called twice/); |
||
| 107 | }); |
||
| 108 | pend.wait(cb); |
||
| 109 | } |
||
| 110 | |||
| 111 | function testCallingWaitTwice(cb) { |
||
| 112 | var pend = new Pend(); |
||
| 113 | pend.go(function(cb) {
|
||
| 114 | setTimeout(cb, 100);
|
||
| 115 | }); |
||
| 116 | pend.wait(function() {
|
||
| 117 | pend.go(function(cb) {
|
||
| 118 | setTimeout(cb, 50);
|
||
| 119 | }); |
||
| 120 | pend.go(function(cb) {
|
||
| 121 | setTimeout(cb, 10);
|
||
| 122 | }); |
||
| 123 | pend.go(function(cb) {
|
||
| 124 | setTimeout(cb, 20);
|
||
| 125 | }); |
||
| 126 | pend.wait(cb); |
||
| 127 | }); |
||
| 128 | } |
||
| 129 | |||
| 130 | function testHoldFn(cb) { |
||
| 131 | var pend = new Pend(); |
||
| 132 | setTimeout(pend.hold(), 100);
|
||
| 133 | pend.go(function(cb) {
|
||
| 134 | cb(); |
||
| 135 | }); |
||
| 136 | pend.wait(cb); |
||
| 137 | } |