spec/util_async_spec.lua
author Matthew Wild <mwild1@gmail.com>
Mon, 19 Mar 2018 16:24:42 +0000
changeset 8633 deade38ffbbd
parent 8631 e88744fa0985
child 8634 1daabc077393
permissions -rw-r--r--
util.async: tests: slight modifications to allow more code reuse in 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
8626
ab242c513bf4 util.async: tests: Add helper function to create mock watcher callbacks
Matthew Wild <mwild1@gmail.com>
parents: 8625
diff changeset
    11
8633
deade38ffbbd util.async: tests: slight modifications to allow more code reuse in tests
Matthew Wild <mwild1@gmail.com>
parents: 8631
diff changeset
    12
	local function mock_watchers(event_log)
deade38ffbbd util.async: tests: slight modifications to allow more code reuse in tests
Matthew Wild <mwild1@gmail.com>
parents: 8631
diff changeset
    13
		local function generic_logging_watcher(name)
deade38ffbbd util.async: tests: slight modifications to allow more code reuse in tests
Matthew Wild <mwild1@gmail.com>
parents: 8631
diff changeset
    14
			return function (...)
deade38ffbbd util.async: tests: slight modifications to allow more code reuse in tests
Matthew Wild <mwild1@gmail.com>
parents: 8631
diff changeset
    15
				table.insert(event_log, { name = name, n = select("#", ...)-1, select(2, ...) });
deade38ffbbd util.async: tests: slight modifications to allow more code reuse in tests
Matthew Wild <mwild1@gmail.com>
parents: 8631
diff changeset
    16
			end;
deade38ffbbd util.async: tests: slight modifications to allow more code reuse in tests
Matthew Wild <mwild1@gmail.com>
parents: 8631
diff changeset
    17
		end;
8626
ab242c513bf4 util.async: tests: Add helper function to create mock watcher callbacks
Matthew Wild <mwild1@gmail.com>
parents: 8625
diff changeset
    18
		return setmetatable(mock{
8633
deade38ffbbd util.async: tests: slight modifications to allow more code reuse in tests
Matthew Wild <mwild1@gmail.com>
parents: 8631
diff changeset
    19
			ready = generic_logging_watcher("ready");
deade38ffbbd util.async: tests: slight modifications to allow more code reuse in tests
Matthew Wild <mwild1@gmail.com>
parents: 8631
diff changeset
    20
			waiting = generic_logging_watcher("waiting");
deade38ffbbd util.async: tests: slight modifications to allow more code reuse in tests
Matthew Wild <mwild1@gmail.com>
parents: 8631
diff changeset
    21
			error = generic_logging_watcher("error");
8626
ab242c513bf4 util.async: tests: Add helper function to create mock watcher callbacks
Matthew Wild <mwild1@gmail.com>
parents: 8625
diff changeset
    22
		}, {
ab242c513bf4 util.async: tests: Add helper function to create mock watcher callbacks
Matthew Wild <mwild1@gmail.com>
parents: 8625
diff changeset
    23
			__index = function (_, event)
ab242c513bf4 util.async: tests: Add helper function to create mock watcher callbacks
Matthew Wild <mwild1@gmail.com>
parents: 8625
diff changeset
    24
				-- Unexpected watcher called
ab242c513bf4 util.async: tests: Add helper function to create mock watcher callbacks
Matthew Wild <mwild1@gmail.com>
parents: 8625
diff changeset
    25
				assert(false);
ab242c513bf4 util.async: tests: Add helper function to create mock watcher callbacks
Matthew Wild <mwild1@gmail.com>
parents: 8625
diff changeset
    26
			end;
ab242c513bf4 util.async: tests: Add helper function to create mock watcher callbacks
Matthew Wild <mwild1@gmail.com>
parents: 8625
diff changeset
    27
		})
ab242c513bf4 util.async: tests: Add helper function to create mock watcher callbacks
Matthew Wild <mwild1@gmail.com>
parents: 8625
diff changeset
    28
	end
ab242c513bf4 util.async: tests: Add helper function to create mock watcher callbacks
Matthew Wild <mwild1@gmail.com>
parents: 8625
diff changeset
    29
8608
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    30
	local function new(func, name)
8633
deade38ffbbd util.async: tests: slight modifications to allow more code reuse in tests
Matthew Wild <mwild1@gmail.com>
parents: 8631
diff changeset
    31
		local event_log = {};
deade38ffbbd util.async: tests: slight modifications to allow more code reuse in tests
Matthew Wild <mwild1@gmail.com>
parents: 8631
diff changeset
    32
		local spy_func = spy.new(func);
deade38ffbbd util.async: tests: slight modifications to allow more code reuse in tests
Matthew Wild <mwild1@gmail.com>
parents: 8631
diff changeset
    33
		return async.runner(spy_func, mock_watchers(event_log)), event_log, spy_func;
8608
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    34
	end
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    35
	describe("#runner", function()
8625
92fee8a6c988 util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents: 8621
diff changeset
    36
		it("should work", function()
8608
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    37
			local r, l = new(function (item) assert(type(item) == "number") end);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    38
			r:run(1);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    39
			r:run(2);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    40
		end);
8610
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
    41
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
    42
		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
    43
			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
    44
			assert.equal(r.state, "ready");
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
    45
		end);
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
    46
8617
bfbafeced0c4 util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents: 8612
diff changeset
    47
		it("should do nothing if the queue is empty", function ()
bfbafeced0c4 util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents: 8612
diff changeset
    48
			local did_run;
bfbafeced0c4 util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents: 8612
diff changeset
    49
			local r = async.runner(function (item) did_run = true end);
bfbafeced0c4 util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents: 8612
diff changeset
    50
			r:run();
bfbafeced0c4 util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents: 8612
diff changeset
    51
			assert.equal(r.state, "ready");
bfbafeced0c4 util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents: 8612
diff changeset
    52
			assert.is_nil(did_run);
bfbafeced0c4 util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents: 8612
diff changeset
    53
			r:run("hello");
bfbafeced0c4 util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents: 8612
diff changeset
    54
			assert.is_true(did_run);
bfbafeced0c4 util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents: 8612
diff changeset
    55
		end);
bfbafeced0c4 util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents: 8612
diff changeset
    56
bfbafeced0c4 util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents: 8612
diff changeset
    57
		it("should support queuing work items without running", function ()
bfbafeced0c4 util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents: 8612
diff changeset
    58
			local did_run;
bfbafeced0c4 util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents: 8612
diff changeset
    59
			local r = async.runner(function (item) did_run = true end);
bfbafeced0c4 util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents: 8612
diff changeset
    60
			r:enqueue("hello");
bfbafeced0c4 util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents: 8612
diff changeset
    61
			assert.equal(r.state, "ready");
bfbafeced0c4 util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents: 8612
diff changeset
    62
			assert.is_nil(did_run);
bfbafeced0c4 util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents: 8612
diff changeset
    63
			r:run();
bfbafeced0c4 util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents: 8612
diff changeset
    64
			assert.is_true(did_run);
bfbafeced0c4 util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents: 8612
diff changeset
    65
		end);
bfbafeced0c4 util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents: 8612
diff changeset
    66
bfbafeced0c4 util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents: 8612
diff changeset
    67
		it("should support queuing multiple work items", function ()
bfbafeced0c4 util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents: 8612
diff changeset
    68
			local last_item;
bfbafeced0c4 util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents: 8612
diff changeset
    69
			local s = spy(function (item) last_item = item; end);
bfbafeced0c4 util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents: 8612
diff changeset
    70
			local r = async.runner(s);
bfbafeced0c4 util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents: 8612
diff changeset
    71
			r:enqueue("hello");
bfbafeced0c4 util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents: 8612
diff changeset
    72
			r:enqueue("there");
bfbafeced0c4 util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents: 8612
diff changeset
    73
			r:enqueue("world");
bfbafeced0c4 util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents: 8612
diff changeset
    74
			assert.equal(r.state, "ready");
bfbafeced0c4 util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents: 8612
diff changeset
    75
			r:run();
bfbafeced0c4 util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents: 8612
diff changeset
    76
			assert.equal(r.state, "ready");
bfbafeced0c4 util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents: 8612
diff changeset
    77
			assert.spy(s).was.called(3);
bfbafeced0c4 util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents: 8612
diff changeset
    78
			assert.equal(last_item, "world");
bfbafeced0c4 util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents: 8612
diff changeset
    79
		end);
bfbafeced0c4 util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents: 8612
diff changeset
    80
bfbafeced0c4 util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents: 8612
diff changeset
    81
		it("should support all simple data types", function ()
bfbafeced0c4 util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents: 8612
diff changeset
    82
			local last_item;
bfbafeced0c4 util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents: 8612
diff changeset
    83
			local s = spy(function (item) last_item = item; end);
bfbafeced0c4 util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents: 8612
diff changeset
    84
			local r = async.runner(s);
bfbafeced0c4 util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents: 8612
diff changeset
    85
			local values = { {}, 123, "hello", true, false };
bfbafeced0c4 util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents: 8612
diff changeset
    86
			for i = 1, #values do
bfbafeced0c4 util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents: 8612
diff changeset
    87
				r:enqueue(values[i]);
bfbafeced0c4 util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents: 8612
diff changeset
    88
			end
bfbafeced0c4 util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents: 8612
diff changeset
    89
			assert.equal(r.state, "ready");
bfbafeced0c4 util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents: 8612
diff changeset
    90
			r:run();
bfbafeced0c4 util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents: 8612
diff changeset
    91
			assert.equal(r.state, "ready");
bfbafeced0c4 util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents: 8612
diff changeset
    92
			assert.spy(s).was.called(#values);
bfbafeced0c4 util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents: 8612
diff changeset
    93
			for i = 1, #values do
bfbafeced0c4 util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents: 8612
diff changeset
    94
				assert.spy(s).was.called_with(values[i]);
bfbafeced0c4 util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents: 8612
diff changeset
    95
			end
bfbafeced0c4 util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents: 8612
diff changeset
    96
			assert.equal(last_item, values[#values]);
bfbafeced0c4 util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents: 8612
diff changeset
    97
		end);
bfbafeced0c4 util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents: 8612
diff changeset
    98
8610
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
    99
		describe("#errors", function ()
8621
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   100
			describe("should notify", function ()
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   101
				local last_processed_item, last_error;
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   102
				local r;
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   103
				r = async.runner(function (item)
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   104
					if item == "error" then
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   105
						error({ e = "test error" });
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   106
					end
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   107
					last_processed_item = item;
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   108
				end, mock{
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   109
					ready = function () end;
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   110
					waiting = function () end;
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   111
					error = function (runner, err)
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   112
						assert.equal(r, runner);
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   113
						last_error = err;
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   114
					end;
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   115
				});
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   116
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   117
				-- Simple item, no error
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   118
				r:run("hello");
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   119
				assert.equal(r.state, "ready");
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   120
				assert.equal(last_processed_item, "hello");
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   121
				assert.spy(r.watchers.ready).was_not.called();
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   122
				assert.spy(r.watchers.error).was_not.called();
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   123
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   124
				-- Trigger an error inside the runner
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   125
				assert.equal(last_error, nil);
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   126
				r:run("error");
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   127
				test("the correct watcher functions", function ()
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   128
					-- Only the error watcher should have been called
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   129
					assert.spy(r.watchers.ready).was_not.called();
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   130
					assert.spy(r.watchers.waiting).was_not.called();
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   131
					assert.spy(r.watchers.error).was.called(1);
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   132
				end);
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   133
				test("with the correct error", function ()
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   134
					-- The error watcher state should be correct, to
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   135
					-- demonstrate the error was passed correctly
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   136
					assert.is_table(last_error);
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   137
					assert.equal(last_error.e, "test error");
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   138
					last_error = nil;
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   139
				end);
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   140
				assert.equal(r.state, "ready");
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   141
				assert.equal(last_processed_item, "hello");
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   142
			end);
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   143
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   144
8610
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
   145
			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
   146
			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
   147
			local wait, done;
8610
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
   148
			r = async.runner(function (item)
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
   149
				if item == "error" then
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
   150
					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
   151
				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
   152
					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
   153
					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
   154
					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
   155
				end
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
   156
				last_processed_item = item;
8621
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   157
			end, mock({
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   158
				ready = function () end;
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   159
				waiting = function () end;
8610
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
   160
				error = function (runner, err)
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
   161
					assert.equal(r, runner);
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
   162
					last_error = err;
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
   163
				end;
8621
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   164
			}));
8610
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
   165
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
   166
			randomize(false);
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
   167
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
   168
			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
   169
				r:run("world");
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
   170
				assert.equal(r.state, "ready");
8621
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   171
				assert.spy(r.watchers.ready).was_not.called();
8610
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
   172
				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
   173
			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
   174
			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
   175
				-- 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
   176
				-- 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
   177
				-- 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
   178
				-- 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
   179
				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
   180
				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
   181
				assert.equal(r.state, "waiting");
8621
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   182
				assert.spy(r.watchers.waiting).was.called(1);
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
   183
				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
   184
				-- 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
   185
				assert.equal(r.state, "ready");
8621
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   186
				assert.spy(r.watchers.error).was.called(1);
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   187
				assert.spy(r.watchers.ready).was.called(1);
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
   188
				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
   189
				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
   190
				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
   191
				r:run("hello again");
8621
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   192
				assert.spy(r.watchers.ready).was.called(1);
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   193
				assert.spy(r.watchers.waiting).was.called(1);
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   194
				assert.spy(r.watchers.error).was.called(1);
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
   195
				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
   196
				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
   197
			end);
8618
e77b37de482e util.async: Behaviour change: continue to process queued items after errors
Matthew Wild <mwild1@gmail.com>
parents: 8617
diff changeset
   198
e77b37de482e util.async: Behaviour change: continue to process queued items after errors
Matthew Wild <mwild1@gmail.com>
parents: 8617
diff changeset
   199
			it("should continue to process work items", function ()
e77b37de482e util.async: Behaviour change: continue to process queued items after errors
Matthew Wild <mwild1@gmail.com>
parents: 8617
diff changeset
   200
				local wait, done, last_item;
e77b37de482e util.async: Behaviour change: continue to process queued items after errors
Matthew Wild <mwild1@gmail.com>
parents: 8617
diff changeset
   201
				local runner_func = spy.new(function (item)
e77b37de482e util.async: Behaviour change: continue to process queued items after errors
Matthew Wild <mwild1@gmail.com>
parents: 8617
diff changeset
   202
					if item == "error" then
e77b37de482e util.async: Behaviour change: continue to process queued items after errors
Matthew Wild <mwild1@gmail.com>
parents: 8617
diff changeset
   203
						error("test error");
8621
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   204
					end
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   205
					last_item = item;
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   206
				end);
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   207
				local runner = async.runner(runner_func, mock{
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   208
					ready = function () end;
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   209
					waiting = function () end;
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   210
					error = function () end;
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   211
				});
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   212
				runner:enqueue("one");
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   213
				runner:enqueue("error");
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   214
				runner:enqueue("two");
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   215
				runner:run();
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   216
				assert.equal(r.state, "ready");
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   217
				assert.spy(runner_func).was.called(3);
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   218
				assert.spy(runner.watchers.error).was.called(1);
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   219
				assert.spy(runner.watchers.ready).was.called(0);
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   220
				assert.spy(runner.watchers.waiting).was.called(0);
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   221
				assert.equal(last_item, "two");
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   222
			end);
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   223
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   224
			it("should continue to process work items during resume", function ()
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   225
				local wait, done, last_item;
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   226
				local runner_func = spy.new(function (item)
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   227
					if item == "wait-error" then
8618
e77b37de482e util.async: Behaviour change: continue to process queued items after errors
Matthew Wild <mwild1@gmail.com>
parents: 8617
diff changeset
   228
						wait, done = async.waiter();
e77b37de482e util.async: Behaviour change: continue to process queued items after errors
Matthew Wild <mwild1@gmail.com>
parents: 8617
diff changeset
   229
						wait();
e77b37de482e util.async: Behaviour change: continue to process queued items after errors
Matthew Wild <mwild1@gmail.com>
parents: 8617
diff changeset
   230
						error("test error");
e77b37de482e util.async: Behaviour change: continue to process queued items after errors
Matthew Wild <mwild1@gmail.com>
parents: 8617
diff changeset
   231
					end
e77b37de482e util.async: Behaviour change: continue to process queued items after errors
Matthew Wild <mwild1@gmail.com>
parents: 8617
diff changeset
   232
					last_item = item;
e77b37de482e util.async: Behaviour change: continue to process queued items after errors
Matthew Wild <mwild1@gmail.com>
parents: 8617
diff changeset
   233
				end);
8621
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   234
				local runner = async.runner(runner_func, mock{
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   235
					ready = function () end;
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   236
					waiting = function () end;
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   237
					error = function () end;
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   238
				});
8619
a15c891c6232 util.async: ensure change in e77b37de482e applies after out-of-loop resume also
Matthew Wild <mwild1@gmail.com>
parents: 8618
diff changeset
   239
				runner:enqueue("one");
a15c891c6232 util.async: ensure change in e77b37de482e applies after out-of-loop resume also
Matthew Wild <mwild1@gmail.com>
parents: 8618
diff changeset
   240
				runner:enqueue("wait-error");
a15c891c6232 util.async: ensure change in e77b37de482e applies after out-of-loop resume also
Matthew Wild <mwild1@gmail.com>
parents: 8618
diff changeset
   241
				runner:enqueue("two");
a15c891c6232 util.async: ensure change in e77b37de482e applies after out-of-loop resume also
Matthew Wild <mwild1@gmail.com>
parents: 8618
diff changeset
   242
				runner:run();
a15c891c6232 util.async: ensure change in e77b37de482e applies after out-of-loop resume also
Matthew Wild <mwild1@gmail.com>
parents: 8618
diff changeset
   243
				done();
a15c891c6232 util.async: ensure change in e77b37de482e applies after out-of-loop resume also
Matthew Wild <mwild1@gmail.com>
parents: 8618
diff changeset
   244
				assert.equal(r.state, "ready");
a15c891c6232 util.async: ensure change in e77b37de482e applies after out-of-loop resume also
Matthew Wild <mwild1@gmail.com>
parents: 8618
diff changeset
   245
				assert.spy(runner_func).was.called(3);
a15c891c6232 util.async: ensure change in e77b37de482e applies after out-of-loop resume also
Matthew Wild <mwild1@gmail.com>
parents: 8618
diff changeset
   246
				assert.spy(runner.watchers.error).was.called(1);
8621
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   247
				assert.spy(runner.watchers.waiting).was.called(1);
680b1caa2dea util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents: 8620
diff changeset
   248
				assert.spy(runner.watchers.ready).was.called(1);
8619
a15c891c6232 util.async: ensure change in e77b37de482e applies after out-of-loop resume also
Matthew Wild <mwild1@gmail.com>
parents: 8618
diff changeset
   249
				assert.equal(last_item, "two");
a15c891c6232 util.async: ensure change in e77b37de482e applies after out-of-loop resume also
Matthew Wild <mwild1@gmail.com>
parents: 8618
diff changeset
   250
			end);
8610
62bb06cf8a43 util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents: 8608
diff changeset
   251
		end);
8608
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   252
	end);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   253
	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
   254
		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
   255
			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
   256
				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
   257
			end);
a2e6caf5848d util.async: Add test to ensure waiters throw an error outside async contexts
Matthew Wild <mwild1@gmail.com>
parents: 8610
diff changeset
   258
		end);
8608
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   259
		it("should work", function ()
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   260
			local wait, done;
8625
92fee8a6c988 util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents: 8621
diff changeset
   261
8608
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   262
			local r, l = new(function (item)
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   263
				assert(type(item) == "number")
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   264
				if item == 3 then
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   265
					wait, done = async.waiter();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   266
					wait();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   267
				end
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   268
			end);
8625
92fee8a6c988 util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents: 8621
diff changeset
   269
8608
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   270
			r:run(1);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   271
			assert(r.state == "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   272
			r:run(2);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   273
			assert(r.state == "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   274
			r:run(3);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   275
			assert(r.state == "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   276
			done();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   277
			assert(r.state == "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   278
			--for k, v in ipairs(l) do print(k,v) end
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   279
		end);
8625
92fee8a6c988 util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents: 8621
diff changeset
   280
8608
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   281
		it("should work", function ()
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
			local wait, done;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   284
			local last_item = 0;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   285
			local r, l = new(function (item)
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   286
				assert(type(item) == "number")
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   287
				assert(item == last_item + 1);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   288
				last_item = item;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   289
				if item == 3 then
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   290
					wait, done = async.waiter();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   291
					wait();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   292
				end
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   293
			end);
8625
92fee8a6c988 util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents: 8621
diff changeset
   294
8608
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   295
			r:run(1);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   296
			assert(r.state == "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   297
			r:run(2);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   298
			assert(r.state == "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   299
			r:run(3);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   300
			assert(r.state == "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   301
			r:run(4);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   302
			assert(r.state == "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   303
			done();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   304
			assert(r.state == "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   305
			--for k, v in ipairs(l) do print(k,v) 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
		it("should work", function ()
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   308
			--------------------
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   309
			local wait, done;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   310
			local last_item = 0;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   311
			local r, l = new(function (item)
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   312
				assert(type(item) == "number")
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   313
				assert((item == last_item + 1) or item == 3);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   314
				last_item = item;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   315
				if item == 3 then
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   316
					wait, done = async.waiter();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   317
					wait();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   318
				end
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   319
			end);
8625
92fee8a6c988 util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents: 8621
diff changeset
   320
8608
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   321
			r:run(1);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   322
			assert(r.state == "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   323
			r:run(2);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   324
			assert(r.state == "ready");
8625
92fee8a6c988 util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents: 8621
diff changeset
   325
8608
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   326
			local dones = {};
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   327
			r:run(3);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   328
			assert(r.state == "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   329
			r:run(3);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   330
			assert(r.state == "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   331
			r:run(3);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   332
			assert(r.state == "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   333
			r:run(4);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   334
			assert(r.state == "waiting");
8625
92fee8a6c988 util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents: 8621
diff changeset
   335
8608
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   336
			for i = 1, 3 do
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   337
				done();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   338
				if i < 3 then
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   339
					assert(r.state == "waiting");
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
8625
92fee8a6c988 util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents: 8621
diff changeset
   342
8608
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   343
			assert(r.state == "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   344
			--for k, v in ipairs(l) do print(k,v) end
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   345
		end);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   346
		it("should work", function ()
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
			local wait, done;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   349
			local last_item = 0;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   350
			local r, l = new(function (item)
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   351
				assert(type(item) == "number")
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   352
				assert((item == last_item + 1) or item == 3);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   353
				last_item = item;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   354
				if item == 3 then
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   355
					wait, done = async.waiter();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   356
					wait();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   357
				end
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   358
			end);
8625
92fee8a6c988 util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents: 8621
diff changeset
   359
8608
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   360
			r:run(1);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   361
			assert(r.state == "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   362
			r:run(2);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   363
			assert(r.state == "ready");
8625
92fee8a6c988 util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents: 8621
diff changeset
   364
8608
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   365
			local dones = {};
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   366
			r:run(3);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   367
			assert(r.state == "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   368
			r:run(3);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   369
			assert(r.state == "waiting");
8625
92fee8a6c988 util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents: 8621
diff changeset
   370
8608
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   371
			for i = 1, 2 do
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   372
				done();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   373
				if i < 2 then
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   374
					assert(r.state == "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   375
				end
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   376
			end
8625
92fee8a6c988 util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents: 8621
diff changeset
   377
8608
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   378
			assert(r.state == "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   379
			r:run(4);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   380
			assert(r.state == "ready");
8625
92fee8a6c988 util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents: 8621
diff changeset
   381
8608
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   382
			assert(r.state == "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   383
			--for k, v in ipairs(l) do print(k,v) end
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   384
		end);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   385
		it("should work with multiple runners in parallel", function ()
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   386
			-- Now with multiple runners
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   387
			--------------------
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   388
			local wait1, done1;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   389
			local last_item1 = 0;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   390
			local r1, l1 = new(function (item)
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   391
				assert(type(item) == "number")
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   392
				assert((item == last_item1 + 1) or item == 3);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   393
				last_item1 = item;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   394
				if item == 3 then
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   395
					wait1, done1 = async.waiter();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   396
					wait1();
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, "r1");
8625
92fee8a6c988 util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents: 8621
diff changeset
   399
8608
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   400
			local wait2, done2;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   401
			local last_item2 = 0;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   402
			local r2, l2 = new(function (item)
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   403
				assert(type(item) == "number")
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   404
				assert((item == last_item2 + 1) or item == 3);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   405
				last_item2 = item;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   406
				if item == 3 then
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   407
					wait2, done2 = async.waiter();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   408
					wait2();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   409
				end
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   410
			end, "r2");
8625
92fee8a6c988 util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents: 8621
diff changeset
   411
8608
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   412
			r1:run(1);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   413
			assert(r1.state == "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   414
			r1:run(2);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   415
			assert(r1.state == "ready");
8625
92fee8a6c988 util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents: 8621
diff changeset
   416
8608
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   417
			local dones = {};
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   418
			r1:run(3);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   419
			assert(r1.state == "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   420
			r1:run(3);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   421
			assert(r1.state == "waiting");
8625
92fee8a6c988 util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents: 8621
diff changeset
   422
8608
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   423
			r2:run(1);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   424
			assert(r1.state == "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   425
			assert(r2.state == "ready");
8625
92fee8a6c988 util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents: 8621
diff changeset
   426
8608
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   427
			r2:run(2);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   428
			assert(r1.state == "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   429
			assert(r2.state == "ready");
8625
92fee8a6c988 util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents: 8621
diff changeset
   430
8608
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   431
			r2:run(3);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   432
			assert(r1.state == "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   433
			assert(r2.state == "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   434
			done2();
8625
92fee8a6c988 util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents: 8621
diff changeset
   435
8608
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   436
			r2:run(3);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   437
			assert(r1.state == "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   438
			assert(r2.state == "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   439
			done2();
8625
92fee8a6c988 util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents: 8621
diff changeset
   440
8608
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   441
			r2:run(4);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   442
			assert(r1.state == "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   443
			assert(r2.state == "ready");
8625
92fee8a6c988 util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents: 8621
diff changeset
   444
8608
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   445
			for i = 1, 2 do
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   446
				done1();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   447
				if i < 2 then
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   448
					assert(r1.state == "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   449
				end
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   450
			end
8625
92fee8a6c988 util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents: 8621
diff changeset
   451
8608
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   452
			assert(r1.state == "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   453
			r1:run(4);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   454
			assert(r1.state == "ready");
8625
92fee8a6c988 util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents: 8621
diff changeset
   455
8608
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   456
			assert(r1.state == "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   457
			--for k, v in ipairs(l1) do print(k,v) end
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   458
		end);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   459
		it("should work work with multiple runners in parallel", function ()
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   460
			--------------------
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   461
			local wait1, done1;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   462
			local last_item1 = 0;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   463
			local r1, l1 = new(function (item)
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   464
				print("r1 processing ", item);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   465
				assert(type(item) == "number")
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   466
				assert((item == last_item1 + 1) or item == 3);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   467
				last_item1 = item;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   468
				if item == 3 then
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   469
					wait1, done1 = async.waiter();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   470
					wait1();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   471
				end
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   472
			end, "r1");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   473
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   474
			local wait2, done2;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   475
			local last_item2 = 0;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   476
			local r2, l2 = new(function (item)
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   477
				print("r2 processing ", item);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   478
				assert.is_number(item);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   479
				assert((item == last_item2 + 1) or item == 3);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   480
				last_item2 = item;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   481
				if item == 3 then
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   482
					wait2, done2 = async.waiter();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   483
					wait2();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   484
				end
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   485
			end, "r2");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   486
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   487
			r1:run(1);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   488
			assert.equal(r1.state, "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   489
			r1:run(2);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   490
			assert.equal(r1.state, "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   491
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   492
			r1:run(5);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   493
			assert.equal(r1.state, "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   494
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   495
			local dones = {};
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   496
			r1:run(3);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   497
			assert.equal(r1.state, "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   498
			r1:run(5); -- Will error, when we get to it
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   499
			assert.equal(r1.state, "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   500
			done1();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   501
			assert.equal(r1.state, "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   502
			r1:run(3);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   503
			assert.equal(r1.state, "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   504
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   505
			r2:run(1);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   506
			assert.equal(r1.state, "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   507
			assert.equal(r2.state, "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   508
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   509
			r2:run(2);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   510
			assert.equal(r1.state, "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   511
			assert.equal(r2.state, "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   512
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   513
			r2:run(3);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   514
			assert.equal(r1.state, "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   515
			assert.equal(r2.state, "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   516
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   517
			done2();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   518
			assert.equal(r1.state, "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   519
			assert.equal(r2.state, "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   520
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   521
			r2:run(3);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   522
			assert.equal(r1.state, "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   523
			assert.equal(r2.state, "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   524
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   525
			done2();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   526
			assert.equal(r1.state, "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   527
			assert.equal(r2.state, "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   528
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   529
			r2:run(4);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   530
			assert.equal(r1.state, "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   531
			assert.equal(r2.state, "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   532
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   533
			done1();
8625
92fee8a6c988 util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents: 8621
diff changeset
   534
8608
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   535
			assert.equal(r1.state, "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   536
			r1:run(4);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   537
			assert.equal(r1.state, "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   538
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   539
			assert.equal(r1.state, "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   540
			--for k, v in ipairs(l1) do print(k,v) end
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   541
		end);
8627
5325f0e1791b util.async: tests: Ensure done() can be called before wait()
Matthew Wild <mwild1@gmail.com>
parents: 8626
diff changeset
   542
8631
e88744fa0985 util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents: 8627
diff changeset
   543
		it("should support multiple done() calls", function ()
e88744fa0985 util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents: 8627
diff changeset
   544
			local processed_item;
e88744fa0985 util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents: 8627
diff changeset
   545
			local wait, done;
8633
deade38ffbbd util.async: tests: slight modifications to allow more code reuse in tests
Matthew Wild <mwild1@gmail.com>
parents: 8631
diff changeset
   546
			local r, _, rf = new(function (item)
8631
e88744fa0985 util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents: 8627
diff changeset
   547
				wait, done = async.waiter(4);
e88744fa0985 util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents: 8627
diff changeset
   548
				wait();
e88744fa0985 util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents: 8627
diff changeset
   549
				processed_item = item;
e88744fa0985 util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents: 8627
diff changeset
   550
			end);
e88744fa0985 util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents: 8627
diff changeset
   551
			r:run("test");
e88744fa0985 util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents: 8627
diff changeset
   552
			for i = 1, 3 do
e88744fa0985 util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents: 8627
diff changeset
   553
				done();
e88744fa0985 util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents: 8627
diff changeset
   554
				assert.equal(r.state, "waiting");
e88744fa0985 util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents: 8627
diff changeset
   555
				assert.is_nil(processed_item);
e88744fa0985 util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents: 8627
diff changeset
   556
			end
e88744fa0985 util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents: 8627
diff changeset
   557
			done();
e88744fa0985 util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents: 8627
diff changeset
   558
			assert.equal(r.state, "ready");
e88744fa0985 util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents: 8627
diff changeset
   559
			assert.equal(processed_item, "test");
e88744fa0985 util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents: 8627
diff changeset
   560
			assert.spy(r.watchers.error).was_not.called();
e88744fa0985 util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents: 8627
diff changeset
   561
		end);
e88744fa0985 util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents: 8627
diff changeset
   562
e88744fa0985 util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents: 8627
diff changeset
   563
		it("should not allow done() to be called more than specified", function ()
e88744fa0985 util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents: 8627
diff changeset
   564
			local processed_item;
e88744fa0985 util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents: 8627
diff changeset
   565
			local wait, done;
8633
deade38ffbbd util.async: tests: slight modifications to allow more code reuse in tests
Matthew Wild <mwild1@gmail.com>
parents: 8631
diff changeset
   566
			local r, _, rf = new(function (item)
8631
e88744fa0985 util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents: 8627
diff changeset
   567
				wait, done = async.waiter(4);
e88744fa0985 util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents: 8627
diff changeset
   568
				wait();
e88744fa0985 util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents: 8627
diff changeset
   569
				processed_item = item;
e88744fa0985 util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents: 8627
diff changeset
   570
			end);
e88744fa0985 util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents: 8627
diff changeset
   571
			r:run("test");
e88744fa0985 util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents: 8627
diff changeset
   572
			for i = 1, 4 do
e88744fa0985 util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents: 8627
diff changeset
   573
				done();
e88744fa0985 util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents: 8627
diff changeset
   574
			end
e88744fa0985 util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents: 8627
diff changeset
   575
			assert.has_error(done);;
e88744fa0985 util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents: 8627
diff changeset
   576
			assert.equal(r.state, "ready");
e88744fa0985 util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents: 8627
diff changeset
   577
			assert.equal(processed_item, "test");
e88744fa0985 util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents: 8627
diff changeset
   578
			assert.spy(r.watchers.error).was_not.called();
e88744fa0985 util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents: 8627
diff changeset
   579
		end);
e88744fa0985 util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents: 8627
diff changeset
   580
8627
5325f0e1791b util.async: tests: Ensure done() can be called before wait()
Matthew Wild <mwild1@gmail.com>
parents: 8626
diff changeset
   581
		it("should allow done() to be called before wait()", function ()
5325f0e1791b util.async: tests: Ensure done() can be called before wait()
Matthew Wild <mwild1@gmail.com>
parents: 8626
diff changeset
   582
			local processed_item;
8633
deade38ffbbd util.async: tests: slight modifications to allow more code reuse in tests
Matthew Wild <mwild1@gmail.com>
parents: 8631
diff changeset
   583
			local r, _, rf = new(function (item)
8627
5325f0e1791b util.async: tests: Ensure done() can be called before wait()
Matthew Wild <mwild1@gmail.com>
parents: 8626
diff changeset
   584
				local wait, done = async.waiter();
5325f0e1791b util.async: tests: Ensure done() can be called before wait()
Matthew Wild <mwild1@gmail.com>
parents: 8626
diff changeset
   585
				done();
5325f0e1791b util.async: tests: Ensure done() can be called before wait()
Matthew Wild <mwild1@gmail.com>
parents: 8626
diff changeset
   586
				wait();
5325f0e1791b util.async: tests: Ensure done() can be called before wait()
Matthew Wild <mwild1@gmail.com>
parents: 8626
diff changeset
   587
				processed_item = item;
5325f0e1791b util.async: tests: Ensure done() can be called before wait()
Matthew Wild <mwild1@gmail.com>
parents: 8626
diff changeset
   588
			end);
5325f0e1791b util.async: tests: Ensure done() can be called before wait()
Matthew Wild <mwild1@gmail.com>
parents: 8626
diff changeset
   589
			r:run("test");
5325f0e1791b util.async: tests: Ensure done() can be called before wait()
Matthew Wild <mwild1@gmail.com>
parents: 8626
diff changeset
   590
			assert.equal(processed_item, "test");
5325f0e1791b util.async: tests: Ensure done() can be called before wait()
Matthew Wild <mwild1@gmail.com>
parents: 8626
diff changeset
   591
			assert.equal(r.state, "ready");
5325f0e1791b util.async: tests: Ensure done() can be called before wait()
Matthew Wild <mwild1@gmail.com>
parents: 8626
diff changeset
   592
			-- Since the observable state did not change,
5325f0e1791b util.async: tests: Ensure done() can be called before wait()
Matthew Wild <mwild1@gmail.com>
parents: 8626
diff changeset
   593
			-- the watchers should not have been called
5325f0e1791b util.async: tests: Ensure done() can be called before wait()
Matthew Wild <mwild1@gmail.com>
parents: 8626
diff changeset
   594
			assert.spy(r.watchers.waiting).was_not.called();
5325f0e1791b util.async: tests: Ensure done() can be called before wait()
Matthew Wild <mwild1@gmail.com>
parents: 8626
diff changeset
   595
			assert.spy(r.watchers.ready).was_not.called();
5325f0e1791b util.async: tests: Ensure done() can be called before wait()
Matthew Wild <mwild1@gmail.com>
parents: 8626
diff changeset
   596
		end);
8608
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   597
	end);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   598
end);