odyssey/tests/test_client_server.c

99 lines
1.7 KiB
C

/*
* machinarium.
*
* Cooperative multitasking engine.
*/
#include <machinarium.h>
#include <machinarium_test.h>
#include <string.h>
#include <arpa/inet.h>
static void
server(void *arg)
{
machine_io_t server = machine_create_io();
test(server != NULL);
struct sockaddr_in sa;
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = inet_addr("127.0.0.1");
sa.sin_port = htons(7778);
int rc;
rc = machine_bind(server, (struct sockaddr*)&sa);
test(rc == 0);
machine_io_t client;
rc = machine_accept(server, &client, 16, INT_MAX);
test(rc == 0);
char msg[] = "hello world";
rc = machine_write(client, msg, sizeof(msg), INT_MAX);
test(rc == 0);
rc = machine_close(client);
test(rc == 0);
machine_free_io(client);
rc = machine_close(server);
test(rc == 0);
machine_free_io(server);
}
static void
client(void *arg)
{
machine_io_t client = machine_create_io();
test(client != NULL);
struct sockaddr_in sa;
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = inet_addr("127.0.0.1");
sa.sin_port = htons(7778);
int rc;
rc = machine_connect(client, (struct sockaddr*)&sa, INT_MAX);
test(rc == 0);
char buf[16];
rc = machine_read(client, buf, 12, INT_MAX);
test(rc == 0);
test(memcmp(buf, "hello world", 12) == 0);
rc = machine_read(client, buf, 1, INT_MAX);
/* eof */
test(rc == -1);
rc = machine_close(client);
test(rc == 0);
machine_free_io(client);
}
static void
test_cs(void *arg)
{
int rc;
rc = machine_create_fiber(server, NULL);
test(rc != -1);
rc = machine_create_fiber(client, NULL);
test(rc != -1);
}
void
test_client_server(void)
{
machinarium_init();
int id;
id = machine_create(test_cs, NULL);
test(id != -1);
int rc;
rc = machine_join(id);
test(rc != -1);
machinarium_free();
}