Skip to content

Commit 0b674d5

Browse files
committed
update test
1 parent a3c8432 commit 0b674d5

File tree

4 files changed

+61
-4
lines changed

4 files changed

+61
-4
lines changed

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"type": "lua",
2727
"request": "attach",
2828
"stopOnEntry": false,
29-
"address": "127.0.0.1:11413",
29+
"address": "127.0.0.1:11414",
3030
"outputCapture": [
3131
],
3232
"sourceFormat": "string",

3rd/bee.lua

channel_flow_test.lua

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
local channel = require 'bee.channel'
2+
local thread = require 'bee.thread'
3+
local select = require 'bee.select'
4+
local time = require 'bee.time'
5+
6+
-- 极简复现:只保留必要前提与关键路径;所有时间单位为毫秒。
7+
8+
-- 独立测试通道,避免与服务通道冲突
9+
local taskpad = channel.create('test_taskpad_flow')
10+
local waiter = channel.create('test_waiter_flow')
11+
12+
-- 启动 4 个子线程:阻塞抢任务;其中处理 initial(2) 的线程延迟 100ms 后回复;收到 post 立即回复。
13+
local WORKER_COUNT = 2
14+
local worker_src = [[
15+
local channel = require 'bee.channel'
16+
local select = require 'bee.select'
17+
local thread = require 'bee.thread'
18+
local taskpad = channel.query('test_taskpad_flow')
19+
local waiter = channel.query('test_waiter_flow')
20+
local sel = select.create(); sel:event_add(taskpad:fd(), select.SELECT_READ)
21+
while true do
22+
local ok, name, id = taskpad:pop()
23+
if not ok then sel:wait(-1) else
24+
if name == '__end' then break end
25+
if name == 'initial' and id == 2 then
26+
waiter:push(0, 'reply', id)
27+
elseif name == 'post' then
28+
waiter:push(0, 'postDone', id)
29+
end
30+
end
31+
end
32+
]]
33+
for i = 1, WORKER_COUNT do
34+
assert(thread.create(worker_src))
35+
end
36+
37+
-- 主线程:推送 2 个 initial 任务
38+
taskpad:push('initial', 2, nil)
39+
40+
-- 等待 reply(阻塞式,毫秒超时步进)
41+
local sel = select.create()
42+
sel:event_add(waiter:fd(), select.SELECT_READ)
43+
while true do
44+
local ok, _, name = waiter:pop()
45+
if not ok then
46+
sel:wait(100)
47+
else
48+
if name == 'reply' then
49+
break
50+
end
51+
end
52+
end
53+
54+
-- 立即发送 post 任务,观察是否卡住
55+
print('[INFO] sending post task immediately after reply')
56+
taskpad:push('post', 3, nil)
57+
print('[INFO] sending post task done')

script/brave/brave.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ function m.start(privatePad)
5151
local resPad = privatePad and channelMod.query('res:' .. privatePad) or waiter
5252
local selector = selectMod.create()
5353
selector:event_add(reqPad:fd(), selectMod.SELECT_READ)
54-
54+
5555
m.push('mem', collectgarbage 'count')
5656
while true do
5757
-- 使用 select 实现阻塞等待
@@ -64,7 +64,7 @@ function m.start(privatePad)
6464
end
6565
selector:wait(-1)
6666
end
67-
67+
6868
local ability = m.ability[name]
6969
-- TODO
7070
if not ability then

0 commit comments

Comments
 (0)