2016-09-13 18:54:26 -07:00
|
|
|
#include "greatest.h"
|
|
|
|
|
|
|
|
#include <assert.h>
|
2016-10-18 12:38:30 -07:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/wait.h>
|
2016-09-13 18:54:26 -07:00
|
|
|
|
|
|
|
#include "event_loop.h"
|
2016-09-22 23:15:45 -07:00
|
|
|
#include "test/example_task.h"
|
2016-09-13 18:54:26 -07:00
|
|
|
#include "state/db.h"
|
|
|
|
#include "state/object_table.h"
|
2016-09-29 21:12:06 -07:00
|
|
|
#include "state/task_log.h"
|
2016-09-13 18:54:26 -07:00
|
|
|
#include "state/redis.h"
|
2016-09-20 17:02:56 -07:00
|
|
|
#include "task.h"
|
2016-09-13 18:54:26 -07:00
|
|
|
|
|
|
|
SUITE(db_tests);
|
|
|
|
|
|
|
|
const char *manager_addr = "127.0.0.1";
|
2016-09-20 17:02:56 -07:00
|
|
|
int manager_port1 = 12345;
|
|
|
|
int manager_port2 = 12346;
|
|
|
|
char received_addr1[16] = {0};
|
|
|
|
char received_port1[6] = {0};
|
|
|
|
char received_addr2[16] = {0};
|
|
|
|
char received_port2[6] = {0};
|
2016-09-13 18:54:26 -07:00
|
|
|
|
2016-09-23 22:53:58 -07:00
|
|
|
/* Test if entries have been written to the database. */
|
2016-09-20 17:02:56 -07:00
|
|
|
void test_callback(object_id object_id,
|
|
|
|
int manager_count,
|
2016-10-18 12:38:30 -07:00
|
|
|
const char *manager_vector[],
|
|
|
|
void *context) {
|
2016-09-20 17:02:56 -07:00
|
|
|
CHECK(manager_count == 2);
|
|
|
|
if (!manager_vector[0] ||
|
|
|
|
sscanf(manager_vector[0], "%15[0-9.]:%5[0-9]", received_addr1,
|
|
|
|
received_port1) != 2) {
|
|
|
|
CHECK(0);
|
|
|
|
}
|
|
|
|
if (!manager_vector[1] ||
|
|
|
|
sscanf(manager_vector[1], "%15[0-9.]:%5[0-9]", received_addr2,
|
|
|
|
received_port2) != 2) {
|
|
|
|
CHECK(0);
|
2016-09-13 18:54:26 -07:00
|
|
|
}
|
2016-09-20 17:02:56 -07:00
|
|
|
free(manager_vector);
|
2016-09-13 18:54:26 -07:00
|
|
|
}
|
|
|
|
|
2016-10-12 00:23:40 -07:00
|
|
|
int timeout_handler(event_loop *loop, timer_id timer_id, void *context) {
|
2016-09-23 22:53:58 -07:00
|
|
|
event_loop_stop(loop);
|
2016-10-12 00:23:40 -07:00
|
|
|
return EVENT_LOOP_TIMER_DONE;
|
2016-09-23 22:53:58 -07:00
|
|
|
}
|
|
|
|
|
2016-09-13 18:54:26 -07:00
|
|
|
TEST object_table_lookup_test(void) {
|
2016-09-23 22:53:58 -07:00
|
|
|
event_loop *loop = event_loop_create();
|
2016-09-25 21:52:06 -07:00
|
|
|
db_handle *db1 = db_connect("127.0.0.1", 6379, "plasma_manager", manager_addr,
|
|
|
|
manager_port1);
|
|
|
|
db_handle *db2 = db_connect("127.0.0.1", 6379, "plasma_manager", manager_addr,
|
|
|
|
manager_port2);
|
|
|
|
db_attach(db1, loop);
|
|
|
|
db_attach(db2, loop);
|
2016-09-20 17:02:56 -07:00
|
|
|
unique_id id = globally_unique_id();
|
2016-09-25 21:52:06 -07:00
|
|
|
object_table_add(db1, id);
|
|
|
|
object_table_add(db2, id);
|
2016-09-23 22:53:58 -07:00
|
|
|
event_loop_add_timer(loop, 100, timeout_handler, NULL);
|
|
|
|
event_loop_run(loop);
|
2016-10-18 12:38:30 -07:00
|
|
|
object_table_lookup(db1, id, test_callback, NULL);
|
2016-09-23 22:53:58 -07:00
|
|
|
event_loop_add_timer(loop, 100, timeout_handler, NULL);
|
|
|
|
event_loop_run(loop);
|
2016-09-20 17:02:56 -07:00
|
|
|
int port1 = atoi(received_port1);
|
|
|
|
int port2 = atoi(received_port2);
|
|
|
|
ASSERT_STR_EQ(&received_addr1[0], manager_addr);
|
|
|
|
ASSERT((port1 == manager_port1 && port2 == manager_port2) ||
|
|
|
|
(port2 == manager_port1 && port1 == manager_port2));
|
|
|
|
|
2016-09-25 21:52:06 -07:00
|
|
|
db_disconnect(db1);
|
|
|
|
db_disconnect(db2);
|
2016-09-20 17:02:56 -07:00
|
|
|
|
2016-09-23 22:53:58 -07:00
|
|
|
event_loop_destroy(loop);
|
2016-09-13 18:54:26 -07:00
|
|
|
PASS();
|
|
|
|
}
|
|
|
|
|
2016-09-29 21:12:06 -07:00
|
|
|
void task_log_test_callback(task_instance *instance, void *userdata) {
|
|
|
|
task_instance *other = userdata;
|
2016-10-04 16:59:44 -07:00
|
|
|
CHECK(*task_instance_state(instance) == TASK_STATUS_SCHEDULED);
|
2016-09-29 21:12:06 -07:00
|
|
|
CHECK(task_instance_size(instance) == task_instance_size(other));
|
|
|
|
CHECK(memcmp(instance, other, task_instance_size(instance)) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST task_log_test(void) {
|
2016-09-23 22:53:58 -07:00
|
|
|
event_loop *loop = event_loop_create();
|
2016-09-25 21:52:06 -07:00
|
|
|
db_handle *db = db_connect("127.0.0.1", 6379, "local_scheduler", "", -1);
|
|
|
|
db_attach(db, loop);
|
2016-09-29 21:12:06 -07:00
|
|
|
node_id node = globally_unique_id();
|
2016-09-22 23:15:45 -07:00
|
|
|
task_spec *task = example_task();
|
2016-10-05 09:17:08 -07:00
|
|
|
task_instance *instance = make_task_instance(globally_unique_id(), task,
|
|
|
|
TASK_STATUS_SCHEDULED, node);
|
2016-10-04 16:59:44 -07:00
|
|
|
task_log_register_callback(db, task_log_test_callback, node,
|
|
|
|
TASK_STATUS_SCHEDULED, instance);
|
2016-09-29 21:12:06 -07:00
|
|
|
task_log_add_task(db, instance);
|
2016-09-23 22:53:58 -07:00
|
|
|
event_loop_add_timer(loop, 100, timeout_handler, NULL);
|
|
|
|
event_loop_run(loop);
|
2016-09-29 21:12:06 -07:00
|
|
|
task_instance_free(instance);
|
|
|
|
free_task_spec(task);
|
|
|
|
db_disconnect(db);
|
|
|
|
event_loop_destroy(loop);
|
|
|
|
PASS();
|
|
|
|
}
|
|
|
|
|
|
|
|
int num_test_callback_called = 0;
|
2016-09-22 23:15:45 -07:00
|
|
|
|
2016-09-29 21:12:06 -07:00
|
|
|
void task_log_all_test_callback(task_instance *instance, void *userdata) {
|
|
|
|
num_test_callback_called += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST task_log_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();
|
|
|
|
/* Schedule two tasks on different nodes. */
|
|
|
|
task_instance *instance1 = make_task_instance(
|
2016-10-04 16:59:44 -07:00
|
|
|
globally_unique_id(), task, TASK_STATUS_SCHEDULED, globally_unique_id());
|
2016-09-29 21:12:06 -07:00
|
|
|
task_instance *instance2 = make_task_instance(
|
2016-10-04 16:59:44 -07:00
|
|
|
globally_unique_id(), task, TASK_STATUS_SCHEDULED, globally_unique_id());
|
2016-09-29 21:12:06 -07:00
|
|
|
task_log_register_callback(db, task_log_all_test_callback, NIL_ID,
|
2016-10-04 16:59:44 -07:00
|
|
|
TASK_STATUS_SCHEDULED, NULL);
|
2016-09-29 21:12:06 -07:00
|
|
|
task_log_add_task(db, instance1);
|
|
|
|
task_log_add_task(db, instance2);
|
|
|
|
event_loop_add_timer(loop, 100, timeout_handler, NULL);
|
|
|
|
event_loop_run(loop);
|
|
|
|
task_instance_free(instance2);
|
|
|
|
task_instance_free(instance1);
|
2016-09-22 23:15:45 -07:00
|
|
|
free_task_spec(task);
|
2016-09-25 21:52:06 -07:00
|
|
|
db_disconnect(db);
|
2016-09-23 22:53:58 -07:00
|
|
|
event_loop_destroy(loop);
|
2016-09-29 21:12:06 -07:00
|
|
|
ASSERT(num_test_callback_called == 2);
|
2016-09-22 23:15:45 -07:00
|
|
|
PASS();
|
|
|
|
}
|
|
|
|
|
2016-10-18 12:38:30 -07:00
|
|
|
TEST unique_client_id_test(void) {
|
|
|
|
const int num_conns = 50;
|
|
|
|
|
|
|
|
db_handle *db;
|
|
|
|
pid_t pid = fork();
|
|
|
|
for (int i = 0; i < num_conns; ++i) {
|
|
|
|
db = db_connect("127.0.0.1", 6379, "plasma_manager", manager_addr,
|
|
|
|
manager_port1);
|
|
|
|
db_disconnect(db);
|
|
|
|
}
|
|
|
|
if (pid == 0) {
|
|
|
|
exit(0);
|
|
|
|
} else {
|
|
|
|
wait(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
db = db_connect("127.0.0.1", 6379, "plasma_manager", manager_addr,
|
|
|
|
manager_port1);
|
|
|
|
ASSERT_EQ(get_client_id(db), num_conns * 2);
|
|
|
|
db_disconnect(db);
|
|
|
|
PASS();
|
|
|
|
}
|
|
|
|
|
2016-09-13 18:54:26 -07:00
|
|
|
SUITE(db_tests) {
|
2016-09-20 22:40:35 -07:00
|
|
|
redisContext *context = redisConnect("127.0.0.1", 6379);
|
2016-09-23 22:53:58 -07:00
|
|
|
freeReplyObject(redisCommand(context, "FLUSHALL"));
|
2016-09-20 22:40:35 -07:00
|
|
|
RUN_REDIS_TEST(context, object_table_lookup_test);
|
2016-09-29 21:12:06 -07:00
|
|
|
RUN_REDIS_TEST(context, task_log_test);
|
|
|
|
RUN_REDIS_TEST(context, task_log_all_test);
|
2016-10-18 12:38:30 -07:00
|
|
|
RUN_REDIS_TEST(context, unique_client_id_test);
|
2016-09-20 22:40:35 -07:00
|
|
|
redisFree(context);
|
2016-09-13 18:54:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
GREATEST_MAIN_DEFS();
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
GREATEST_MAIN_BEGIN();
|
|
|
|
RUN_SUITE(db_tests);
|
|
|
|
GREATEST_MAIN_END();
|
|
|
|
}
|