mirror of
https://github.com/wassname/ray.git
synced 2026-08-18 12:20:14 +08:00
task queue tests and extensions (#18)
* task queue tests and extensions * clean up test
This commit is contained in:
committed by
Robert Nishihara
parent
313241e303
commit
7a079547b0
@@ -0,0 +1,27 @@
|
||||
#include "greatest.h"
|
||||
|
||||
#include "common.h"
|
||||
|
||||
SUITE(common_tests);
|
||||
|
||||
TEST sha1_test(void) {
|
||||
static char hex[2 * UNIQUE_ID_SIZE + 1];
|
||||
static unsigned char id[UNIQUE_ID_SIZE];
|
||||
unique_id uid = globally_unique_id();
|
||||
sha1_to_hex(&uid.id[0], &hex[0]);
|
||||
hex_to_sha1(&hex[0], &id[0]);
|
||||
ASSERT(memcmp(&uid.id[0], &id[0], 20) == 0);
|
||||
PASS();
|
||||
}
|
||||
|
||||
SUITE(common_tests) {
|
||||
RUN_TEST(sha1_test);
|
||||
}
|
||||
|
||||
GREATEST_MAIN_DEFS();
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
GREATEST_MAIN_BEGIN();
|
||||
RUN_SUITE(common_tests);
|
||||
GREATEST_MAIN_END();
|
||||
}
|
||||
+37
-2
@@ -3,6 +3,7 @@
|
||||
#include <assert.h>
|
||||
|
||||
#include "event_loop.h"
|
||||
#include "test/example_task.h"
|
||||
#include "state/db.h"
|
||||
#include "state/object_table.h"
|
||||
#include "state/redis.h"
|
||||
@@ -62,7 +63,7 @@ TEST object_table_lookup_test(void) {
|
||||
object_table_add(&conn2, id);
|
||||
object_table_lookup(&conn1, id, sync_test_callback);
|
||||
while (!lookup_successful) {
|
||||
int num_ready = event_loop_poll(&loop);
|
||||
int num_ready = event_loop_poll(&loop, -1);
|
||||
if (num_ready < 0) {
|
||||
exit(-1);
|
||||
}
|
||||
@@ -81,7 +82,7 @@ TEST object_table_lookup_test(void) {
|
||||
lookup_successful = 0;
|
||||
object_table_lookup(&conn1, id, test_callback);
|
||||
while (!lookup_successful) {
|
||||
int num_ready = event_loop_poll(&loop);
|
||||
int num_ready = event_loop_poll(&loop, -1);
|
||||
if (num_ready < 0) {
|
||||
exit(-1);
|
||||
}
|
||||
@@ -112,10 +113,44 @@ TEST object_table_lookup_test(void) {
|
||||
PASS();
|
||||
}
|
||||
|
||||
TEST task_queue_test(void) {
|
||||
event_loop loop;
|
||||
event_loop_init(&loop);
|
||||
db_conn conn;
|
||||
db_connect("127.0.0.1", 6379, "local_scheduler", "", -1, &conn);
|
||||
int64_t index = db_attach(&conn, &loop, 0);
|
||||
|
||||
task_spec *task = example_task();
|
||||
task_queue_submit_task(&conn, globally_unique_id(), task);
|
||||
while (1) {
|
||||
int num_ready = event_loop_poll(&loop, 100);
|
||||
if (num_ready < 0) {
|
||||
exit(-1);
|
||||
}
|
||||
if (num_ready == 0) {
|
||||
break;
|
||||
}
|
||||
for (int i = 0; i < event_loop_size(&loop); ++i) {
|
||||
struct pollfd *waiting = event_loop_get(&loop, i);
|
||||
if (waiting->revents == 0)
|
||||
continue;
|
||||
if (i == index) {
|
||||
db_event(&conn);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
free_task_spec(task);
|
||||
db_disconnect(&conn);
|
||||
event_loop_free(&loop);
|
||||
PASS();
|
||||
}
|
||||
|
||||
SUITE(db_tests) {
|
||||
redisContext *context = redisConnect("127.0.0.1", 6379);
|
||||
redisCommand(context, "FLUSHALL");
|
||||
RUN_REDIS_TEST(context, object_table_lookup_test);
|
||||
RUN_TEST(task_queue_test);
|
||||
redisFree(context);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
#ifndef EXAMPLE_TASK_H
|
||||
#define EXAMPLE_TASK_H
|
||||
|
||||
#include "task.h"
|
||||
|
||||
task_spec *example_task(void) {
|
||||
function_id func_id = globally_unique_id();
|
||||
task_spec *task = alloc_task_spec(func_id, 2, 1, 0);
|
||||
task_args_add_ref(task, globally_unique_id());
|
||||
task_args_add_ref(task, globally_unique_id());
|
||||
return task;
|
||||
}
|
||||
|
||||
#endif
|
||||
+1
-1
@@ -85,7 +85,7 @@ TEST async_redis_socket_test(void) {
|
||||
send_redis_command(client_fd, test_set_format, test_key, test_value);
|
||||
|
||||
while (!lookup_successful) {
|
||||
int num_ready = event_loop_poll(&loop);
|
||||
int num_ready = event_loop_poll(&loop, -1);
|
||||
if (num_ready < 0) {
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include <sys/socket.h>
|
||||
|
||||
#include "common.h"
|
||||
#include "test/example_task.h"
|
||||
#include "task.h"
|
||||
|
||||
SUITE(task_tests);
|
||||
@@ -56,9 +57,28 @@ TEST send_task(void) {
|
||||
PASS();
|
||||
}
|
||||
|
||||
TEST print_and_parse_task(void) {
|
||||
task_spec *task = example_task();
|
||||
|
||||
UT_string *output;
|
||||
utstring_new(output);
|
||||
print_task(task, output);
|
||||
task_spec *result = parse_task(utstring_body(output), utstring_len(output));
|
||||
utstring_free(output);
|
||||
|
||||
ASSERT_EQ(task_size(task), task_size(result));
|
||||
ASSERT(memcmp(task, result, task_size(task)) == 0);
|
||||
|
||||
free_task_spec(task);
|
||||
free_task_spec(result);
|
||||
|
||||
PASS();
|
||||
}
|
||||
|
||||
SUITE(task_tests) {
|
||||
RUN_TEST(task_test);
|
||||
RUN_TEST(send_task);
|
||||
RUN_TEST(print_and_parse_task);
|
||||
}
|
||||
|
||||
GREATEST_MAIN_DEFS();
|
||||
|
||||
Reference in New Issue
Block a user