aboutsummaryrefslogtreecommitdiffstats
path: root/tests/core/async_spec.lua
blob: 7d965e9ba877aa97945ef7839839934c9d987038 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
local assert = require "luassert"
local spy = require "luassert.spy"
local match = require "luassert.match"
local a = require "nvim-lsp-installer.core.async"
local process = require "nvim-lsp-installer.process"

local function timestamp()
    local seconds, microseconds = vim.loop.gettimeofday()
    return (seconds * 1000) + math.floor(microseconds / 1000)
end

describe("async", function()
    it("should run in blocking mode", function()
        local start = timestamp()
        a.run_blocking(function()
            a.sleep(1000)
        end)
        local stop = timestamp()
        local grace_ms = 25
        assert.is_true((stop - start) >= (1000 - grace_ms))
    end)

    it(
        "should wrap callback-style async functions",
        async_test(function()
            local stdio = process.in_memory_sink()
            local ok, success = a.promisify(process.spawn)("env", {
                args = {},
                env = { "FOO=BAR", "BAR=BAZ" },
                stdio_sink = stdio.sink,
            })
            assert.is_true(ok)
            assert.is_true(success)
            assert.equals("FOO=BAR\nBAR=BAZ\n", table.concat(stdio.buffers.stdout, ""))
        end)
    )

    it(
        "should cancel coroutine",
        async_test(function()
            local james_bond = spy.new()
            local poutine = a.scope(function()
                a.sleep(100)
                james_bond()
            end)()
            poutine()
            a.sleep(200)
            assert.spy(james_bond).was_not.called()
        end)
    )

    it(
        "should reject if async function raises error",
        async_test(function()
            local ok, err = a.promisify(function()
                error "something went wrong"
            end)()
            assert.is_false(ok)
            assert.is_true(match.has_match "something went wrong$"(err))
        end)
    )
end)