Skip to content

Commit 58fe2ef

Browse files
committed
test: update test for new release
1 parent 39c2de6 commit 58fe2ef

File tree

2 files changed

+16
-29
lines changed

2 files changed

+16
-29
lines changed
Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,19 @@
1+
package.loaded["assistant.config"] = { opts = { core = { port = 10043 } } }
2+
13
local tcp = require("assistant.core.tcplistener")
24
local test = require("mini.test")
35

46
local T = test.new_set()
57

68
T["TCPListener"] = function()
7-
---@param cmd string
8-
---@return string?
9-
local function shell(cmd)
10-
local handle = io.popen(cmd)
11-
12-
if not handle then
13-
return nil
14-
end
15-
16-
local result = handle:read("*a")
17-
handle:close()
18-
return result
19-
end
20-
21-
tcp.init()
22-
vim.uv.sleep(100)
23-
local result = shell("netstat -an | grep :10043")
24-
25-
if not result then
26-
test.expect(false, "[SHELL] `netstat` command problem")
27-
else
28-
test.expect.equality(result:match("127%.0%.0%.1:(%d+)"), "10043")
29-
end
9+
tcp.start()
10+
tcp.is_port_in_use("127.0.0.1", 10043, function(first)
11+
test.expect.equality(first, true)
12+
tcp.stop()
13+
tcp.is_port_in_use("127.0.0.1", 10043, function(second)
14+
test.expect.equality(second, false)
15+
end)
16+
end)
3017
end
3118

3219
return T

lua/assistant/core/tcplistener.lua

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ local luv = vim.uv or vim.loop
55

66
local M = {}
77

8-
local function is_port_in_use(host, port, callback)
8+
function M.is_port_in_use(host, port, callback)
99
local check = luv.new_tcp()
1010

1111
if not check then
@@ -26,19 +26,19 @@ local function is_port_in_use(host, port, callback)
2626
end)
2727
end
2828

29-
local function wait_for_unbind(host, port, attempt, max_attempts, callback)
29+
function M.wait_for_unbind(host, port, attempt, max_attempts, callback)
3030
if attempt > max_attempts then
3131
utils.notify_err("Existing server did not unbind in time")
3232
return
3333
end
3434

35-
is_port_in_use(host, port, function(in_use)
35+
M.is_port_in_use(host, port, function(in_use)
3636
if not in_use then
3737
callback()
3838
else
3939
local delay = math.min(1000 * 2 ^ (attempt - 1), 10000)
4040
vim.defer_fn(function()
41-
wait_for_unbind(host, port, attempt + 1, max_attempts, callback)
41+
M.wait_for_unbind(host, port, attempt + 1, max_attempts, callback)
4242
end, delay)
4343
end
4444
end)
@@ -126,7 +126,7 @@ function M.start()
126126
end
127127

128128
function M.init()
129-
is_port_in_use("127.0.0.1", opts.core.port, function(in_use)
129+
M.is_port_in_use("127.0.0.1", opts.core.port, function(in_use)
130130
if in_use then
131131
-- utils.notify_info("Stopping existing server before initializing")
132132
local stop_signal = luv.new_tcp()
@@ -139,7 +139,7 @@ function M.init()
139139
if not err then
140140
stop_signal:write("shutdown", function()
141141
stop_signal:close()
142-
wait_for_unbind("127.0.0.1", opts.core.port, 1, 5, M.start)
142+
M.wait_for_unbind("127.0.0.1", opts.core.port, 1, 5, M.start)
143143
end)
144144
else
145145
M.start()

0 commit comments

Comments
 (0)