spec/util_async_spec.lua
author Matthew Wild <mwild1@gmail.com>
Fri, 16 Mar 2018 22:26:15 +0000
changeset 8612 9f6ab206d741
parent 8611 a2e6caf5848d
child 8617 bfbafeced0c4
permissions -rw-r--r--
util.async: Ensure runner is left in correct state after out-of-main-loop error (+tests)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8608
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     1
local async = require "util.async";
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     2
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     3
describe("util.async", function()
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     4
	local debug = false;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     5
	local print = print;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     6
	if debug then
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     7
		require "util.logger".add_simple_sink(print);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     8
	else
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     9
		print = function () end
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    10
	end
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    11
	local function new(func, name)
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    12
		local log = {};
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    13
		return async.runner(func, setmetatable({}, {
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    14
			__index = function (_, event)
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    15
				return function (runner, err)
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    16
					print(name, "event", event, err)
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    17
					print "--"
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    18
					table.insert(log, { event = event, err = err });
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    19
				end;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    20
			end;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    21
		})), log;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    22
	end
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    23
	describe("#runner", function()
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    24
		it("should work", function()			
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    25
			local r, l = new(function (item) assert(type(item) == "number") end);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    26
			r:run(1);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    27
			r:run(2);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    28
		end);
8610
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
    29
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
    30
		it("should be ready after creation", function ()
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
    31
			local r = async.runner(function (item) end);
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
    32
			assert.equal(r.state, "ready");
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
    33
		end);
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
    34
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
    35
		describe("#errors", function ()
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
    36
			local last_processed_item, last_error;
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
    37
			local r;
8612
9f6ab206d741 util.async: Ensure runner is left in correct state after out-of-main-loop error (+tests)
Matthew Wild <mwild1@gmail.com>
parents: 8611
diff changeset
    38
			local wait, done;
8610
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
    39
			r = async.runner(function (item)
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
    40
				if item == "error" then
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
    41
					error({ e = "test error" });
8612
9f6ab206d741 util.async: Ensure runner is left in correct state after out-of-main-loop error (+tests)
Matthew Wild <mwild1@gmail.com>
parents: 8611
diff changeset
    42
				elseif item == "wait" then
9f6ab206d741 util.async: Ensure runner is left in correct state after out-of-main-loop error (+tests)
Matthew Wild <mwild1@gmail.com>
parents: 8611
diff changeset
    43
					wait, done = async.waiter();
9f6ab206d741 util.async: Ensure runner is left in correct state after out-of-main-loop error (+tests)
Matthew Wild <mwild1@gmail.com>
parents: 8611
diff changeset
    44
					wait();
9f6ab206d741 util.async: Ensure runner is left in correct state after out-of-main-loop error (+tests)
Matthew Wild <mwild1@gmail.com>
parents: 8611
diff changeset
    45
					error({ e = "post wait error" });
8610
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
    46
				end
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
    47
				last_processed_item = item;
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
    48
			end, {
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
    49
				error = function (runner, err)
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
    50
					assert.equal(r, runner);
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
    51
					last_error = err;
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
    52
				end;
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
    53
			});
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
    54
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
    55
			randomize(false);
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
    56
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
    57
			it("should notify", function ()
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
    58
				local last_processed_item, last_error;
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
    59
				local r;
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
    60
				r = async.runner(function (item)
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
    61
					if item == "error" then
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
    62
						error({ e = "test error" });
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
    63
					end
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
    64
					last_processed_item = item;
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
    65
				end, {
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
    66
					error = function (runner, err)
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
    67
						assert.equal(r, runner);
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
    68
						last_error = err;
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
    69
					end;
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
    70
				});
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
    71
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
    72
				r:run("hello");
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
    73
				assert.equal(r.state, "ready");
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
    74
				assert.equal(last_processed_item, "hello");
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
    75
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
    76
				assert.equal(last_error, nil);
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
    77
				r:run("error");
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
    78
				assert.is_table(last_error);
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
    79
				assert.equal(last_error.e, "test error");
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
    80
				last_error = nil;
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
    81
				assert.equal(r.state, "ready");
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
    82
				assert.equal(last_processed_item, "hello");
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
    83
			end);
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
    84
			it("should not be fatal to the runner", function ()
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
    85
				r:run("world");
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
    86
				assert.equal(r.state, "ready");
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
    87
				assert.equal(last_processed_item, "world");
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
    88
			end);
8612
9f6ab206d741 util.async: Ensure runner is left in correct state after out-of-main-loop error (+tests)
Matthew Wild <mwild1@gmail.com>
parents: 8611
diff changeset
    89
			it("should work despite a #waiter", function ()
9f6ab206d741 util.async: Ensure runner is left in correct state after out-of-main-loop error (+tests)
Matthew Wild <mwild1@gmail.com>
parents: 8611
diff changeset
    90
				-- This test covers an important case where a runner
9f6ab206d741 util.async: Ensure runner is left in correct state after out-of-main-loop error (+tests)
Matthew Wild <mwild1@gmail.com>
parents: 8611
diff changeset
    91
				-- throws an error while being executed outside of the
9f6ab206d741 util.async: Ensure runner is left in correct state after out-of-main-loop error (+tests)
Matthew Wild <mwild1@gmail.com>
parents: 8611
diff changeset
    92
				-- main loop. This happens when it was blocked ('waiting'),
9f6ab206d741 util.async: Ensure runner is left in correct state after out-of-main-loop error (+tests)
Matthew Wild <mwild1@gmail.com>
parents: 8611
diff changeset
    93
				-- and then released (via a call to done()).
9f6ab206d741 util.async: Ensure runner is left in correct state after out-of-main-loop error (+tests)
Matthew Wild <mwild1@gmail.com>
parents: 8611
diff changeset
    94
				last_error = nil;
9f6ab206d741 util.async: Ensure runner is left in correct state after out-of-main-loop error (+tests)
Matthew Wild <mwild1@gmail.com>
parents: 8611
diff changeset
    95
				r:run("wait");
9f6ab206d741 util.async: Ensure runner is left in correct state after out-of-main-loop error (+tests)
Matthew Wild <mwild1@gmail.com>
parents: 8611
diff changeset
    96
				assert.equal(r.state, "waiting");
9f6ab206d741 util.async: Ensure runner is left in correct state after out-of-main-loop error (+tests)
Matthew Wild <mwild1@gmail.com>
parents: 8611
diff changeset
    97
				done();
9f6ab206d741 util.async: Ensure runner is left in correct state after out-of-main-loop error (+tests)
Matthew Wild <mwild1@gmail.com>
parents: 8611
diff changeset
    98
				-- At this point an error happens (state goes error->ready)
9f6ab206d741 util.async: Ensure runner is left in correct state after out-of-main-loop error (+tests)
Matthew Wild <mwild1@gmail.com>
parents: 8611
diff changeset
    99
				assert.equal(r.state, "ready");
9f6ab206d741 util.async: Ensure runner is left in correct state after out-of-main-loop error (+tests)
Matthew Wild <mwild1@gmail.com>
parents: 8611
diff changeset
   100
				assert.is_table(last_error);
9f6ab206d741 util.async: Ensure runner is left in correct state after out-of-main-loop error (+tests)
Matthew Wild <mwild1@gmail.com>
parents: 8611
diff changeset
   101
				assert.equal(last_error.e, "post wait error");
9f6ab206d741 util.async: Ensure runner is left in correct state after out-of-main-loop error (+tests)
Matthew Wild <mwild1@gmail.com>
parents: 8611
diff changeset
   102
				last_error = nil;
9f6ab206d741 util.async: Ensure runner is left in correct state after out-of-main-loop error (+tests)
Matthew Wild <mwild1@gmail.com>
parents: 8611
diff changeset
   103
				r:run("hello again");
9f6ab206d741 util.async: Ensure runner is left in correct state after out-of-main-loop error (+tests)
Matthew Wild <mwild1@gmail.com>
parents: 8611
diff changeset
   104
				assert.equal(r.state, "ready");
9f6ab206d741 util.async: Ensure runner is left in correct state after out-of-main-loop error (+tests)
Matthew Wild <mwild1@gmail.com>
parents: 8611
diff changeset
   105
				assert.equal(last_processed_item, "hello again");
9f6ab206d741 util.async: Ensure runner is left in correct state after out-of-main-loop error (+tests)
Matthew Wild <mwild1@gmail.com>
parents: 8611
diff changeset
   106
			end);
8610
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
   107
		end);
8608
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   108
	end);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   109
	describe("#waiter", function()
8611
a2e6caf5848d util.async: Add test to ensure waiters throw an error outside async contexts
Matthew Wild <mwild1@gmail.com>
parents: 8610
diff changeset
   110
		it("should error outside of async context", function ()
a2e6caf5848d util.async: Add test to ensure waiters throw an error outside async contexts
Matthew Wild <mwild1@gmail.com>
parents: 8610
diff changeset
   111
			assert.has_error(function ()
a2e6caf5848d util.async: Add test to ensure waiters throw an error outside async contexts
Matthew Wild <mwild1@gmail.com>
parents: 8610
diff changeset
   112
				async.waiter();
a2e6caf5848d util.async: Add test to ensure waiters throw an error outside async contexts
Matthew Wild <mwild1@gmail.com>
parents: 8610
diff changeset
   113
			end);
a2e6caf5848d util.async: Add test to ensure waiters throw an error outside async contexts
Matthew Wild <mwild1@gmail.com>
parents: 8610
diff changeset
   114
		end);
8608
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   115
		it("should work", function ()
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   116
			local wait, done;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   117
		
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   118
			local r, l = new(function (item)
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   119
				assert(type(item) == "number")
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   120
				if item == 3 then
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   121
					wait, done = async.waiter();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   122
					wait();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   123
				end
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   124
			end);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   125
			
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   126
			r:run(1);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   127
			assert(r.state == "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   128
			r:run(2);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   129
			assert(r.state == "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   130
			r:run(3);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   131
			assert(r.state == "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   132
			done();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   133
			assert(r.state == "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   134
			--for k, v in ipairs(l) do print(k,v) end
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   135
		end);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   136
		
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   137
		it("should work", function ()
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   138
			--------------------
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   139
			local wait, done;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   140
			local last_item = 0;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   141
			local r, l = new(function (item)
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   142
				assert(type(item) == "number")
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   143
				assert(item == last_item + 1);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   144
				last_item = item;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   145
				if item == 3 then
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   146
					wait, done = async.waiter();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   147
					wait();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   148
				end
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   149
			end);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   150
			
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   151
			r:run(1);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   152
			assert(r.state == "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   153
			r:run(2);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   154
			assert(r.state == "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   155
			r:run(3);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   156
			assert(r.state == "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   157
			r:run(4);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   158
			assert(r.state == "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   159
			done();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   160
			assert(r.state == "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   161
			--for k, v in ipairs(l) do print(k,v) end
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   162
		end);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   163
		it("should work", function ()
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   164
			--------------------
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   165
			local wait, done;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   166
			local last_item = 0;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   167
			local r, l = new(function (item)
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   168
				assert(type(item) == "number")
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   169
				assert((item == last_item + 1) or item == 3);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   170
				last_item = item;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   171
				if item == 3 then
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   172
					wait, done = async.waiter();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   173
					wait();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   174
				end
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   175
			end);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   176
			
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   177
			r:run(1);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   178
			assert(r.state == "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   179
			r:run(2);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   180
			assert(r.state == "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   181
			
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   182
			local dones = {};
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   183
			r:run(3);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   184
			assert(r.state == "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   185
			r:run(3);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   186
			assert(r.state == "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   187
			r:run(3);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   188
			assert(r.state == "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   189
			r:run(4);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   190
			assert(r.state == "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   191
		
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   192
			for i = 1, 3 do
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   193
				done();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   194
				if i < 3 then
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   195
					assert(r.state == "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   196
				end
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   197
			end
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   198
		
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   199
			assert(r.state == "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   200
			--for k, v in ipairs(l) do print(k,v) end
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   201
		end);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   202
		it("should work", function ()
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   203
			--------------------
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   204
			local wait, done;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   205
			local last_item = 0;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   206
			local r, l = new(function (item)
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   207
				assert(type(item) == "number")
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   208
				assert((item == last_item + 1) or item == 3);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   209
				last_item = item;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   210
				if item == 3 then
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   211
					wait, done = async.waiter();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   212
					wait();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   213
				end
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   214
			end);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   215
			
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   216
			r:run(1);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   217
			assert(r.state == "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   218
			r:run(2);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   219
			assert(r.state == "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   220
			
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   221
			local dones = {};
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   222
			r:run(3);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   223
			assert(r.state == "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   224
			r:run(3);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   225
			assert(r.state == "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   226
		
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   227
			for i = 1, 2 do
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   228
				done();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   229
				if i < 2 then
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   230
					assert(r.state == "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   231
				end
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   232
			end
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   233
		
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   234
			assert(r.state == "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   235
			r:run(4);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   236
			assert(r.state == "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   237
		
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   238
			assert(r.state == "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   239
			--for k, v in ipairs(l) do print(k,v) end
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   240
		end);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   241
		it("should work with multiple runners in parallel", function ()
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   242
			-- Now with multiple runners
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   243
			--------------------
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   244
			local wait1, done1;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   245
			local last_item1 = 0;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   246
			local r1, l1 = new(function (item)
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   247
				assert(type(item) == "number")
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   248
				assert((item == last_item1 + 1) or item == 3);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   249
				last_item1 = item;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   250
				if item == 3 then
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   251
					wait1, done1 = async.waiter();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   252
					wait1();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   253
				end
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   254
			end, "r1");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   255
		
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   256
			local wait2, done2;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   257
			local last_item2 = 0;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   258
			local r2, l2 = new(function (item)
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   259
				assert(type(item) == "number")
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   260
				assert((item == last_item2 + 1) or item == 3);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   261
				last_item2 = item;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   262
				if item == 3 then
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   263
					wait2, done2 = async.waiter();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   264
					wait2();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   265
				end
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   266
			end, "r2");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   267
			
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   268
			r1:run(1);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   269
			assert(r1.state == "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   270
			r1:run(2);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   271
			assert(r1.state == "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   272
			
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   273
			local dones = {};
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   274
			r1:run(3);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   275
			assert(r1.state == "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   276
			r1:run(3);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   277
			assert(r1.state == "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   278
		
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   279
			r2:run(1);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   280
			assert(r1.state == "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   281
			assert(r2.state == "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   282
		
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   283
			r2:run(2);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   284
			assert(r1.state == "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   285
			assert(r2.state == "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   286
		
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   287
			r2:run(3);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   288
			assert(r1.state == "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   289
			assert(r2.state == "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   290
			done2();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   291
		
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   292
			r2:run(3);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   293
			assert(r1.state == "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   294
			assert(r2.state == "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   295
			done2();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   296
		
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   297
			r2:run(4);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   298
			assert(r1.state == "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   299
			assert(r2.state == "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   300
		
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   301
			for i = 1, 2 do
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   302
				done1();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   303
				if i < 2 then
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   304
					assert(r1.state == "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   305
				end
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   306
			end
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   307
		
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   308
			assert(r1.state == "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   309
			r1:run(4);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   310
			assert(r1.state == "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   311
		
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   312
			assert(r1.state == "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   313
			--for k, v in ipairs(l1) do print(k,v) end
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   314
		end);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   315
		it("should work work with multiple runners in parallel", function ()
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   316
			--------------------
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   317
			local wait1, done1;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   318
			local last_item1 = 0;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   319
			local r1, l1 = new(function (item)
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   320
				print("r1 processing ", item);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   321
				assert(type(item) == "number")
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   322
				assert((item == last_item1 + 1) or item == 3);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   323
				last_item1 = item;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   324
				if item == 3 then
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   325
					wait1, done1 = async.waiter();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   326
					wait1();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   327
				end
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   328
			end, "r1");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   329
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   330
			local wait2, done2;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   331
			local last_item2 = 0;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   332
			local r2, l2 = new(function (item)
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   333
				print("r2 processing ", item);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   334
				assert.is_number(item);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   335
				assert((item == last_item2 + 1) or item == 3);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   336
				last_item2 = item;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   337
				if item == 3 then
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   338
					wait2, done2 = async.waiter();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   339
					wait2();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   340
				end
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   341
			end, "r2");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   342
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   343
			r1:run(1);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   344
			assert.equal(r1.state, "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   345
			r1:run(2);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   346
			assert.equal(r1.state, "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   347
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   348
			r1:run(5);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   349
			assert.equal(r1.state, "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   350
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   351
			local dones = {};
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   352
			r1:run(3);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   353
			assert.equal(r1.state, "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   354
			r1:run(5); -- Will error, when we get to it
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   355
			assert.equal(r1.state, "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   356
			done1();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   357
			assert.equal(r1.state, "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   358
			r1:run(3);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   359
			assert.equal(r1.state, "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   360
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   361
			r2:run(1);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   362
			assert.equal(r1.state, "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   363
			assert.equal(r2.state, "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   364
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   365
			r2:run(2);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   366
			assert.equal(r1.state, "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   367
			assert.equal(r2.state, "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   368
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   369
			r2:run(3);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   370
			assert.equal(r1.state, "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   371
			assert.equal(r2.state, "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   372
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   373
			done2();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   374
			assert.equal(r1.state, "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   375
			assert.equal(r2.state, "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   376
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   377
			r2:run(3);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   378
			assert.equal(r1.state, "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   379
			assert.equal(r2.state, "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   380
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   381
			done2();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   382
			assert.equal(r1.state, "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   383
			assert.equal(r2.state, "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   384
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   385
			r2:run(4);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   386
			assert.equal(r1.state, "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   387
			assert.equal(r2.state, "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   388
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   389
			done1();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   390
		
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   391
			assert.equal(r1.state, "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   392
			r1:run(4);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   393
			assert.equal(r1.state, "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   394
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   395
			assert.equal(r1.state, "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   396
			--for k, v in ipairs(l1) do print(k,v) end
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   397
		end);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   398
	end);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   399
end);