mirror of
https://github.com/vale981/ray
synced 2025-03-12 14:16:39 -04:00

* Allow reading/writing generic message types, not just tasks. * Allow messages of length 0 to be read/written, and handle closed sockets. * Address comments. * Simplify accept_client. * Allow ports to be reused in bind_ipc_sock.
60 lines
1.3 KiB
C
60 lines
1.3 KiB
C
#include "greatest.h"
|
|
|
|
#include <assert.h>
|
|
#include <unistd.h>
|
|
#include <inttypes.h>
|
|
|
|
#include "io.h"
|
|
|
|
SUITE(io_tests);
|
|
|
|
TEST ipc_socket_test(void) {
|
|
const char *socket_pathname = "test-socket";
|
|
int socket_fd = bind_ipc_sock(socket_pathname);
|
|
ASSERT(socket_fd >= 0);
|
|
|
|
char *test_string = "hello world";
|
|
char *test_bytes = "another string";
|
|
pid_t pid = fork();
|
|
if (pid == 0) {
|
|
close(socket_fd);
|
|
socket_fd = connect_ipc_sock(socket_pathname);
|
|
ASSERT(socket_fd >= 0);
|
|
write_log_message(socket_fd, test_string);
|
|
write_message(socket_fd, LOG_MESSAGE, strlen(test_bytes),
|
|
(uint8_t *) test_bytes);
|
|
close(socket_fd);
|
|
exit(0);
|
|
} else {
|
|
int client_fd = accept_client(socket_fd);
|
|
ASSERT(client_fd >= 0);
|
|
char *message = read_log_message(client_fd);
|
|
ASSERT(message != NULL);
|
|
ASSERT_STR_EQ(test_string, message);
|
|
free(message);
|
|
int64_t type;
|
|
int64_t len;
|
|
uint8_t *bytes;
|
|
read_message(client_fd, &type, &len, &bytes);
|
|
ASSERT(type == LOG_MESSAGE);
|
|
ASSERT(memcmp(test_bytes, bytes, len) == 0);
|
|
free(bytes);
|
|
close(client_fd);
|
|
close(socket_fd);
|
|
unlink(socket_pathname);
|
|
}
|
|
|
|
PASS();
|
|
}
|
|
|
|
SUITE(io_tests) {
|
|
RUN_TEST(ipc_socket_test);
|
|
}
|
|
|
|
GREATEST_MAIN_DEFS();
|
|
|
|
int main(int argc, char **argv) {
|
|
GREATEST_MAIN_BEGIN();
|
|
RUN_SUITE(io_tests);
|
|
GREATEST_MAIN_END();
|
|
}
|