Skip to content

Commit

Permalink
remove channel.reset
Browse files Browse the repository at this point in the history
  • Loading branch information
actboy168 committed May 7, 2024
1 parent 8dfb202 commit de3f4c8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
23 changes: 13 additions & 10 deletions binding/lua_channel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,14 @@ namespace bee::lua_channel {
}
return r->second;
}
void clear() noexcept {
void destroy(zstring_view name) noexcept {
std::unique_lock<spinlock> lk(mutex);
for (const auto& [_, c] : channels) {
c->clear();
std::string namestr { name.data(), name.size() };
auto it = channels.find(namestr);
if (it != channels.end()) {
it->second->clear();
channels.erase(it);
}
channels.clear();
}
channel::box query(zstring_view name) noexcept {
std::unique_lock<spinlock> lk(mutex);
Expand Down Expand Up @@ -147,6 +149,12 @@ namespace bee::lua_channel {
return 1;
}

static int ldestroy(lua_State* L) {
auto name = lua::checkstrview(L, 1);
g_channel.destroy(name);
return 0;
}

static int lquery(lua_State* L) {
auto name = lua::checkstrview(L, 1);
channel::box c = g_channel.query(name);
Expand All @@ -157,20 +165,15 @@ namespace bee::lua_channel {
return 1;
}

static int lreset(lua_State* L) {
g_channel.clear();
return 0;
}

static int luaopen(lua_State* L) {
if (!net::socket::initialize()) {
lua_pushstring(L, error::sys_errmsg("initialize").c_str());
return lua_error(L);
}
luaL_Reg lib[] = {
{ "create", lcreate },
{ "destroy", ldestroy },
{ "query", lquery },
{ "reset", lreset },
{ NULL, NULL },
};
luaL_newlibtable(L, lib);
Expand Down
22 changes: 10 additions & 12 deletions test/test_channel.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,25 @@ end
local test_channel = lt.test "channel"

function test_channel:test_create()
channel.reset()
lt.assertErrorMsgEquals("Can't query channel 'test'", channel.query, "test")
channel.create "test"
lt.assertIsUserdata(channel.query "test")
lt.assertIsUserdata(channel.query "test")
channel.reset()
channel.destroy "test"
channel.create "test"
lt.assertErrorMsgEquals("Duplicate channel 'test'", channel.create, "test")
channel.reset()
channel.destroy "test"
end

function test_channel:test_reset_1()
channel.reset()
lt.assertErrorMsgEquals("Can't query channel 'test'", channel.query, "test")
channel.create "test"
lt.assertIsUserdata(channel.query "test")
channel.reset()
channel.destroy "test"
lt.assertErrorMsgEquals("Can't query channel 'test'", channel.query, "test")
channel.create "test"
lt.assertIsUserdata(channel.query "test")
channel.reset()
channel.destroy "test"
end

local function TestSuit(f)
Expand All @@ -48,7 +46,6 @@ local function TestSuit(f)
end

function test_channel:test_pop_1()
channel.reset()
local chan = channel.create "test"
local function pack_pop(ok, ...)
lt.assertEquals(ok, true)
Expand All @@ -60,10 +57,10 @@ function test_channel:test_pop_1()
end
TestSuit(test_ok)
-- 基本和serialization的测试重复,所以failed就不测了
channel.destroy "test"
end

function test_channel:test_pop_2()
channel.reset()
local chan = channel.create "test"

local function assertIs(expected)
Expand Down Expand Up @@ -98,12 +95,10 @@ function test_channel:test_pop_2()
assertIs(1025)
assertIs(1026)
assertEmpty()

channel.reset()
channel.destroy "test"
end

function test_channel:test_pop_3()
channel.reset()
assertNotThreadError()
local req = channel.create "testReq"
local res = channel.create "testRes"
Expand Down Expand Up @@ -146,11 +141,12 @@ function test_channel:test_pop_3()
TestSuit(test_ok)
req:push "exit"
thread.wait(thd)
channel.destroy "testReq"
channel.destroy "testRes"
assertNotThreadError()
end

function test_channel:test_fd()
channel.reset()
assertNotThreadError()
local req = channel.create "testReq"
local res = channel.create "testRes"
Expand Down Expand Up @@ -213,5 +209,7 @@ function test_channel:test_fd()
TestSuit(test_ok)
req:push "exit"
thread.wait(thd)
channel.destroy "testReq"
channel.destroy "testRes"
assertNotThreadError()
end

0 comments on commit de3f4c8

Please sign in to comment.