mirror of
https://github.com/wassname/ray.git
synced 2026-07-28 11:25:04 +08:00
Merge task table and task log into a single table (#30)
* Merge task table and task log * Fix test in db tests * Address Robert's comments and some better error checking * Add a LOG_FATAL that exits the program
This commit is contained in:
committed by
Philipp Moritz
parent
194bdb1d96
commit
9d1e750e8f
+62
-35
@@ -8,7 +8,7 @@
|
||||
#include "test_common.h"
|
||||
#include "state/db.h"
|
||||
#include "state/object_table.h"
|
||||
#include "state/task_log.h"
|
||||
#include "state/task_table.h"
|
||||
#include "state/redis.h"
|
||||
#include "task.h"
|
||||
|
||||
@@ -54,7 +54,7 @@ void lookup_done_callback(object_id object_id,
|
||||
void add_done_callback(object_id object_id, void *user_context) {}
|
||||
|
||||
/* Test if we got a timeout callback if we couldn't connect database. */
|
||||
void timeout_callback(object_id object_id, void *context) {
|
||||
void timeout_callback(object_id object_id, void *context, void *user_data) {
|
||||
user_context *uc = (user_context *) context;
|
||||
CHECK(uc->test_number == TEST_NUMBER)
|
||||
}
|
||||
@@ -101,71 +101,98 @@ TEST object_table_lookup_test(void) {
|
||||
PASS();
|
||||
}
|
||||
|
||||
void task_log_test_callback(task_instance *instance, void *userdata) {
|
||||
task_instance *other = userdata;
|
||||
CHECK(*task_instance_state(instance) == TASK_STATUS_SCHEDULED);
|
||||
CHECK(task_instance_size(instance) == task_instance_size(other));
|
||||
CHECK(memcmp(instance, other, task_instance_size(instance)) == 0);
|
||||
int task_table_test_callback_called = 0;
|
||||
task *task_table_test_task;
|
||||
|
||||
void task_table_test_fail_callback(unique_id id,
|
||||
void *context,
|
||||
void *user_data) {
|
||||
event_loop *loop = user_data;
|
||||
event_loop_stop(loop);
|
||||
}
|
||||
|
||||
TEST task_log_test(void) {
|
||||
int64_t task_table_delayed_add_task(event_loop *loop,
|
||||
int64_t id,
|
||||
void *context) {
|
||||
db_handle *db = context;
|
||||
retry_info retry = {
|
||||
.num_retries = NUM_RETRIES,
|
||||
.timeout = TIMEOUT,
|
||||
.fail_callback = task_table_test_fail_callback,
|
||||
};
|
||||
task_table_add_task(db, task_table_test_task, &retry, NULL, (void *) loop);
|
||||
return EVENT_LOOP_TIMER_DONE;
|
||||
}
|
||||
|
||||
void task_table_test_callback(task *callback_task, void *user_data) {
|
||||
task_table_test_callback_called = 1;
|
||||
CHECK(task_state(callback_task) == TASK_STATUS_SCHEDULED);
|
||||
CHECK(task_size(callback_task) == task_size(task_table_test_task));
|
||||
CHECK(memcmp(callback_task, task_table_test_task, task_size(callback_task)) ==
|
||||
0);
|
||||
event_loop *loop = user_data;
|
||||
event_loop_stop(loop);
|
||||
}
|
||||
|
||||
TEST task_table_test(void) {
|
||||
task_table_test_callback_called = 0;
|
||||
event_loop *loop = event_loop_create();
|
||||
db_handle *db = db_connect("127.0.0.1", 6379, "local_scheduler", "", -1);
|
||||
db_attach(db, loop);
|
||||
node_id node = globally_unique_id();
|
||||
task_spec *task = example_task();
|
||||
task_instance *instance = make_task_instance(globally_unique_id(), task,
|
||||
TASK_STATUS_SCHEDULED, node);
|
||||
task_spec *spec = example_task_spec();
|
||||
task_table_test_task = alloc_task(spec, TASK_STATUS_SCHEDULED, node);
|
||||
free_task_spec(spec);
|
||||
retry_info retry = {
|
||||
.num_retries = NUM_RETRIES, .timeout = TIMEOUT, .fail_callback = NULL,
|
||||
.num_retries = NUM_RETRIES,
|
||||
.timeout = TIMEOUT,
|
||||
.fail_callback = task_table_test_fail_callback,
|
||||
};
|
||||
task_log_subscribe(db, node, TASK_STATUS_SCHEDULED, task_log_test_callback,
|
||||
instance, &retry, NULL, NULL);
|
||||
task_log_publish(db, instance, &retry, NULL, NULL);
|
||||
event_loop_add_timer(loop, 200, (event_loop_timer_handler) timeout_handler,
|
||||
NULL);
|
||||
task_table_subscribe(db, node, TASK_STATUS_SCHEDULED,
|
||||
task_table_test_callback, (void *) loop, &retry, NULL,
|
||||
(void *) loop);
|
||||
event_loop_add_timer(
|
||||
loop, 200, (event_loop_timer_handler) task_table_delayed_add_task, db);
|
||||
event_loop_run(loop);
|
||||
task_instance_free(instance);
|
||||
free_task_spec(task);
|
||||
free_task(task_table_test_task);
|
||||
db_disconnect(db);
|
||||
destroy_outstanding_callbacks(loop);
|
||||
event_loop_destroy(loop);
|
||||
ASSERT(task_table_test_callback_called);
|
||||
PASS();
|
||||
}
|
||||
|
||||
int num_test_callback_called = 0;
|
||||
|
||||
void task_log_all_test_callback(task_instance *instance, void *userdata) {
|
||||
void task_table_all_test_callback(task *task, void *user_data) {
|
||||
num_test_callback_called += 1;
|
||||
}
|
||||
|
||||
TEST task_log_all_test(void) {
|
||||
TEST task_table_all_test(void) {
|
||||
event_loop *loop = event_loop_create();
|
||||
db_handle *db = db_connect("127.0.0.1", 6379, "local_scheduler", "", -1);
|
||||
db_attach(db, loop);
|
||||
task_spec *task = example_task();
|
||||
task_spec *spec = example_task_spec();
|
||||
/* Schedule two tasks on different nodes. */
|
||||
task_instance *instance1 = make_task_instance(
|
||||
globally_unique_id(), task, TASK_STATUS_SCHEDULED, globally_unique_id());
|
||||
task_instance *instance2 = make_task_instance(
|
||||
globally_unique_id(), task, TASK_STATUS_SCHEDULED, globally_unique_id());
|
||||
task *task1 = alloc_task(spec, TASK_STATUS_SCHEDULED, globally_unique_id());
|
||||
task *task2 = alloc_task(spec, TASK_STATUS_SCHEDULED, globally_unique_id());
|
||||
retry_info retry = {
|
||||
.num_retries = NUM_RETRIES, .timeout = TIMEOUT, .fail_callback = NULL,
|
||||
};
|
||||
task_log_subscribe(db, NIL_ID, TASK_STATUS_SCHEDULED,
|
||||
task_log_all_test_callback, NULL, &retry, NULL, NULL);
|
||||
task_table_subscribe(db, NIL_ID, TASK_STATUS_SCHEDULED,
|
||||
task_table_all_test_callback, NULL, &retry, NULL, NULL);
|
||||
event_loop_add_timer(loop, 50, (event_loop_timer_handler) timeout_handler,
|
||||
NULL);
|
||||
event_loop_run(loop);
|
||||
/* TODO(pcm): Get rid of this sleep once the robust pubsub is implemented. */
|
||||
task_log_publish(db, instance1, &retry, NULL, NULL);
|
||||
task_log_publish(db, instance2, &retry, NULL, NULL);
|
||||
task_table_update(db, task1, &retry, NULL, NULL);
|
||||
task_table_update(db, task2, &retry, NULL, NULL);
|
||||
event_loop_add_timer(loop, 200, (event_loop_timer_handler) timeout_handler,
|
||||
NULL);
|
||||
event_loop_run(loop);
|
||||
task_instance_free(instance2);
|
||||
task_instance_free(instance1);
|
||||
free_task_spec(task);
|
||||
free(task2);
|
||||
free(task1);
|
||||
free_task_spec(spec);
|
||||
db_disconnect(db);
|
||||
destroy_outstanding_callbacks(loop);
|
||||
event_loop_destroy(loop);
|
||||
@@ -198,8 +225,8 @@ TEST unique_client_id_test(void) {
|
||||
|
||||
SUITE(db_tests) {
|
||||
RUN_REDIS_TEST(object_table_lookup_test);
|
||||
RUN_REDIS_TEST(task_log_test);
|
||||
RUN_REDIS_TEST(task_log_all_test);
|
||||
RUN_REDIS_TEST(task_table_test);
|
||||
RUN_REDIS_TEST(task_table_all_test);
|
||||
RUN_REDIS_TEST(unique_client_id_test);
|
||||
}
|
||||
|
||||
|
||||
@@ -12,6 +12,132 @@ SUITE(object_table_tests);
|
||||
|
||||
static event_loop *g_loop;
|
||||
|
||||
/* ==== Test adding and looking up metadata ==== */
|
||||
|
||||
int new_object_failed = 0;
|
||||
int new_object_succeeded = 0;
|
||||
object_id new_object_id;
|
||||
task *new_object_task;
|
||||
task_spec *new_object_task_spec;
|
||||
task_id new_object_task_id;
|
||||
|
||||
void new_object_fail_callback(unique_id id,
|
||||
void *user_context,
|
||||
void *user_data) {
|
||||
new_object_failed = 1;
|
||||
event_loop_stop(g_loop);
|
||||
}
|
||||
|
||||
/* === Test adding an object with an associated task === */
|
||||
|
||||
void new_object_done_callback(object_id object_id,
|
||||
task *task,
|
||||
void *user_context) {
|
||||
new_object_succeeded = 1;
|
||||
CHECK(object_ids_equal(object_id, new_object_id));
|
||||
CHECK(task);
|
||||
CHECK(memcmp(task, new_object_task, task_size(task)) == 0);
|
||||
event_loop_stop(g_loop);
|
||||
}
|
||||
|
||||
void new_object_lookup_callback(object_id object_id, void *user_context) {
|
||||
CHECK(object_ids_equal(object_id, new_object_id));
|
||||
retry_info retry = {
|
||||
.num_retries = 5,
|
||||
.timeout = 100,
|
||||
.fail_callback = new_object_fail_callback,
|
||||
};
|
||||
db_handle *db = user_context;
|
||||
result_table_lookup(db, new_object_id, &retry, new_object_done_callback,
|
||||
NULL);
|
||||
}
|
||||
|
||||
void new_object_task_callback(task_id task_id, void *user_context) {
|
||||
retry_info retry = {
|
||||
.num_retries = 5,
|
||||
.timeout = 100,
|
||||
.fail_callback = new_object_fail_callback,
|
||||
};
|
||||
db_handle *db = user_context;
|
||||
result_table_add(db, new_object_id, new_object_task_id, &retry,
|
||||
new_object_lookup_callback, (void *) db);
|
||||
}
|
||||
|
||||
TEST new_object_test(void) {
|
||||
new_object_failed = 0;
|
||||
new_object_succeeded = 0;
|
||||
new_object_id = globally_unique_id();
|
||||
new_object_task = example_task();
|
||||
new_object_task_spec = task_task_spec(new_object_task);
|
||||
new_object_task_id = task_spec_id(new_object_task_spec);
|
||||
g_loop = event_loop_create();
|
||||
db_handle *db =
|
||||
db_connect("127.0.0.1", 6379, "plasma_manager", "127.0.0.1", 1234);
|
||||
db_attach(db, g_loop);
|
||||
retry_info retry = {
|
||||
.num_retries = 5,
|
||||
.timeout = 100,
|
||||
.fail_callback = new_object_fail_callback,
|
||||
};
|
||||
task_table_add_task(db, new_object_task, &retry, new_object_task_callback,
|
||||
db);
|
||||
event_loop_run(g_loop);
|
||||
db_disconnect(db);
|
||||
destroy_outstanding_callbacks(g_loop);
|
||||
event_loop_destroy(g_loop);
|
||||
free_task(new_object_task);
|
||||
ASSERT(new_object_succeeded);
|
||||
ASSERT(!new_object_failed);
|
||||
PASS();
|
||||
}
|
||||
|
||||
/* === Test adding an object without an associated task === */
|
||||
|
||||
void new_object_no_task_lookup_callback(object_id object_id,
|
||||
task *task,
|
||||
void *user_context) {
|
||||
new_object_succeeded = 1;
|
||||
CHECK(task == NULL);
|
||||
event_loop_stop(g_loop);
|
||||
}
|
||||
|
||||
void new_object_no_task_callback(object_id object_id, void *user_context) {
|
||||
CHECK(node_ids_equal(object_id, new_object_id));
|
||||
retry_info retry = {
|
||||
.num_retries = 5,
|
||||
.timeout = 100,
|
||||
.fail_callback = new_object_fail_callback,
|
||||
};
|
||||
db_handle *db = user_context;
|
||||
result_table_lookup(db, object_id, &retry, new_object_no_task_lookup_callback,
|
||||
NULL);
|
||||
}
|
||||
|
||||
TEST new_object_no_task_test(void) {
|
||||
new_object_failed = 0;
|
||||
new_object_succeeded = 0;
|
||||
new_object_id = globally_unique_id();
|
||||
new_object_task_id = globally_unique_id();
|
||||
g_loop = event_loop_create();
|
||||
db_handle *db =
|
||||
db_connect("127.0.0.1", 6379, "plasma_manager", "127.0.0.1", 1234);
|
||||
db_attach(db, g_loop);
|
||||
retry_info retry = {
|
||||
.num_retries = 5,
|
||||
.timeout = 100,
|
||||
.fail_callback = new_object_fail_callback,
|
||||
};
|
||||
result_table_add(db, new_object_id, new_object_task_id, &retry,
|
||||
new_object_no_task_callback, db);
|
||||
event_loop_run(g_loop);
|
||||
db_disconnect(db);
|
||||
destroy_outstanding_callbacks(g_loop);
|
||||
event_loop_destroy(g_loop);
|
||||
ASSERT(new_object_succeeded);
|
||||
ASSERT(!new_object_failed);
|
||||
PASS();
|
||||
}
|
||||
|
||||
/* ==== Test if operations time out correctly ==== */
|
||||
|
||||
/* === Test lookup timeout === */
|
||||
@@ -27,9 +153,9 @@ void lookup_done_callback(object_id object_id,
|
||||
CHECK(0);
|
||||
}
|
||||
|
||||
void lookup_fail_callback(unique_id id, void *user_data) {
|
||||
void lookup_fail_callback(unique_id id, void *user_context, void *user_data) {
|
||||
lookup_failed = 1;
|
||||
CHECK(user_data == (void *) lookup_timeout_context);
|
||||
CHECK(user_context == (void *) lookup_timeout_context);
|
||||
event_loop_stop(g_loop);
|
||||
}
|
||||
|
||||
@@ -63,9 +189,9 @@ void add_done_callback(object_id object_id, void *user_context) {
|
||||
CHECK(0);
|
||||
}
|
||||
|
||||
void add_fail_callback(unique_id id, void *user_data) {
|
||||
void add_fail_callback(unique_id id, void *user_context, void *user_data) {
|
||||
add_failed = 1;
|
||||
CHECK(user_data == (void *) add_timeout_context);
|
||||
CHECK(user_context == (void *) add_timeout_context);
|
||||
event_loop_stop(g_loop);
|
||||
}
|
||||
|
||||
@@ -99,9 +225,11 @@ void subscribe_done_callback(object_id object_id, void *user_context) {
|
||||
CHECK(0);
|
||||
}
|
||||
|
||||
void subscribe_fail_callback(unique_id id, void *user_data) {
|
||||
void subscribe_fail_callback(unique_id id,
|
||||
void *user_context,
|
||||
void *user_data) {
|
||||
subscribe_failed = 1;
|
||||
CHECK(user_data == (void *) subscribe_timeout_context);
|
||||
CHECK(user_context == (void *) subscribe_timeout_context);
|
||||
event_loop_stop(g_loop);
|
||||
}
|
||||
|
||||
@@ -166,7 +294,9 @@ void lookup_retry_done_callback(object_id object_id,
|
||||
free(manager_vector);
|
||||
}
|
||||
|
||||
void lookup_retry_fail_callback(unique_id id, void *user_data) {
|
||||
void lookup_retry_fail_callback(unique_id id,
|
||||
void *user_context,
|
||||
void *user_data) {
|
||||
/* The fail callback should not be called. */
|
||||
CHECK(0);
|
||||
}
|
||||
@@ -210,7 +340,9 @@ void add_retry_done_callback(object_id object_id, void *user_context) {
|
||||
add_retry_succeeded = 1;
|
||||
}
|
||||
|
||||
void add_retry_fail_callback(unique_id id, void *user_data) {
|
||||
void add_retry_fail_callback(unique_id id,
|
||||
void *user_context,
|
||||
void *user_data) {
|
||||
/* The fail callback should not be called. */
|
||||
CHECK(0);
|
||||
}
|
||||
@@ -269,7 +401,9 @@ void subscribe_retry_done_callback(object_id object_id, void *user_context) {
|
||||
subscribe_retry_succeeded = 1;
|
||||
}
|
||||
|
||||
void subscribe_retry_fail_callback(unique_id id, void *user_data) {
|
||||
void subscribe_retry_fail_callback(unique_id id,
|
||||
void *user_context,
|
||||
void *user_data) {
|
||||
/* The fail callback should not be called. */
|
||||
CHECK(0);
|
||||
}
|
||||
@@ -312,7 +446,9 @@ TEST subscribe_retry_test(void) {
|
||||
const char *lookup_late_context = "lookup_late";
|
||||
int lookup_late_failed = 0;
|
||||
|
||||
void lookup_late_fail_callback(unique_id id, void *user_context) {
|
||||
void lookup_late_fail_callback(unique_id id,
|
||||
void *user_context,
|
||||
void *user_data) {
|
||||
CHECK(user_context == (void *) lookup_late_context);
|
||||
lookup_late_failed = 1;
|
||||
}
|
||||
@@ -357,7 +493,7 @@ TEST lookup_late_test(void) {
|
||||
const char *add_late_context = "add_late";
|
||||
int add_late_failed = 0;
|
||||
|
||||
void add_late_fail_callback(unique_id id, void *user_context) {
|
||||
void add_late_fail_callback(unique_id id, void *user_context, void *user_data) {
|
||||
CHECK(user_context == (void *) add_late_context);
|
||||
add_late_failed = 1;
|
||||
}
|
||||
@@ -397,7 +533,9 @@ TEST add_late_test(void) {
|
||||
const char *subscribe_late_context = "subscribe_late";
|
||||
int subscribe_late_failed = 0;
|
||||
|
||||
void subscribe_late_fail_callback(unique_id id, void *user_context) {
|
||||
void subscribe_late_fail_callback(unique_id id,
|
||||
void *user_context,
|
||||
void *user_data) {
|
||||
CHECK(user_context == (void *) subscribe_late_context);
|
||||
subscribe_late_failed = 1;
|
||||
}
|
||||
@@ -441,7 +579,9 @@ const char *subscribe_success_context = "subscribe_success";
|
||||
int subscribe_success_done = 0;
|
||||
int subscribe_success_succeeded = 0;
|
||||
|
||||
void subscribe_success_fail_callback(unique_id id, void *user_context) {
|
||||
void subscribe_success_fail_callback(unique_id id,
|
||||
void *user_context,
|
||||
void *user_data) {
|
||||
/* This function should never be called. */
|
||||
CHECK(0);
|
||||
}
|
||||
@@ -492,16 +632,18 @@ TEST subscribe_success_test(void) {
|
||||
}
|
||||
|
||||
SUITE(object_table_tests) {
|
||||
RUN_TEST(lookup_timeout_test);
|
||||
RUN_TEST(add_timeout_test);
|
||||
RUN_TEST(subscribe_timeout_test);
|
||||
RUN_TEST(lookup_retry_test);
|
||||
RUN_TEST(add_retry_test);
|
||||
RUN_TEST(subscribe_retry_test);
|
||||
RUN_TEST(lookup_late_test);
|
||||
RUN_TEST(add_late_test);
|
||||
RUN_TEST(subscribe_late_test);
|
||||
RUN_TEST(subscribe_success_test);
|
||||
RUN_REDIS_TEST(new_object_test);
|
||||
RUN_REDIS_TEST(new_object_no_task_test);
|
||||
RUN_REDIS_TEST(lookup_timeout_test);
|
||||
RUN_REDIS_TEST(add_timeout_test);
|
||||
RUN_REDIS_TEST(subscribe_timeout_test);
|
||||
RUN_REDIS_TEST(lookup_retry_test);
|
||||
RUN_REDIS_TEST(add_retry_test);
|
||||
RUN_REDIS_TEST(subscribe_retry_test);
|
||||
RUN_REDIS_TEST(lookup_late_test);
|
||||
RUN_REDIS_TEST(add_late_test);
|
||||
RUN_REDIS_TEST(subscribe_late_test);
|
||||
RUN_REDIS_TEST(subscribe_success_test);
|
||||
}
|
||||
|
||||
GREATEST_MAIN_DEFS();
|
||||
|
||||
@@ -1,316 +0,0 @@
|
||||
#include "greatest.h"
|
||||
|
||||
#include "event_loop.h"
|
||||
#include "test_common.h"
|
||||
#include "common.h"
|
||||
#include "state/object_table.h"
|
||||
#include "state/redis.h"
|
||||
|
||||
#include <unistd.h>
|
||||
#include <ae.h>
|
||||
|
||||
SUITE(task_log_tests);
|
||||
|
||||
event_loop *loop;
|
||||
|
||||
/* ==== Test if operations time out correctly ==== */
|
||||
|
||||
/* === Test subscribe timeout === */
|
||||
|
||||
const char *subscribe_timeout_context = "subscribe_timeout";
|
||||
int subscribe_failed = 0;
|
||||
|
||||
void subscribe_done_callback(task_iid task_iid, void *user_context) {
|
||||
/* The done callback should not be called. */
|
||||
CHECK(0);
|
||||
}
|
||||
|
||||
void subscribe_fail_callback(unique_id id, void *user_data) {
|
||||
subscribe_failed = 1;
|
||||
CHECK(user_data == (void *) subscribe_timeout_context);
|
||||
event_loop_stop(loop);
|
||||
}
|
||||
|
||||
TEST subscribe_timeout_test(void) {
|
||||
loop = event_loop_create();
|
||||
db_handle *db =
|
||||
db_connect("127.0.0.1", 6379, "plasma_manager", "127.0.0.1", 1234);
|
||||
db_attach(db, loop);
|
||||
retry_info retry = {
|
||||
.num_retries = 5,
|
||||
.timeout = 100,
|
||||
.fail_callback = subscribe_fail_callback,
|
||||
};
|
||||
task_log_subscribe(db, NIL_ID, TASK_STATUS_WAITING, NULL, NULL, &retry,
|
||||
subscribe_done_callback,
|
||||
(void *) subscribe_timeout_context);
|
||||
/* Disconnect the database to see if the subscribe times out. */
|
||||
close(db->sub_context->c.fd);
|
||||
aeProcessEvents(loop, AE_TIME_EVENTS);
|
||||
event_loop_run(loop);
|
||||
db_disconnect(db);
|
||||
destroy_outstanding_callbacks(loop);
|
||||
event_loop_destroy(loop);
|
||||
ASSERT(subscribe_failed);
|
||||
PASS();
|
||||
}
|
||||
|
||||
/* === Test publish timeout === */
|
||||
|
||||
const char *publish_timeout_context = "publish_timeout";
|
||||
const int publish_test_number = 272;
|
||||
int publish_failed = 0;
|
||||
|
||||
void publish_done_callback(task_iid task_iid, void *user_context) {
|
||||
/* The done callback should not be called. */
|
||||
CHECK(0);
|
||||
}
|
||||
|
||||
void publish_fail_callback(unique_id id, void *user_data) {
|
||||
publish_failed = 1;
|
||||
CHECK(user_data == (void *) publish_timeout_context);
|
||||
event_loop_stop(loop);
|
||||
}
|
||||
|
||||
TEST publish_timeout_test(void) {
|
||||
loop = event_loop_create();
|
||||
db_handle *db =
|
||||
db_connect("127.0.0.1", 6379, "plasma_manager", "127.0.0.1", 1234);
|
||||
db_attach(db, loop);
|
||||
task_instance *task = example_task_instance();
|
||||
retry_info retry = {
|
||||
.num_retries = 5, .timeout = 100, .fail_callback = publish_fail_callback,
|
||||
};
|
||||
task_log_publish(db, task, &retry, publish_done_callback,
|
||||
(void *) publish_timeout_context);
|
||||
/* Disconnect the database to see if the publish times out. */
|
||||
close(db->context->c.fd);
|
||||
aeProcessEvents(loop, AE_TIME_EVENTS);
|
||||
event_loop_run(loop);
|
||||
db_disconnect(db);
|
||||
destroy_outstanding_callbacks(loop);
|
||||
event_loop_destroy(loop);
|
||||
ASSERT(publish_failed);
|
||||
task_instance_free(task);
|
||||
PASS();
|
||||
}
|
||||
|
||||
/* ==== Test if the retry is working correctly ==== */
|
||||
|
||||
int64_t reconnect_db_callback(event_loop *loop,
|
||||
int64_t timer_id,
|
||||
void *context) {
|
||||
db_handle *db = context;
|
||||
/* Reconnect to redis. */
|
||||
redisAsyncFree(db->sub_context);
|
||||
db->sub_context = redisAsyncConnect("127.0.0.1", 6379);
|
||||
db->sub_context->data = (void *) db;
|
||||
/* Re-attach the database to the event loop (the file descriptor changed). */
|
||||
db_attach(db, loop);
|
||||
return EVENT_LOOP_TIMER_DONE;
|
||||
}
|
||||
|
||||
int64_t terminate_event_loop_callback(event_loop *loop,
|
||||
int64_t timer_id,
|
||||
void *context) {
|
||||
event_loop_stop(loop);
|
||||
return EVENT_LOOP_TIMER_DONE;
|
||||
}
|
||||
|
||||
/* === Test subscribe retry === */
|
||||
|
||||
const char *subscribe_retry_context = "subscribe_retry";
|
||||
const int subscribe_retry_test_number = 273;
|
||||
int subscribe_retry_succeeded = 0;
|
||||
|
||||
void subscribe_retry_done_callback(object_id object_id, void *user_context) {
|
||||
CHECK(user_context == (void *) subscribe_retry_context);
|
||||
subscribe_retry_succeeded = 1;
|
||||
}
|
||||
|
||||
void subscribe_retry_fail_callback(unique_id id, void *user_data) {
|
||||
/* The fail callback should not be called. */
|
||||
CHECK(0);
|
||||
}
|
||||
|
||||
TEST subscribe_retry_test(void) {
|
||||
loop = event_loop_create();
|
||||
db_handle *db =
|
||||
db_connect("127.0.0.1", 6379, "plasma_manager", "127.0.0.1", 11235);
|
||||
db_attach(db, loop);
|
||||
retry_info retry = {
|
||||
.num_retries = 5,
|
||||
.timeout = 100,
|
||||
.fail_callback = subscribe_retry_fail_callback,
|
||||
};
|
||||
task_log_subscribe(db, NIL_ID, TASK_STATUS_WAITING, NULL, NULL, &retry,
|
||||
subscribe_retry_done_callback,
|
||||
(void *) subscribe_retry_context);
|
||||
/* Disconnect the database to see if the subscribe times out. */
|
||||
close(db->sub_context->c.fd);
|
||||
/* Install handler for reconnecting the database. */
|
||||
event_loop_add_timer(loop, 150,
|
||||
(event_loop_timer_handler) reconnect_db_callback, db);
|
||||
/* Install handler for terminating the event loop. */
|
||||
event_loop_add_timer(loop, 750,
|
||||
(event_loop_timer_handler) terminate_event_loop_callback,
|
||||
NULL);
|
||||
event_loop_run(loop);
|
||||
db_disconnect(db);
|
||||
destroy_outstanding_callbacks(loop);
|
||||
event_loop_destroy(loop);
|
||||
ASSERT(subscribe_retry_succeeded);
|
||||
PASS();
|
||||
}
|
||||
|
||||
/* === Test publish retry === */
|
||||
|
||||
const char *publish_retry_context = "publish_retry";
|
||||
int publish_retry_succeeded = 0;
|
||||
|
||||
void publish_retry_done_callback(object_id object_id, void *user_context) {
|
||||
CHECK(user_context == (void *) publish_retry_context);
|
||||
publish_retry_succeeded = 1;
|
||||
}
|
||||
|
||||
void publish_retry_fail_callback(unique_id id, void *user_data) {
|
||||
/* The fail callback should not be called. */
|
||||
CHECK(0);
|
||||
}
|
||||
|
||||
TEST publish_retry_test(void) {
|
||||
loop = event_loop_create();
|
||||
db_handle *db =
|
||||
db_connect("127.0.0.1", 6379, "plasma_manager", "127.0.0.1", 11235);
|
||||
db_attach(db, loop);
|
||||
task_instance *task = example_task_instance();
|
||||
retry_info retry = {
|
||||
.num_retries = 5,
|
||||
.timeout = 100,
|
||||
.fail_callback = publish_retry_fail_callback,
|
||||
};
|
||||
task_log_publish(db, task, &retry, publish_retry_done_callback,
|
||||
(void *) publish_retry_context);
|
||||
/* Disconnect the database to see if the publish times out. */
|
||||
close(db->sub_context->c.fd);
|
||||
/* Install handler for reconnecting the database. */
|
||||
event_loop_add_timer(loop, 150,
|
||||
(event_loop_timer_handler) reconnect_db_callback, db);
|
||||
/* Install handler for terminating the event loop. */
|
||||
event_loop_add_timer(loop, 750,
|
||||
(event_loop_timer_handler) terminate_event_loop_callback,
|
||||
NULL);
|
||||
event_loop_run(loop);
|
||||
db_disconnect(db);
|
||||
destroy_outstanding_callbacks(loop);
|
||||
event_loop_destroy(loop);
|
||||
ASSERT(publish_retry_succeeded);
|
||||
task_instance_free(task);
|
||||
PASS();
|
||||
}
|
||||
|
||||
/* ==== Test if late succeed is working correctly ==== */
|
||||
|
||||
/* === Test subscribe late succeed === */
|
||||
|
||||
const char *subscribe_late_context = "subscribe_late";
|
||||
int subscribe_late_failed = 0;
|
||||
|
||||
void subscribe_late_fail_callback(unique_id id, void *user_context) {
|
||||
CHECK(user_context == (void *) subscribe_late_context);
|
||||
subscribe_late_failed = 1;
|
||||
}
|
||||
|
||||
void subscribe_late_done_callback(task_iid task_iid, void *user_context) {
|
||||
/* This function should never be called. */
|
||||
CHECK(0);
|
||||
}
|
||||
|
||||
TEST subscribe_late_test(void) {
|
||||
loop = event_loop_create();
|
||||
db_handle *db =
|
||||
db_connect("127.0.0.1", 6379, "plasma_manager", "127.0.0.1", 11236);
|
||||
db_attach(db, loop);
|
||||
retry_info retry = {
|
||||
.num_retries = 0,
|
||||
.timeout = 0,
|
||||
.fail_callback = subscribe_late_fail_callback,
|
||||
};
|
||||
task_log_subscribe(db, NIL_ID, TASK_STATUS_WAITING, NULL, NULL, &retry,
|
||||
subscribe_late_done_callback,
|
||||
(void *) subscribe_late_context);
|
||||
/* Install handler for terminating the event loop. */
|
||||
event_loop_add_timer(loop, 750,
|
||||
(event_loop_timer_handler) terminate_event_loop_callback,
|
||||
NULL);
|
||||
/* First process timer events to make sure the timeout is processed before
|
||||
* anything else. */
|
||||
aeProcessEvents(loop, AE_TIME_EVENTS);
|
||||
event_loop_run(loop);
|
||||
db_disconnect(db);
|
||||
destroy_outstanding_callbacks(loop);
|
||||
event_loop_destroy(loop);
|
||||
ASSERT(subscribe_late_failed);
|
||||
PASS();
|
||||
}
|
||||
|
||||
/* === Test publish late succeed === */
|
||||
|
||||
const char *publish_late_context = "publish_late";
|
||||
int publish_late_failed = 0;
|
||||
|
||||
void publish_late_fail_callback(unique_id id, void *user_context) {
|
||||
CHECK(user_context == (void *) publish_late_context);
|
||||
publish_late_failed = 1;
|
||||
}
|
||||
|
||||
void publish_late_done_callback(task_iid task_iik, void *user_context) {
|
||||
/* This function should never be called. */
|
||||
CHECK(0);
|
||||
}
|
||||
|
||||
TEST publish_late_test(void) {
|
||||
loop = event_loop_create();
|
||||
db_handle *db =
|
||||
db_connect("127.0.0.1", 6379, "plasma_manager", "127.0.0.1", 11236);
|
||||
db_attach(db, loop);
|
||||
task_instance *task = example_task_instance();
|
||||
retry_info retry = {
|
||||
.num_retries = 0,
|
||||
.timeout = 0,
|
||||
.fail_callback = publish_late_fail_callback,
|
||||
};
|
||||
task_log_publish(db, task, &retry, publish_late_done_callback,
|
||||
(void *) publish_late_context);
|
||||
/* Install handler for terminating the event loop. */
|
||||
event_loop_add_timer(loop, 750,
|
||||
(event_loop_timer_handler) terminate_event_loop_callback,
|
||||
NULL);
|
||||
/* First process timer events to make sure the timeout is processed before
|
||||
* anything else. */
|
||||
aeProcessEvents(loop, AE_TIME_EVENTS);
|
||||
event_loop_run(loop);
|
||||
db_disconnect(db);
|
||||
destroy_outstanding_callbacks(loop);
|
||||
event_loop_destroy(loop);
|
||||
ASSERT(publish_late_failed);
|
||||
task_instance_free(task);
|
||||
PASS();
|
||||
}
|
||||
|
||||
SUITE(task_log_tests) {
|
||||
RUN_TEST(subscribe_timeout_test);
|
||||
RUN_TEST(publish_timeout_test);
|
||||
RUN_TEST(subscribe_retry_test);
|
||||
RUN_TEST(publish_retry_test);
|
||||
RUN_TEST(subscribe_late_test);
|
||||
RUN_TEST(publish_late_test);
|
||||
}
|
||||
|
||||
GREATEST_MAIN_DEFS();
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
GREATEST_MAIN_BEGIN();
|
||||
RUN_SUITE(task_log_tests);
|
||||
GREATEST_MAIN_END();
|
||||
}
|
||||
@@ -0,0 +1,430 @@
|
||||
#include "greatest.h"
|
||||
|
||||
#include "event_loop.h"
|
||||
#include "test_common.h"
|
||||
#include "common.h"
|
||||
#include "state/object_table.h"
|
||||
#include "state/redis.h"
|
||||
|
||||
#include <unistd.h>
|
||||
#include <ae.h>
|
||||
|
||||
SUITE(task_table_tests);
|
||||
|
||||
event_loop *g_loop;
|
||||
|
||||
/* ==== Test operations in non-failure scenario ==== */
|
||||
|
||||
/* === A lookup of a task not in the table === */
|
||||
|
||||
task_id lookup_nil_id;
|
||||
int lookup_nil_success = 0;
|
||||
const char *lookup_nil_context = "lookup_nil";
|
||||
|
||||
void lookup_nil_fail_callback(unique_id id,
|
||||
void *user_context,
|
||||
void *user_data) {
|
||||
/* The fail callback should not be called. */
|
||||
CHECK(0);
|
||||
}
|
||||
|
||||
void lookup_nil_success_callback(task *task, void *context) {
|
||||
lookup_nil_success = 1;
|
||||
CHECK(task == NULL);
|
||||
CHECK(context == (void *) lookup_nil_context);
|
||||
event_loop_stop(g_loop);
|
||||
}
|
||||
|
||||
TEST lookup_nil_test(void) {
|
||||
lookup_nil_id = globally_unique_id();
|
||||
g_loop = event_loop_create();
|
||||
db_handle *db =
|
||||
db_connect("127.0.0.1", 6379, "plasma_manager", "127.0.0.1", 1234);
|
||||
db_attach(db, g_loop);
|
||||
retry_info retry = {
|
||||
.num_retries = 5,
|
||||
.timeout = 1000,
|
||||
.fail_callback = lookup_nil_fail_callback,
|
||||
};
|
||||
task_table_get_task(db, lookup_nil_id, &retry, lookup_nil_success_callback,
|
||||
(void *) lookup_nil_context);
|
||||
/* Disconnect the database to see if the lookup times out. */
|
||||
event_loop_run(g_loop);
|
||||
db_disconnect(db);
|
||||
destroy_outstanding_callbacks(g_loop);
|
||||
event_loop_destroy(g_loop);
|
||||
ASSERT(lookup_nil_success);
|
||||
PASS();
|
||||
}
|
||||
|
||||
/* === A lookup of a task after it's added returns the same spec === */
|
||||
|
||||
int add_success = 0;
|
||||
int lookup_success = 0;
|
||||
task *add_lookup_task;
|
||||
const char *add_lookup_context = "add_lookup";
|
||||
|
||||
void add_lookup_fail_callback(unique_id id,
|
||||
void *user_context,
|
||||
void *user_data) {
|
||||
/* The fail callback should not be called. */
|
||||
CHECK(0);
|
||||
}
|
||||
|
||||
void lookup_success_callback(task *task, void *context) {
|
||||
lookup_success = 1;
|
||||
CHECK(memcmp(task, add_lookup_task, task_size(task)) == 0);
|
||||
event_loop_stop(g_loop);
|
||||
}
|
||||
|
||||
void add_success_callback(task_id task_id, void *context) {
|
||||
add_success = 1;
|
||||
CHECK(task_ids_equal(task_id, task_task_id(add_lookup_task)));
|
||||
|
||||
db_handle *db = context;
|
||||
retry_info retry = {
|
||||
.num_retries = 5,
|
||||
.timeout = 1000,
|
||||
.fail_callback = add_lookup_fail_callback,
|
||||
};
|
||||
task_table_get_task(db, task_id, &retry, lookup_success_callback,
|
||||
(void *) add_lookup_context);
|
||||
}
|
||||
|
||||
TEST add_lookup_test(void) {
|
||||
add_lookup_task = example_task();
|
||||
g_loop = event_loop_create();
|
||||
db_handle *db =
|
||||
db_connect("127.0.0.1", 6379, "plasma_manager", "127.0.0.1", 1234);
|
||||
db_attach(db, g_loop);
|
||||
retry_info retry = {
|
||||
.num_retries = 5,
|
||||
.timeout = 1000,
|
||||
.fail_callback = add_lookup_fail_callback,
|
||||
};
|
||||
task_table_add_task(db, add_lookup_task, &retry, add_success_callback,
|
||||
(void *) db);
|
||||
/* Disconnect the database to see if the lookup times out. */
|
||||
event_loop_run(g_loop);
|
||||
db_disconnect(db);
|
||||
destroy_outstanding_callbacks(g_loop);
|
||||
event_loop_destroy(g_loop);
|
||||
free(add_lookup_task);
|
||||
ASSERT(add_success);
|
||||
ASSERT(lookup_success);
|
||||
PASS();
|
||||
}
|
||||
|
||||
/* ==== Test if operations time out correctly ==== */
|
||||
|
||||
/* === Test subscribe timeout === */
|
||||
|
||||
const char *subscribe_timeout_context = "subscribe_timeout";
|
||||
int subscribe_failed = 0;
|
||||
|
||||
void subscribe_done_callback(task_id task_id, void *user_context) {
|
||||
/* The done callback should not be called. */
|
||||
CHECK(0);
|
||||
}
|
||||
|
||||
void subscribe_fail_callback(unique_id id,
|
||||
void *user_context,
|
||||
void *user_data) {
|
||||
subscribe_failed = 1;
|
||||
CHECK(user_context == (void *) subscribe_timeout_context);
|
||||
event_loop_stop(g_loop);
|
||||
}
|
||||
|
||||
TEST subscribe_timeout_test(void) {
|
||||
g_loop = event_loop_create();
|
||||
db_handle *db =
|
||||
db_connect("127.0.0.1", 6379, "plasma_manager", "127.0.0.1", 1234);
|
||||
db_attach(db, g_loop);
|
||||
retry_info retry = {
|
||||
.num_retries = 5,
|
||||
.timeout = 100,
|
||||
.fail_callback = subscribe_fail_callback,
|
||||
};
|
||||
task_table_subscribe(db, NIL_ID, TASK_STATUS_WAITING, NULL, NULL, &retry,
|
||||
subscribe_done_callback,
|
||||
(void *) subscribe_timeout_context);
|
||||
/* Disconnect the database to see if the subscribe times out. */
|
||||
close(db->sub_context->c.fd);
|
||||
aeProcessEvents(g_loop, AE_TIME_EVENTS);
|
||||
event_loop_run(g_loop);
|
||||
db_disconnect(db);
|
||||
destroy_outstanding_callbacks(g_loop);
|
||||
event_loop_destroy(g_loop);
|
||||
ASSERT(subscribe_failed);
|
||||
PASS();
|
||||
}
|
||||
|
||||
/* === Test publish timeout === */
|
||||
|
||||
const char *publish_timeout_context = "publish_timeout";
|
||||
const int publish_test_number = 272;
|
||||
int publish_failed = 0;
|
||||
|
||||
void publish_done_callback(task_id task_id, void *user_context) {
|
||||
/* The done callback should not be called. */
|
||||
CHECK(0);
|
||||
}
|
||||
|
||||
void publish_fail_callback(unique_id id, void *user_context, void *user_data) {
|
||||
publish_failed = 1;
|
||||
CHECK(user_context == (void *) publish_timeout_context);
|
||||
event_loop_stop(g_loop);
|
||||
}
|
||||
|
||||
TEST publish_timeout_test(void) {
|
||||
g_loop = event_loop_create();
|
||||
db_handle *db =
|
||||
db_connect("127.0.0.1", 6379, "plasma_manager", "127.0.0.1", 1234);
|
||||
db_attach(db, g_loop);
|
||||
task *task = example_task();
|
||||
retry_info retry = {
|
||||
.num_retries = 5, .timeout = 100, .fail_callback = publish_fail_callback,
|
||||
};
|
||||
task_table_update(db, task, &retry, publish_done_callback,
|
||||
(void *) publish_timeout_context);
|
||||
/* Disconnect the database to see if the publish times out. */
|
||||
close(db->context->c.fd);
|
||||
aeProcessEvents(g_loop, AE_TIME_EVENTS);
|
||||
event_loop_run(g_loop);
|
||||
db_disconnect(db);
|
||||
destroy_outstanding_callbacks(g_loop);
|
||||
event_loop_destroy(g_loop);
|
||||
ASSERT(publish_failed);
|
||||
free_task(task);
|
||||
PASS();
|
||||
}
|
||||
|
||||
/* ==== Test if the retry is working correctly ==== */
|
||||
|
||||
int64_t reconnect_db_callback(event_loop *loop,
|
||||
int64_t timer_id,
|
||||
void *context) {
|
||||
db_handle *db = context;
|
||||
/* Reconnect to redis. */
|
||||
redisAsyncFree(db->sub_context);
|
||||
db->sub_context = redisAsyncConnect("127.0.0.1", 6379);
|
||||
db->sub_context->data = (void *) db;
|
||||
/* Re-attach the database to the event loop (the file descriptor changed). */
|
||||
db_attach(db, loop);
|
||||
return EVENT_LOOP_TIMER_DONE;
|
||||
}
|
||||
|
||||
int64_t terminate_event_loop_callback(event_loop *loop,
|
||||
int64_t timer_id,
|
||||
void *context) {
|
||||
event_loop_stop(loop);
|
||||
return EVENT_LOOP_TIMER_DONE;
|
||||
}
|
||||
|
||||
/* === Test subscribe retry === */
|
||||
|
||||
const char *subscribe_retry_context = "subscribe_retry";
|
||||
const int subscribe_retry_test_number = 273;
|
||||
int subscribe_retry_succeeded = 0;
|
||||
|
||||
void subscribe_retry_done_callback(object_id object_id, void *user_context) {
|
||||
CHECK(user_context == (void *) subscribe_retry_context);
|
||||
subscribe_retry_succeeded = 1;
|
||||
}
|
||||
|
||||
void subscribe_retry_fail_callback(unique_id id,
|
||||
void *user_context,
|
||||
void *user_data) {
|
||||
/* The fail callback should not be called. */
|
||||
CHECK(0);
|
||||
}
|
||||
|
||||
TEST subscribe_retry_test(void) {
|
||||
g_loop = event_loop_create();
|
||||
db_handle *db =
|
||||
db_connect("127.0.0.1", 6379, "plasma_manager", "127.0.0.1", 11235);
|
||||
db_attach(db, g_loop);
|
||||
retry_info retry = {
|
||||
.num_retries = 5,
|
||||
.timeout = 100,
|
||||
.fail_callback = subscribe_retry_fail_callback,
|
||||
};
|
||||
task_table_subscribe(db, NIL_ID, TASK_STATUS_WAITING, NULL, NULL, &retry,
|
||||
subscribe_retry_done_callback,
|
||||
(void *) subscribe_retry_context);
|
||||
/* Disconnect the database to see if the subscribe times out. */
|
||||
close(db->sub_context->c.fd);
|
||||
/* Install handler for reconnecting the database. */
|
||||
event_loop_add_timer(g_loop, 150,
|
||||
(event_loop_timer_handler) reconnect_db_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_retry_succeeded);
|
||||
PASS();
|
||||
}
|
||||
|
||||
/* === Test publish retry === */
|
||||
|
||||
const char *publish_retry_context = "publish_retry";
|
||||
int publish_retry_succeeded = 0;
|
||||
|
||||
void publish_retry_done_callback(object_id object_id, void *user_context) {
|
||||
CHECK(user_context == (void *) publish_retry_context);
|
||||
publish_retry_succeeded = 1;
|
||||
}
|
||||
|
||||
void publish_retry_fail_callback(unique_id id,
|
||||
void *user_context,
|
||||
void *user_data) {
|
||||
/* The fail callback should not be called. */
|
||||
CHECK(0);
|
||||
}
|
||||
|
||||
TEST publish_retry_test(void) {
|
||||
g_loop = event_loop_create();
|
||||
db_handle *db =
|
||||
db_connect("127.0.0.1", 6379, "plasma_manager", "127.0.0.1", 11235);
|
||||
db_attach(db, g_loop);
|
||||
task *task = example_task();
|
||||
retry_info retry = {
|
||||
.num_retries = 5,
|
||||
.timeout = 100,
|
||||
.fail_callback = publish_retry_fail_callback,
|
||||
};
|
||||
task_table_update(db, task, &retry, publish_retry_done_callback,
|
||||
(void *) publish_retry_context);
|
||||
/* Disconnect the database to see if the publish times out. */
|
||||
close(db->sub_context->c.fd);
|
||||
/* Install handler for reconnecting the database. */
|
||||
event_loop_add_timer(g_loop, 150,
|
||||
(event_loop_timer_handler) reconnect_db_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(publish_retry_succeeded);
|
||||
free_task(task);
|
||||
PASS();
|
||||
}
|
||||
|
||||
/* ==== Test if late succeed is working correctly ==== */
|
||||
|
||||
/* === Test subscribe late succeed === */
|
||||
|
||||
const char *subscribe_late_context = "subscribe_late";
|
||||
int subscribe_late_failed = 0;
|
||||
|
||||
void subscribe_late_fail_callback(unique_id id,
|
||||
void *user_context,
|
||||
void *user_data) {
|
||||
CHECK(user_context == (void *) subscribe_late_context);
|
||||
subscribe_late_failed = 1;
|
||||
}
|
||||
|
||||
void subscribe_late_done_callback(task_id task_id, void *user_context) {
|
||||
/* This function should never be called. */
|
||||
CHECK(0);
|
||||
}
|
||||
|
||||
TEST subscribe_late_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);
|
||||
retry_info retry = {
|
||||
.num_retries = 0,
|
||||
.timeout = 0,
|
||||
.fail_callback = subscribe_late_fail_callback,
|
||||
};
|
||||
task_table_subscribe(db, NIL_ID, TASK_STATUS_WAITING, NULL, NULL, &retry,
|
||||
subscribe_late_done_callback,
|
||||
(void *) subscribe_late_context);
|
||||
/* Install handler for terminating the event loop. */
|
||||
event_loop_add_timer(g_loop, 750,
|
||||
(event_loop_timer_handler) terminate_event_loop_callback,
|
||||
NULL);
|
||||
/* First process timer events to make sure the timeout is processed before
|
||||
* anything else. */
|
||||
aeProcessEvents(g_loop, AE_TIME_EVENTS);
|
||||
event_loop_run(g_loop);
|
||||
db_disconnect(db);
|
||||
destroy_outstanding_callbacks(g_loop);
|
||||
event_loop_destroy(g_loop);
|
||||
ASSERT(subscribe_late_failed);
|
||||
PASS();
|
||||
}
|
||||
|
||||
/* === Test publish late succeed === */
|
||||
|
||||
const char *publish_late_context = "publish_late";
|
||||
int publish_late_failed = 0;
|
||||
|
||||
void publish_late_fail_callback(unique_id id,
|
||||
void *user_context,
|
||||
void *user_data) {
|
||||
CHECK(user_context == (void *) publish_late_context);
|
||||
publish_late_failed = 1;
|
||||
}
|
||||
|
||||
void publish_late_done_callback(task_id task_id, void *user_context) {
|
||||
/* This function should never be called. */
|
||||
CHECK(0);
|
||||
}
|
||||
|
||||
TEST publish_late_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);
|
||||
task *task = example_task();
|
||||
retry_info retry = {
|
||||
.num_retries = 0,
|
||||
.timeout = 0,
|
||||
.fail_callback = publish_late_fail_callback,
|
||||
};
|
||||
task_table_update(db, task, &retry, publish_late_done_callback,
|
||||
(void *) publish_late_context);
|
||||
/* Install handler for terminating the event loop. */
|
||||
event_loop_add_timer(g_loop, 750,
|
||||
(event_loop_timer_handler) terminate_event_loop_callback,
|
||||
NULL);
|
||||
/* First process timer events to make sure the timeout is processed before
|
||||
* anything else. */
|
||||
aeProcessEvents(g_loop, AE_TIME_EVENTS);
|
||||
event_loop_run(g_loop);
|
||||
db_disconnect(db);
|
||||
destroy_outstanding_callbacks(g_loop);
|
||||
event_loop_destroy(g_loop);
|
||||
ASSERT(publish_late_failed);
|
||||
free_task(task);
|
||||
PASS();
|
||||
}
|
||||
|
||||
SUITE(task_table_tests) {
|
||||
RUN_REDIS_TEST(lookup_nil_test);
|
||||
RUN_REDIS_TEST(add_lookup_test);
|
||||
RUN_TEST(subscribe_timeout_test);
|
||||
RUN_TEST(publish_timeout_test);
|
||||
RUN_TEST(subscribe_retry_test);
|
||||
RUN_TEST(publish_retry_test);
|
||||
RUN_TEST(subscribe_late_test);
|
||||
RUN_TEST(publish_late_test);
|
||||
}
|
||||
|
||||
GREATEST_MAIN_DEFS();
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
GREATEST_MAIN_BEGIN();
|
||||
RUN_SUITE(task_table_tests);
|
||||
GREATEST_MAIN_END();
|
||||
}
|
||||
@@ -14,33 +14,33 @@ SUITE(task_tests);
|
||||
TEST task_test(void) {
|
||||
task_id parent_task_id = globally_unique_id();
|
||||
function_id func_id = globally_unique_id();
|
||||
task_spec *task =
|
||||
task_spec *spec =
|
||||
start_construct_task_spec(parent_task_id, 0, func_id, 4, 2, 10);
|
||||
ASSERT(task_num_args(task) == 4);
|
||||
ASSERT(task_num_returns(task) == 2);
|
||||
ASSERT(task_num_args(spec) == 4);
|
||||
ASSERT(task_num_returns(spec) == 2);
|
||||
|
||||
unique_id arg1 = globally_unique_id();
|
||||
ASSERT(task_args_add_ref(task, arg1) == 0);
|
||||
ASSERT(task_args_add_val(task, (uint8_t *) "hello", 5) == 1);
|
||||
ASSERT(task_args_add_ref(spec, arg1) == 0);
|
||||
ASSERT(task_args_add_val(spec, (uint8_t *) "hello", 5) == 1);
|
||||
unique_id arg2 = globally_unique_id();
|
||||
ASSERT(task_args_add_ref(task, arg2) == 2);
|
||||
ASSERT(task_args_add_val(task, (uint8_t *) "world", 5) == 3);
|
||||
/* Finish constructing the task. This constructs the task ID and the task
|
||||
ASSERT(task_args_add_ref(spec, arg2) == 2);
|
||||
ASSERT(task_args_add_val(spec, (uint8_t *) "world", 5) == 3);
|
||||
/* Finish constructing the spec. This constructs the task ID and the
|
||||
* return IDs. */
|
||||
finish_construct_task_spec(task);
|
||||
finish_construct_task_spec(spec);
|
||||
|
||||
/* Check that the task was constructed as expected. */
|
||||
ASSERT(task_num_args(task) == 4);
|
||||
ASSERT(task_num_returns(task) == 2);
|
||||
ASSERT(function_ids_equal(task_function(task), func_id));
|
||||
ASSERT(object_ids_equal(task_arg_id(task, 0), arg1));
|
||||
ASSERT(memcmp(task_arg_val(task, 1), (uint8_t *) "hello",
|
||||
task_arg_length(task, 1)) == 0);
|
||||
ASSERT(object_ids_equal(task_arg_id(task, 2), arg2));
|
||||
ASSERT(memcmp(task_arg_val(task, 3), (uint8_t *) "world",
|
||||
task_arg_length(task, 3)) == 0);
|
||||
/* Check that the spec was constructed as expected. */
|
||||
ASSERT(task_num_args(spec) == 4);
|
||||
ASSERT(task_num_returns(spec) == 2);
|
||||
ASSERT(function_ids_equal(task_function(spec), func_id));
|
||||
ASSERT(object_ids_equal(task_arg_id(spec, 0), arg1));
|
||||
ASSERT(memcmp(task_arg_val(spec, 1), (uint8_t *) "hello",
|
||||
task_arg_length(spec, 1)) == 0);
|
||||
ASSERT(object_ids_equal(task_arg_id(spec, 2), arg2));
|
||||
ASSERT(memcmp(task_arg_val(spec, 3), (uint8_t *) "world",
|
||||
task_arg_length(spec, 3)) == 0);
|
||||
|
||||
free_task_spec(task);
|
||||
free_task_spec(spec);
|
||||
PASS();
|
||||
}
|
||||
|
||||
@@ -52,121 +52,121 @@ TEST deterministic_ids_test(void) {
|
||||
uint8_t *arg2 = (uint8_t *) "hello world";
|
||||
|
||||
/* Construct a first task. */
|
||||
task_spec *task1 =
|
||||
task_spec *spec1 =
|
||||
start_construct_task_spec(parent_task_id, 0, func_id, 2, 3, 11);
|
||||
task_args_add_ref(task1, arg1);
|
||||
task_args_add_val(task1, arg2, 11);
|
||||
finish_construct_task_spec(task1);
|
||||
task_args_add_ref(spec1, arg1);
|
||||
task_args_add_val(spec1, arg2, 11);
|
||||
finish_construct_task_spec(spec1);
|
||||
|
||||
/* Construct a second identical task. */
|
||||
task_spec *task2 =
|
||||
task_spec *spec2 =
|
||||
start_construct_task_spec(parent_task_id, 0, func_id, 2, 3, 11);
|
||||
task_args_add_ref(task2, arg1);
|
||||
task_args_add_val(task2, arg2, 11);
|
||||
finish_construct_task_spec(task2);
|
||||
task_args_add_ref(spec2, arg1);
|
||||
task_args_add_val(spec2, arg2, 11);
|
||||
finish_construct_task_spec(spec2);
|
||||
|
||||
/* Check that these tasks have the same task IDs and the same return IDs.*/
|
||||
ASSERT(task_ids_equal(task_task_id(task1), task_task_id(task2)));
|
||||
ASSERT(object_ids_equal(task_return(task1, 0), task_return(task2, 0)));
|
||||
ASSERT(object_ids_equal(task_return(task1, 1), task_return(task2, 1)));
|
||||
ASSERT(object_ids_equal(task_return(task1, 2), task_return(task2, 2)));
|
||||
ASSERT(task_ids_equal(task_spec_id(spec1), task_spec_id(spec2)));
|
||||
ASSERT(object_ids_equal(task_return(spec1, 0), task_return(spec2, 0)));
|
||||
ASSERT(object_ids_equal(task_return(spec1, 1), task_return(spec2, 1)));
|
||||
ASSERT(object_ids_equal(task_return(spec1, 2), task_return(spec2, 2)));
|
||||
/* Check that the return IDs are all distinct. */
|
||||
ASSERT(!object_ids_equal(task_return(task1, 0), task_return(task2, 1)));
|
||||
ASSERT(!object_ids_equal(task_return(task1, 0), task_return(task2, 2)));
|
||||
ASSERT(!object_ids_equal(task_return(task1, 1), task_return(task2, 2)));
|
||||
ASSERT(!object_ids_equal(task_return(spec1, 0), task_return(spec2, 1)));
|
||||
ASSERT(!object_ids_equal(task_return(spec1, 0), task_return(spec2, 2)));
|
||||
ASSERT(!object_ids_equal(task_return(spec1, 1), task_return(spec2, 2)));
|
||||
|
||||
/* Create more tasks that are only mildly different. */
|
||||
|
||||
/* Construct a task with a different parent task ID. */
|
||||
task_spec *task3 =
|
||||
task_spec *spec3 =
|
||||
start_construct_task_spec(globally_unique_id(), 0, func_id, 2, 3, 11);
|
||||
task_args_add_ref(task3, arg1);
|
||||
task_args_add_val(task3, arg2, 11);
|
||||
finish_construct_task_spec(task3);
|
||||
task_args_add_ref(spec3, arg1);
|
||||
task_args_add_val(spec3, arg2, 11);
|
||||
finish_construct_task_spec(spec3);
|
||||
|
||||
/* Construct a task with a different parent counter. */
|
||||
task_spec *task4 =
|
||||
task_spec *spec4 =
|
||||
start_construct_task_spec(parent_task_id, 1, func_id, 2, 3, 11);
|
||||
task_args_add_ref(task4, arg1);
|
||||
task_args_add_val(task4, arg2, 11);
|
||||
finish_construct_task_spec(task4);
|
||||
task_args_add_ref(spec4, arg1);
|
||||
task_args_add_val(spec4, arg2, 11);
|
||||
finish_construct_task_spec(spec4);
|
||||
|
||||
/* Construct a task with a different function ID. */
|
||||
task_spec *task5 = start_construct_task_spec(parent_task_id, 0,
|
||||
task_spec *spec5 = start_construct_task_spec(parent_task_id, 0,
|
||||
globally_unique_id(), 2, 3, 11);
|
||||
task_args_add_ref(task5, arg1);
|
||||
task_args_add_val(task5, arg2, 11);
|
||||
finish_construct_task_spec(task5);
|
||||
task_args_add_ref(spec5, arg1);
|
||||
task_args_add_val(spec5, arg2, 11);
|
||||
finish_construct_task_spec(spec5);
|
||||
|
||||
/* Construct a task with a different object ID argument. */
|
||||
task_spec *task6 =
|
||||
task_spec *spec6 =
|
||||
start_construct_task_spec(parent_task_id, 0, func_id, 2, 3, 11);
|
||||
task_args_add_ref(task6, globally_unique_id());
|
||||
task_args_add_val(task6, arg2, 11);
|
||||
finish_construct_task_spec(task6);
|
||||
task_args_add_ref(spec6, globally_unique_id());
|
||||
task_args_add_val(spec6, arg2, 11);
|
||||
finish_construct_task_spec(spec6);
|
||||
|
||||
/* Construct a task with a different value argument. */
|
||||
task_spec *task7 =
|
||||
task_spec *spec7 =
|
||||
start_construct_task_spec(parent_task_id, 0, func_id, 2, 3, 11);
|
||||
task_args_add_ref(task7, arg1);
|
||||
task_args_add_val(task7, (uint8_t *) "hello_world", 11);
|
||||
finish_construct_task_spec(task7);
|
||||
task_args_add_ref(spec7, arg1);
|
||||
task_args_add_val(spec7, (uint8_t *) "hello_world", 11);
|
||||
finish_construct_task_spec(spec7);
|
||||
|
||||
/* Check that the task IDs are all distinct from the original. */
|
||||
ASSERT(!task_ids_equal(task_task_id(task1), task_task_id(task3)));
|
||||
ASSERT(!task_ids_equal(task_task_id(task1), task_task_id(task4)));
|
||||
ASSERT(!task_ids_equal(task_task_id(task1), task_task_id(task5)));
|
||||
ASSERT(!task_ids_equal(task_task_id(task1), task_task_id(task6)));
|
||||
ASSERT(!task_ids_equal(task_task_id(task1), task_task_id(task7)));
|
||||
ASSERT(!task_ids_equal(task_spec_id(spec1), task_spec_id(spec3)));
|
||||
ASSERT(!task_ids_equal(task_spec_id(spec1), task_spec_id(spec4)));
|
||||
ASSERT(!task_ids_equal(task_spec_id(spec1), task_spec_id(spec5)));
|
||||
ASSERT(!task_ids_equal(task_spec_id(spec1), task_spec_id(spec6)));
|
||||
ASSERT(!task_ids_equal(task_spec_id(spec1), task_spec_id(spec7)));
|
||||
|
||||
/* Check that the return object IDs are distinct from the originals. */
|
||||
task_spec *tasks[6] = {task1, task3, task4, task5, task6, task7};
|
||||
task_spec *specs[6] = {spec1, spec3, spec4, spec5, spec6, spec7};
|
||||
for (int task_index1 = 0; task_index1 < 6; ++task_index1) {
|
||||
for (int return_index1 = 0; return_index1 < 3; ++return_index1) {
|
||||
for (int task_index2 = 0; task_index2 < 6; ++task_index2) {
|
||||
for (int return_index2 = 0; return_index2 < 3; ++return_index2) {
|
||||
if (task_index1 != task_index2 && return_index1 != return_index2) {
|
||||
ASSERT(!object_ids_equal(
|
||||
task_return(tasks[task_index1], return_index1),
|
||||
task_return(tasks[task_index2], return_index2)));
|
||||
task_return(specs[task_index1], return_index1),
|
||||
task_return(specs[task_index2], return_index2)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
free_task_spec(task1);
|
||||
free_task_spec(task2);
|
||||
free_task_spec(task3);
|
||||
free_task_spec(task4);
|
||||
free_task_spec(task5);
|
||||
free_task_spec(task6);
|
||||
free_task_spec(task7);
|
||||
free_task_spec(spec1);
|
||||
free_task_spec(spec2);
|
||||
free_task_spec(spec3);
|
||||
free_task_spec(spec4);
|
||||
free_task_spec(spec5);
|
||||
free_task_spec(spec6);
|
||||
free_task_spec(spec7);
|
||||
PASS();
|
||||
}
|
||||
|
||||
TEST send_task(void) {
|
||||
task_id parent_task_id = globally_unique_id();
|
||||
function_id func_id = globally_unique_id();
|
||||
task_spec *task =
|
||||
task_spec *spec =
|
||||
start_construct_task_spec(parent_task_id, 0, func_id, 4, 2, 10);
|
||||
task_args_add_ref(task, globally_unique_id());
|
||||
task_args_add_val(task, (uint8_t *) "Hello", 5);
|
||||
task_args_add_val(task, (uint8_t *) "World", 5);
|
||||
task_args_add_ref(task, globally_unique_id());
|
||||
finish_construct_task_spec(task);
|
||||
task_args_add_ref(spec, globally_unique_id());
|
||||
task_args_add_val(spec, (uint8_t *) "Hello", 5);
|
||||
task_args_add_val(spec, (uint8_t *) "World", 5);
|
||||
task_args_add_ref(spec, globally_unique_id());
|
||||
finish_construct_task_spec(spec);
|
||||
int fd[2];
|
||||
socketpair(AF_UNIX, SOCK_STREAM, 0, fd);
|
||||
write_message(fd[0], SUBMIT_TASK, task_size(task), (uint8_t *) task);
|
||||
write_message(fd[0], SUBMIT_TASK, task_spec_size(spec), (uint8_t *) spec);
|
||||
int64_t type;
|
||||
int64_t length;
|
||||
uint8_t *message;
|
||||
read_message(fd[1], &type, &length, &message);
|
||||
task_spec *result = (task_spec *) message;
|
||||
ASSERT(type == SUBMIT_TASK);
|
||||
ASSERT(memcmp(task, result, task_size(task)) == 0);
|
||||
ASSERT(memcmp(task, result, task_size(result)) == 0);
|
||||
free(task);
|
||||
ASSERT(memcmp(spec, result, task_spec_size(spec)) == 0);
|
||||
ASSERT(memcmp(spec, result, task_spec_size(result)) == 0);
|
||||
free(spec);
|
||||
free(result);
|
||||
PASS();
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
|
||||
#include "task.h"
|
||||
|
||||
task_spec *example_task(void) {
|
||||
task_spec *example_task_spec(void) {
|
||||
task_id parent_task_id = globally_unique_id();
|
||||
function_id func_id = globally_unique_id();
|
||||
task_spec *task =
|
||||
@@ -16,11 +16,9 @@ task_spec *example_task(void) {
|
||||
return task;
|
||||
}
|
||||
|
||||
task_instance *example_task_instance(void) {
|
||||
task_iid iid = globally_unique_id();
|
||||
task_spec *spec = example_task();
|
||||
task_instance *instance =
|
||||
make_task_instance(iid, spec, TASK_STATUS_WAITING, NIL_ID);
|
||||
task *example_task(void) {
|
||||
task_spec *spec = example_task_spec();
|
||||
task *instance = alloc_task(spec, TASK_STATUS_WAITING, NIL_ID);
|
||||
free_task_spec(spec);
|
||||
return instance;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user