Skip to content

Commit dd7853f

Browse files
committed
增加一个测试
1 parent f4f3f1a commit dd7853f

File tree

2 files changed

+80
-28
lines changed

2 files changed

+80
-28
lines changed

binding/lua_channel.cpp

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,46 +19,37 @@ namespace bee::lua_channel {
1919
using box = std::shared_ptr<channel>;
2020

2121
bool init() noexcept {
22-
if (!ev.open()) {
23-
return false;
24-
}
25-
return true;
22+
return ev.open();
2623
}
2724
net::fd_t fd() const noexcept {
2825
return ev.fd();
2926
}
30-
void push(lua_State* L, int from) noexcept {
31-
void* data = seri_pack(L, from, NULL);
27+
void push(void* data) noexcept {
3228
std::unique_lock<spinlock> lk(mutex);
3329
queue.push(data);
3430
ev.set();
3531
}
36-
int pop(lua_State* L) noexcept {
37-
void* data;
38-
{
39-
std::unique_lock<spinlock> lk(mutex);
40-
if (queue.empty()) {
41-
ev.clear();
42-
lua_pushboolean(L, 0);
43-
return 1;
44-
}
45-
data = queue.front();
46-
queue.pop();
32+
void* pop() noexcept {
33+
std::unique_lock<spinlock> lk(mutex);
34+
if (queue.empty()) {
35+
ev.clear();
36+
return nullptr;
4737
}
48-
lua_pushboolean(L, 1);
49-
return 1 + seri_unpackptr(L, data);
38+
void* data = queue.front();
39+
queue.pop();
40+
if (queue.empty()) {
41+
ev.clear();
42+
}
43+
return data;
5044
}
5145
void clear() noexcept {
5246
std::unique_lock<spinlock> lk(mutex);
53-
for (;;) {
54-
if (queue.empty()) {
55-
ev.clear();
56-
return;
57-
}
47+
while (!queue.empty()) {
5848
void* data = queue.front();
5949
free(data);
6050
queue.pop();
6151
}
52+
ev.clear();
6253
}
6354

6455
private:
@@ -110,14 +101,21 @@ namespace bee::lua_channel {
110101
static channelmgr g_channel;
111102

112103
static int lchannel_push(lua_State* L) {
113-
auto& bc = lua::checkudata<channel::box>(L, 1);
114-
bc->push(L, 1);
104+
auto& bc = lua::checkudata<channel::box>(L, 1);
105+
void* data = seri_pack(L, 1, NULL);
106+
bc->push(data);
115107
return 0;
116108
}
117109

118110
static int lchannel_pop(lua_State* L) {
119-
auto& bc = lua::checkudata<channel::box>(L, 1);
120-
return bc->pop(L);
111+
auto& bc = lua::checkudata<channel::box>(L, 1);
112+
void* data = bc->pop();
113+
if (!data) {
114+
lua_pushboolean(L, 0);
115+
return 1;
116+
}
117+
lua_pushboolean(L, 1);
118+
return 1 + seri_unpackptr(L, data);
121119
}
122120

123121
static int lchannel_fd(lua_State* L) {

test/test_channel.lua

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,3 +213,57 @@ function test_channel:test_fd()
213213
channel.destroy "testRes"
214214
assertNotThreadError()
215215
end
216+
217+
function test_channel:test_fd_2()
218+
assertNotThreadError()
219+
local req = channel.create "testReq"
220+
local res = channel.create "testRes"
221+
local thd = thread.create [[
222+
local thread = require "bee.thread"
223+
local channel = require "bee.channel"
224+
local epoll = require "bee.epoll"
225+
local req = channel.query "testReq"
226+
local res = channel.query "testRes"
227+
local function dispatch(ok, what, ...)
228+
if not ok then
229+
return 1
230+
end
231+
if what == "exit" then
232+
return 0
233+
end
234+
res:push(what, ...)
235+
end
236+
while true do
237+
local r = dispatch(req:pop())
238+
thread.sleep(1)
239+
if r == 0 then
240+
return
241+
elseif r == 1 then
242+
thread.sleep(0)
243+
end
244+
end
245+
]]
246+
local expected = {}
247+
local epfd <close> = epoll.create(16)
248+
epfd:event_add(res:fd(), epoll.EPOLLIN)
249+
TestSuit(function (...)
250+
req:push(...)
251+
expected[#expected+1] = table.pack(true, ...)
252+
end)
253+
req:push "exit"
254+
while #expected > 0 do
255+
for _, event in epfd:wait() do
256+
if event & (epoll.EPOLLERR | epoll.EPOLLHUP) ~= 0 then
257+
lt.failure("unknown error")
258+
end
259+
if event & epoll.EPOLLIN ~= 0 then
260+
local actual = table.pack(res:pop())
261+
lt.assertEquals(actual, table.remove(expected, 1))
262+
end
263+
end
264+
end
265+
thread.wait(thd)
266+
channel.destroy "testReq"
267+
channel.destroy "testRes"
268+
assertNotThreadError()
269+
end

0 commit comments

Comments
 (0)