Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
  • Loading branch information
CatChen committed Dec 23, 2010
2 parents 3d50e7f + b608d4b commit 04d0c73
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 18 deletions.
5 changes: 4 additions & 1 deletion src/async.js
Expand Up @@ -79,9 +79,12 @@
});
while (callbackQueue.length > 0) {
innerChain.next(callbackQueue.shift());
innerChain.next(function(result) {
self.result = result;
return result;
});
}
innerChain.next(function(result) {
self.result = result;
self.state = "completed";
self.completed = true;
return result;
Expand Down
159 changes: 142 additions & 17 deletions test/javascripts/async.js
Expand Up @@ -26,7 +26,7 @@ function testAsync() {
module("async operation");

test("async operation callback", function() {
expect(6);
expect(9);

var testValue = "-- this is the callback result --";
var asyncFunctionDelay = 200;
Expand Down Expand Up @@ -57,10 +57,41 @@ function testAsync() {
}, asyncFunctionDelay * 2)

setTimeout(function() {
equals(operation.result, testValue, "operation.result after yield");
equals(operation.state, "completed", "operation.state after yield");
equals(operation.completed, true, "operation.completed after yield");
start();
}, asyncFunctionDelay * 3)
});

test("async operation callback without yield value", function() {
expect(5);

var testValue = "-- this is the callback result --";
var asyncFunctionDelay = 200;
var syncFunctionDelay = 100;
var asyncFunction = function(value) {
var operation = new Async.Operation();
setTimeout(function() { operation.yield(); }, asyncFunctionDelay);
return operation;
};

stop();

var operation = asyncFunction(testValue)
.addCallback(function(result) {
ok(true, "callback called");
equals(result, undefined, "callback result");
})

setTimeout(function() {
equals(operation.result, undefined, "operation.result after yield");
equals(operation.state, "completed", "operation.state after yield");
equals(operation.completed, true, "operation.completed after yield");
start();
}, asyncFunctionDelay)
});

test("async operation callback without yield", function() {
expect(3);

Expand All @@ -83,6 +114,30 @@ function testAsync() {
start();
});

test("async operation without callback", function() {
expect(3);

var testValue = "-- this is the callback result --";
var asyncFunctionDelay = 200;
var syncFunctionDelay = 100;
var asyncFunction = function(value) {
var operation = new Async.Operation();
setTimeout(function() { operation.yield(value); }, asyncFunctionDelay);
return operation;
};

stop();

var operation = asyncFunction(testValue);

setTimeout(function() {
equals(operation.result, testValue, "operation.result after yield");
equals(operation.state, "completed", "operation.state after yield");
equals(operation.completed, true, "operation.completed after yield");
start();
}, asyncFunctionDelay)
});

test("async operation object", function() {
expect(15);

Expand Down Expand Up @@ -192,22 +247,6 @@ function testAsync() {

module("async chain");

test("sync chain go only operation", function() {
expect(1);

var syncFunctionDelay = 100;

stop();

var chain = Async
.go(0);

setTimeout(function() {
equals(chain.result, 0, "sync chain result")
start();
}, syncFunctionDelay);
});

test("sync chain go first operation", function() {
expect(4);

Expand Down Expand Up @@ -327,6 +366,24 @@ function testAsync() {
}, asyncFunctionDelay + syncFunctionDelay);
});

test("sync chain go only operation", function() {
expect(3);

var syncFunctionDelay = 100;

stop();

var chain = Async
.go(0);

setTimeout(function() {
equals(chain.result, 0, "chain.result after go")
equals(chain.state, "completed", "chain.state after go");
equals(chain.completed, true, "chain.completed after go");
start();
}, syncFunctionDelay);
});

test("async chain go first operation", function() {
expect(4);

Expand Down Expand Up @@ -648,7 +705,73 @@ function testAsync() {

setTimeout(function() {
start();
}, asyncFunctionDelay * 3 + syncFunctionDelay);
});

test("hybrid chain object", function() {
expect(18);

var asyncFunctionDelay = 200;
var syncFunctionDelay = 100;

stop();

var chain = Async
.chain(function(i) {
var operation = new Async.Operation();
setTimeout(function() { operation.yield(i + 1); }, asyncFunctionDelay);
return operation;
})
.next(function(i) {
return i + 1;
})
.next(function(i) {
var operation = new Async.Operation();
setTimeout(function() { operation.yield(i + 1); }, asyncFunctionDelay);
return operation;
});

setTimeout(function() {
chain.go(0);
}, asyncFunctionDelay);

equals(chain.result, undefined, "chain.result at the beginning")
equals(chain.state, "waiting", "chain.state at the beginning");
equals(chain.completed, false, "chain.completed at the beginning");

setTimeout(function() {
equals(chain.result, undefined, "chain.result before go");
equals(chain.state, "waiting", "chain.state before go");
equals(chain.completed, false, "chain.completed before go");
}, asyncFunctionDelay - 1);

setTimeout(function() {
equals(chain.result, 0, "chain.result after go");
equals(chain.state, "chain running", "chain.state after go");
equals(chain.completed, false, "chain.completed after go");
}, asyncFunctionDelay + 1);

setTimeout(function() {
equals(chain.result, 0, "chain.result before first next");
equals(chain.state, "chain running", "chain.state before first next");
equals(chain.completed, false, "chain.completed before first next");
}, asyncFunctionDelay * 2 - 1);

setTimeout(function() {
equals(chain.result, 2, "chain.result after second next");
equals(chain.state, "chain running", "chain.state after second next");
equals(chain.completed, false, "chain.completed after second next");
}, asyncFunctionDelay * 2 + syncFunctionDelay);

setTimeout(function() {
equals(chain.result, 3, "chain.result after third next");
equals(chain.state, "completed", "chain.state after third next");
equals(chain.completed, true, "chain.completed after third next");
}, asyncFunctionDelay * 3 + syncFunctionDelay);

setTimeout(function() {
start();
}, asyncFunctionDelay * 4);
});

test("async chain wait operation", function() {
Expand Down Expand Up @@ -759,6 +882,8 @@ function testAsync() {
}, asyncFunctionDelay * 2 + syncFunctionDelay)
});

module("async helpers")

test("sync to async conversion", function() {
expect(8);

Expand Down

0 comments on commit 04d0c73

Please sign in to comment.