mirror of
https://github.com/wassname/ray.git
synced 2026-07-30 12:30:30 +08:00
Object table subscribe with new semantics (#62)
* new plasma subscribe implementation * object table subscribe with test * clang-format * fix * fix test * fix tests * fix clang-format * add check * final clang-format * final fixes * fix clang-format
This commit is contained in:
committed by
Robert Nishihara
parent
bc1d7db926
commit
c7073d623b
@@ -387,9 +387,12 @@ int64_t reconnect_sub_context_callback(event_loop *loop,
|
||||
db_handle *db = context;
|
||||
/* Reconnect to redis. This is not reconnecting the pub/sub channel. */
|
||||
redisAsyncFree(db->sub_context);
|
||||
redisAsyncFree(db->context);
|
||||
redisFree(db->sync_context);
|
||||
db->sub_context = redisAsyncConnect("127.0.0.1", 6379);
|
||||
db->sub_context->data = (void *) db;
|
||||
db->context = redisAsyncConnect("127.0.0.1", 6379);
|
||||
db->context->data = (void *) db;
|
||||
db->sync_context = redisConnect("127.0.0.1", 6379);
|
||||
/* Re-attach the database to the event loop (the file descriptor changed). */
|
||||
db_attach(db, loop);
|
||||
@@ -631,6 +634,137 @@ TEST subscribe_success_test(void) {
|
||||
PASS();
|
||||
}
|
||||
|
||||
/* Test if subscribe succeeds if the object is already present. */
|
||||
|
||||
const char *subscribe_object_present_context = "subscribe_object_present";
|
||||
int subscribe_object_present_succeeded = 0;
|
||||
|
||||
void subscribe_object_present_object_available_callback(object_id object_id,
|
||||
void *user_context) {
|
||||
CHECK(user_context == (void *) subscribe_object_present_context);
|
||||
subscribe_object_present_succeeded = 1;
|
||||
}
|
||||
|
||||
TEST subscribe_object_present_test(void) {
|
||||
g_loop = event_loop_create();
|
||||
db_handle *db =
|
||||
db_connect("127.0.0.1", 6379, "plasma_manager", "127.0.0.1", 11236);
|
||||
db_attach(db, g_loop);
|
||||
unique_id id = globally_unique_id();
|
||||
retry_info retry = {
|
||||
.num_retries = 0, .timeout = 100, .fail_callback = NULL,
|
||||
};
|
||||
object_table_add(db, id, &retry, NULL, NULL);
|
||||
object_table_subscribe(
|
||||
db, id, subscribe_object_present_object_available_callback,
|
||||
(void *) subscribe_object_present_context, &retry, NULL, (void *) db);
|
||||
|
||||
/* Install handler for terminating the event loop. */
|
||||
event_loop_add_timer(g_loop, 750,
|
||||
(event_loop_timer_handler) terminate_event_loop_callback,
|
||||
NULL);
|
||||
|
||||
event_loop_run(g_loop);
|
||||
db_disconnect(db);
|
||||
destroy_outstanding_callbacks(g_loop);
|
||||
event_loop_destroy(g_loop);
|
||||
ASSERT(subscribe_object_present_succeeded == 1);
|
||||
PASS();
|
||||
}
|
||||
|
||||
/* Test if subscribe is not called if object is not present. */
|
||||
|
||||
const char *subscribe_object_not_present_context =
|
||||
"subscribe_object_not_present";
|
||||
int subscribe_object_not_present_succeeded = 0;
|
||||
|
||||
void subscribe_object_not_present_object_available_callback(
|
||||
object_id object_id,
|
||||
void *user_context) {
|
||||
CHECK(user_context == (void *) subscribe_object_not_present_context);
|
||||
subscribe_object_not_present_succeeded = 1;
|
||||
}
|
||||
|
||||
TEST subscribe_object_not_present_test(void) {
|
||||
g_loop = event_loop_create();
|
||||
db_handle *db =
|
||||
db_connect("127.0.0.1", 6379, "plasma_manager", "127.0.0.1", 11236);
|
||||
db_attach(db, g_loop);
|
||||
unique_id id = globally_unique_id();
|
||||
retry_info retry = {
|
||||
.num_retries = 0, .timeout = 100, .fail_callback = NULL,
|
||||
};
|
||||
object_table_subscribe(
|
||||
db, id, subscribe_object_not_present_object_available_callback,
|
||||
(void *) subscribe_object_not_present_context, &retry, NULL, (void *) db);
|
||||
|
||||
/* Install handler for terminating the event loop. */
|
||||
event_loop_add_timer(g_loop, 750,
|
||||
(event_loop_timer_handler) terminate_event_loop_callback,
|
||||
NULL);
|
||||
|
||||
event_loop_run(g_loop);
|
||||
db_disconnect(db);
|
||||
destroy_outstanding_callbacks(g_loop);
|
||||
event_loop_destroy(g_loop);
|
||||
ASSERT(subscribe_object_not_present_succeeded == 0);
|
||||
PASS();
|
||||
}
|
||||
|
||||
/* Test if subscribe is called if object becomes available later. */
|
||||
|
||||
const char *subscribe_object_available_later_context =
|
||||
"subscribe_object_available_later";
|
||||
int subscribe_object_available_later_succeeded = 0;
|
||||
|
||||
void subscribe_object_available_later_object_available_callback(
|
||||
object_id object_id,
|
||||
void *user_context) {
|
||||
CHECK(user_context == (void *) subscribe_object_available_later_context);
|
||||
/* Make sure the callback is only called once. */
|
||||
subscribe_object_available_later_succeeded += 1;
|
||||
}
|
||||
|
||||
int64_t add_object_callback(event_loop *loop, int64_t timer_id, void *context) {
|
||||
db_handle *db = (db_handle *) context;
|
||||
retry_info retry = {
|
||||
.num_retries = 0, .timeout = 100, .fail_callback = NULL,
|
||||
};
|
||||
object_table_add(db, NIL_ID, &retry, NULL, NULL);
|
||||
/* Reset the timer to this large value, so it doesn't trigger again. */
|
||||
return 10000;
|
||||
}
|
||||
|
||||
TEST subscribe_object_available_later_test(void) {
|
||||
g_loop = event_loop_create();
|
||||
db_handle *db =
|
||||
db_connect("127.0.0.1", 6379, "plasma_manager", "127.0.0.1", 11236);
|
||||
db_attach(db, g_loop);
|
||||
unique_id id = NIL_ID;
|
||||
retry_info retry = {
|
||||
.num_retries = 0, .timeout = 100, .fail_callback = NULL,
|
||||
};
|
||||
object_table_subscribe(
|
||||
db, id, subscribe_object_available_later_object_available_callback,
|
||||
(void *) subscribe_object_available_later_context, &retry, NULL,
|
||||
(void *) db);
|
||||
|
||||
event_loop_add_timer(g_loop, 300,
|
||||
(event_loop_timer_handler) add_object_callback, db);
|
||||
|
||||
/* Install handler for terminating the event loop. */
|
||||
event_loop_add_timer(g_loop, 750,
|
||||
(event_loop_timer_handler) terminate_event_loop_callback,
|
||||
NULL);
|
||||
|
||||
event_loop_run(g_loop);
|
||||
db_disconnect(db);
|
||||
destroy_outstanding_callbacks(g_loop);
|
||||
event_loop_destroy(g_loop);
|
||||
ASSERT(subscribe_object_available_later_succeeded == 1);
|
||||
PASS();
|
||||
}
|
||||
|
||||
SUITE(object_table_tests) {
|
||||
RUN_REDIS_TEST(new_object_test);
|
||||
RUN_REDIS_TEST(new_object_no_task_test);
|
||||
@@ -644,6 +778,9 @@ SUITE(object_table_tests) {
|
||||
RUN_REDIS_TEST(add_late_test);
|
||||
RUN_REDIS_TEST(subscribe_late_test);
|
||||
RUN_REDIS_TEST(subscribe_success_test);
|
||||
RUN_REDIS_TEST(subscribe_object_present_test);
|
||||
RUN_REDIS_TEST(subscribe_object_not_present_test);
|
||||
RUN_REDIS_TEST(subscribe_object_available_later_test);
|
||||
}
|
||||
|
||||
GREATEST_MAIN_DEFS();
|
||||
|
||||
Reference in New Issue
Block a user