Skip to content

Instantly share code, notes, and snippets.

@fictorial
Created December 1, 2009 15:54
Show Gist options
  • Save fictorial/246385 to your computer and use it in GitHub Desktop.
Save fictorial/246385 to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
var promise_group = require("../promise_group"),
test = require("mjsunit"),
sys = require("sys");
var group = new promise_group.PromiseGroup();
test.assertTrue(group instanceof promise_group.PromiseGroup);
function test_single_pass() {
var group = new promise_group.PromiseGroup();
var p0 = new process.Promise();
group.add(p0, "p0");
group.addListener("one_done", function (name, outcome, result) {
test_single_pass_one_done_called = true;
test.assertEquals("p0", name);
test.assertEquals("ok", outcome);
test.assertEquals(42, result);
});
group.addListener("all_done", function (aggregate_results) {
test_single_pass_all_done_called = true;
test.assertTrue(aggregate_results.ok instanceof Object);
test.assertEquals([42], aggregate_results.ok.p0);
});
p0.emitSuccess(42);
}
function test_single_fail() {
var group = new promise_group.PromiseGroup();
var p0 = new process.Promise();
group.add(p0, "p0");
group.addListener("one_done", function (name, outcome, result) {
test_single_fail_one_done_called = true;
test.assertEquals("p0", name);
test.assertEquals("fail", outcome);
test.assertEquals("lpt1 on fire", result);
});
group.addListener("all_done", function (aggregate_results) {
test_single_fail_all_done_called = true;
test.assertTrue(aggregate_results.fail instanceof Object);
test.assertEquals("lpt1 on fire", aggregate_results.fail.p0);
});
p0.emitError("lpt1 on fire");
}
function test_single_cancel() {
var group = new promise_group.PromiseGroup();
var p0 = new process.Promise();
group.add(p0, "p0");
group.addListener("one_done", function (name, outcome, result) {
test_single_cancel_one_done_called = true;
test.assertEquals("p0", name);
test.assertEquals("cancel", outcome);
test.assertEquals("nevermind", result);
});
group.addListener("all_done", function (aggregate_results) {
test_single_cancel_all_done_called = true;
test.assertTrue(aggregate_results.cancel instanceof Object);
test.assertEquals("nevermind", aggregate_results.cancel.p0);
});
p0.emitCancel("nevermind");
}
function test_single_timeout() {
var group = new promise_group.PromiseGroup();
var p0 = new process.Promise();
group.add(p0, "p0");
group.addListener("one_done", function (name, outcome, result) {
test_single_timeout_one_done_called = true;
test.assertEquals("p0", name);
test.assertEquals("timeout", outcome);
test.assertEquals(null, result);
});
group.addListener("all_done", function (aggregate_results) {
test_single_timeout_all_done_called = true;
test.assertTrue(aggregate_results.timeout instanceof Object);
test.assertEquals(null, aggregate_results.timeout.p0);
});
p0.timeout(1);
// Do not emit success/error/cancel, let it timeout.
}
function test_single_pass_fail() {
var group = new promise_group.PromiseGroup();
var p0 = new process.Promise();
var p1 = new process.Promise();
group.add(p0, "p0");
group.add(p1, "p1");
group.addListener("one_done", function (name, outcome, result) {
test_single_pass_fail_one_done_called = true;
if (name === "p0") {
test.assertEquals("ok", outcome);
test.assertEquals(42, result);
} else if (name === "p1") {
test.assertEquals("fail", outcome);
test.assertEquals("lpt2 on fire", result);
} else {
test.assertFalse("unexpected name");
}
});
group.addListener("all_done", function (aggregate_results) {
test_single_pass_fail_all_done_called = true;
test.assertTrue(aggregate_results.ok instanceof Object);
test.assertTrue(aggregate_results.fail instanceof Object);
test.assertEquals([42], aggregate_results.ok.p0);
test.assertEquals("lpt2 on fire", aggregate_results.fail.p1);
});
p0.emitSuccess(42);
p1.emitError("lpt2 on fire");
}
function test_multi_pass_fail_cancel() {
var group = new promise_group.PromiseGroup();
var p0 = new process.Promise();
var p1 = new process.Promise();
var p2 = new process.Promise();
var p3 = new process.Promise();
group.add(p0, "p0");
group.add(p1, "p1");
group.add(p2, "p2");
group.add(p3, "p3");
group.addListener("one_done", function (name, outcome, result) {
if (typeof(test_multi_pass_fail_one_done_called) === 'undefined')
test_multi_pass_fail_one_done_called = 0;
test_multi_pass_fail_one_done_called++;
if (name === "p0") {
test.assertEquals("ok", outcome);
test.assertEquals(42, result);
} else if (name === "p1") {
test.assertEquals("fail", outcome);
test.assertEquals("lpt2 on fire", result);
} else if (name === "p2") {
test.assertEquals("cancel", outcome);
test.assertEquals("nevermind", result);
} else if (name === "p3") {
test.assertEquals("ok", outcome);
test.assertEquals(["a","b"], result);
} else {
test.assertFalse("unexpected name");
}
});
group.addListener("all_done", function (aggregate_results) {
test_multi_pass_fail_all_done_called = true;
test.assertTrue(aggregate_results.ok instanceof Object);
test.assertTrue(aggregate_results.fail instanceof Object);
test.assertTrue(aggregate_results.cancel instanceof Object);
test.assertEquals([42], aggregate_results.ok.p0);
test.assertEquals("lpt2 on fire", aggregate_results.fail.p1);
test.assertEquals("nevermind", aggregate_results.cancel.p2);
test.assertEquals(["a","b"], aggregate_results.ok.p3);
});
p0.emitSuccess(42);
p1.emitError("lpt2 on fire");
p2.emitCancel("nevermind");
p3.emitSuccess("a","b");
}
test_single_pass();
test_single_fail();
test_single_cancel();
test_single_timeout();
test_single_pass_fail();
test_multi_pass_fail_cancel();
process.addListener("exit", function () {
test.assertTrue(test_single_pass_one_done_called);
test.assertTrue(test_single_pass_all_done_called);
test.assertTrue(test_single_fail_one_done_called);
test.assertTrue(test_single_fail_all_done_called);
test.assertTrue(test_single_cancel_one_done_called);
test.assertTrue(test_single_cancel_all_done_called);
test.assertTrue(test_single_pass_fail_one_done_called);
test.assertTrue(test_single_pass_fail_all_done_called);
test.assertEquals(4, test_multi_pass_fail_one_done_called);
test.assertTrue(test_multi_pass_fail_all_done_called);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment