diff options
Diffstat (limited to 'tests/mason-core/async/async_spec.lua')
| -rw-r--r-- | tests/mason-core/async/async_spec.lua | 210 |
1 files changed, 95 insertions, 115 deletions
diff --git a/tests/mason-core/async/async_spec.lua b/tests/mason-core/async/async_spec.lua index 61eeeb1b..a4197b85 100644 --- a/tests/mason-core/async/async_spec.lua +++ b/tests/mason-core/async/async_spec.lua @@ -31,51 +31,39 @@ describe("async", function() assert.equals(8, value) end) - it( - "should pass arguments to .run", - async_test(function() - local callback = spy.new() - local start = timestamp() - a.run(a.sleep, callback, 100) - assert.wait_for(function() - assert.spy(callback).was_called(1) - local stop = timestamp() - local grace_ms = 25 - assert.is_true((stop - start) >= (100 - grace_ms)) - end, 150) - end) - ) + it("should pass arguments to .run", function() + local fn = spy.new() + a.run(function(...) + fn(...) + end, spy.new(), 100, 200) + assert.spy(fn).was_called(1) + assert.spy(fn).was_called_with(100, 200) + end) - it( - "should wrap callback-style async functions", - async_test(function() - local stdio = process.in_memory_sink() - local success, exit_code = a.promisify(process.spawn)("env", { - args = {}, - env = { "FOO=BAR", "BAR=BAZ" }, - stdio_sink = stdio.sink, - }) - assert.is_true(success) - assert.equals(0, exit_code) - assert.equals("FOO=BAR\nBAR=BAZ\n", table.concat(stdio.buffers.stdout, "")) - end) - ) + it("should wrap callback-style async functions via promisify", function() + local async_spawn = _.compose(_.table_pack, a.promisify(process.spawn)) + local stdio = process.in_memory_sink() + local success, exit_code = unpack(a.run_blocking(async_spawn, "env", { + args = {}, + env = { "FOO=BAR", "BAR=BAZ" }, + stdio_sink = stdio.sink, + })) + assert.is_true(success) + assert.equals(0, exit_code) + assert.equals("FOO=BAR\nBAR=BAZ\n", table.concat(stdio.buffers.stdout, "")) + end) - it( - "should reject callback-style functions", - async_test(function() - local err = assert.has_error(function() - a.promisify(function(arg1, cb) - cb(arg1, nil) - end, true) "påskmust" - end) - assert.equals(err, "påskmust") + it("should propagate errors in callback-style functions via promisify", function() + local err = assert.has_error(function() + a.run_blocking(a.promisify(function(cb) + cb "Error message." + end, true)) end) - ) + assert.equals(err, "Error message.") + end) - it( - "should return all values", - async_test(function() + it("should return all values from a.wait", function() + a.run_blocking(function() local val1, val2, val3 = a.wait(function(resolve) resolve(1, 2, 3) end) @@ -83,35 +71,32 @@ describe("async", function() assert.equals(2, val2) assert.equals(3, val3) end) - ) + end) - it( - "should cancel coroutine", - async_test(function() - local james_bond = spy.new() - local poutine = a.scope(function() - a.sleep(100) - james_bond() + it("should cancel coroutine", function() + local capture = spy.new() + a.run_blocking(function() + local cancel = a.scope(function() + a.sleep(10) + capture() end)() - poutine() - a.sleep(200) - assert.spy(james_bond).was_not.called() + cancel() + a.sleep(20) end) - ) + assert.spy(capture).was_not.called() + end) - it( - "should raise error if async function raises error", - async_test(function() + it("should raise error if async function raises error", function() + a.run_blocking(function() local err = assert.has.errors(a.promisify(function() error "something went wrong" end)) assert.is_true(match.has_match "something went wrong$"(err)) end) - ) + end) - it( - "should raise error if async function rejects", - async_test(function() + it("should raise error if async function rejects", function() + a.run_blocking(function() local err = assert.has.errors(function() a.wait(function(_, reject) reject "This is an error" @@ -119,18 +104,17 @@ describe("async", function() end) assert.equals("This is an error", err) end) - ) + end) - it( - "should pass nil arguments to promisified functions", - async_test(function() - local fn = spy.new(function(_, _, _, _, _, _, _, cb) - cb() - end) + it("should pass nil arguments to promisified functions", function() + local fn = spy.new(function(_, _, _, _, _, _, _, cb) + cb() + end) + a.run_blocking(function() a.promisify(fn)(nil, 2, nil, 4, nil, nil, 7) - assert.spy(fn).was_called_with(nil, 2, nil, 4, nil, nil, 7, match.is_function()) end) - ) + assert.spy(fn).was_called_with(nil, 2, nil, 4, nil, nil, 7, match.is_function()) + end) it("should accept yielding non-promise values to parent coroutine context", function() local thread = coroutine.create(function(val) @@ -143,60 +127,56 @@ describe("async", function() assert.equals(1337, value) end) - it( - "should run all suspending functions concurrently", - async_test(function() - local start = timestamp() - local function sleep(ms, ret_val) - return function() - a.sleep(ms) - return ret_val - end + it("should run all suspending functions concurrently", function() + local function sleep(ms, ret_val) + return function() + a.sleep(ms) + return ret_val end - local one, two, three, four, five = a.wait_all { + end + local start = timestamp() + local one, two, three, four, five = unpack(a.run_blocking(function() + return _.table_pack(a.wait_all { sleep(100, 1), sleep(100, "two"), sleep(100, "three"), sleep(100, 4), sleep(100, 5), - } - local grace = 50 - local delta = timestamp() - start - assert.is_true(delta <= (100 + grace)) - assert.is_true(delta >= (100 - grace)) - assert.equals(1, one) - assert.equals("two", two) - assert.equals("three", three) - assert.equals(4, four) - assert.equals(5, five) - end) - ) + }) + end)) + local grace = 50 + local delta = timestamp() - start + assert.is_true(delta <= (100 + grace)) + assert.is_true(delta >= (100 - grace)) + assert.equals(1, one) + assert.equals("two", two) + assert.equals("three", three) + assert.equals(4, four) + assert.equals(5, five) + end) - it( - "should run all suspending functions concurrently", - async_test(function() - local start = timestamp() - local called = spy.new() - local function sleep(ms, ret_val) - return function() - a.sleep(ms) - called() - return ret_val - end + it("should run all suspending functions concurrently", function() + local start = timestamp() + local called = spy.new() + local function sleep(ms, ret_val) + return function() + a.sleep(ms) + called() + return ret_val end - local first = a.wait_first { - sleep(150, 1), - sleep(50, "first"), - sleep(150, "three"), - sleep(150, 4), - sleep(150, 5), - } - local grace = 20 - local delta = timestamp() - start - assert.is_true(delta <= (50 + grace)) - assert.equals("first", first) - end) - ) + end + local first = a.run_blocking(a.wait_first, { + sleep(150, 1), + sleep(50, "first"), + sleep(150, "three"), + sleep(150, 4), + sleep(150, 5), + }) + local grace = 20 + local delta = timestamp() - start + assert.is_true(delta <= (50 + grace)) + assert.equals("first", first) + end) it("should yield back immediately when not providing any functions", function() assert.is_nil(a.wait_first {}) |
