Skip to content

Commit

Permalink
Add support for lua watch tree after quiet
Browse files Browse the repository at this point in the history
  • Loading branch information
carlgsmith committed Oct 5, 2023
1 parent 878ddb0 commit 33f87d8
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
1 change: 1 addition & 0 deletions apteryx.c
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ dw_add (uint64_t ref, void *fn, GNode *root, void *data, guint timeout_ms)
dw->ref = ref;
dw->fn = fn;
dw->root = root;
dw->data = data;
dw->timeout_ms = timeout_ms;
dw_list = g_list_append (dw_list, dw);
dw->handle = rpc_add_callback (rpc, dw_process, (gpointer) dw, timeout_ms);
Expand Down
5 changes: 4 additions & 1 deletion lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -614,8 +614,11 @@ lua_apteryx_watch_tree (lua_State *L)
luaL_checktype (L, 2, LUA_TFUNCTION);
const char *path = lua_tostring (L, 1);
size_t ref = ref_callback (L, 2);
lua_Integer timeout_ms = 0;
if (lua_gettop (L) == 3 && lua_isinteger (L, 3))
timeout_ms = lua_tointeger (L, 3);

if (!add_callback (APTERYX_WATCHERS_PATH, path, (void *)lua_do_watch_tree, true, (void *) ref, 1, 0))
if (!add_callback (APTERYX_WATCHERS_PATH, path, (void *)lua_do_watch_tree, true, (void *) ref, 1, (uint64_t) timeout_ms))
{
luaL_error (L, "Failed to register watch\n");
lua_pushboolean (L, false);
Expand Down
44 changes: 44 additions & 0 deletions test.c
Original file line number Diff line number Diff line change
Expand Up @@ -7911,6 +7911,49 @@ test_lua_watch_tree (void)
CU_ASSERT (assert_apteryx_empty ());
}

void
test_lua_watch_tree_after_quiet (void)
{
CU_ASSERT (_run_lua (
"apteryx = require('apteryx') \n"
"tree = nil \n"
"cb_count = 0 \n"
"function test_watch (t) "
" cb_count = cb_count + 1 "
" tree = t "
"end \n"
"apteryx.watch_tree('"TEST_PATH"/zones/*', test_watch, 50) \n"
"apteryx.process() \n"
"apteryx.set('"TEST_PATH"/zones/zone1', '1') \n"
"apteryx.process() \n"
"apteryx.set('"TEST_PATH"/zones/zone2', '2') \n"
"apteryx.process() \n"
"apteryx.set('"TEST_PATH"/zones/zone3', '3') \n"
"apteryx.process() \n"
"apteryx.set('"TEST_PATH"/zones/zone4', '4') \n"
"apteryx.process() \n"
"apteryx.set('"TEST_PATH"/zones/zone5', '5') \n"
"apteryx.process() \n"
"apteryx.set('"TEST_PATH"/zones/zone5', '6') \n"
"apteryx.process() \n"
"apteryx.set('"TEST_PATH"/zones/zone3', '') \n"
"apteryx.process() \n"
"os.execute('sleep 0.1') \n"
"apteryx.process() \n"
"assert (cb_count == 1) \n"
"assert (tree and tree.test and tree.test.zones) \n"
"assert (tree.test.zones.zone1 == '1') \n"
"assert (tree.test.zones.zone2 == '2') \n"
"assert (tree.test.zones.zone3 == '') \n"
"assert (tree.test.zones.zone4 == '4') \n"
"assert (tree.test.zones.zone5 == '6') \n"
"apteryx.unwatch('"TEST_PATH"/zones/*', test_watch) \n"
"apteryx.prune('"TEST_PATH"/zones') \n"
"apteryx.process(false) \n"
));
CU_ASSERT (assert_apteryx_empty ());
}

static int
test_lua_refresh_thread (void *data)
{
Expand Down Expand Up @@ -8540,6 +8583,7 @@ CU_TestInfo tests_lua[] = {
{ "lua basic watch", test_lua_basic_watch },
{ "lua multiple watchers", test_lua_multiple_watchers },
{ "lua watch tree", test_lua_watch_tree },
{ "lua watch tree after quiet", test_lua_watch_tree_after_quiet },
{ "lua basic refresh", test_lua_basic_refresh },
{ "lua basic provide", test_lua_basic_provide },
{ "lua basic index", test_lua_basic_index },
Expand Down

0 comments on commit 33f87d8

Please sign in to comment.