mirror of https://github.com/yandex/odyssey.git
machinarium: add basic client-server tests
This commit is contained in:
parent
22123c7823
commit
c520c508db
|
@ -29,6 +29,9 @@ extern void test_connect_cancel1(void);
|
|||
extern void test_accept_timeout(void);
|
||||
extern void test_accept_cancel(void);
|
||||
|
||||
extern void test_client_server(void);
|
||||
extern void test_client_server_readahead(void);
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
|
@ -49,5 +52,7 @@ main(int argc, char *argv[])
|
|||
machinarium_test(test_connect_cancel1);
|
||||
machinarium_test(test_accept_timeout);
|
||||
machinarium_test(test_accept_cancel);
|
||||
machinarium_test(test_client_server);
|
||||
machinarium_test(test_client_server_readahead);
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -21,7 +21,9 @@ OBJECTS = machinarium_test.o \
|
|||
test_connect_cancel0.o \
|
||||
test_connect_cancel1.o \
|
||||
test_accept_timeout.o \
|
||||
test_accept_cancel.o
|
||||
test_accept_cancel.o \
|
||||
test_client_server.o \
|
||||
test_client_server_readahead.o
|
||||
all: clean $(OBJECTS) machinarium_test
|
||||
machinarium_test:
|
||||
$(CC) $(OBJECTS) $(LFLAGS) -o machinarium_test
|
||||
|
|
|
@ -0,0 +1,94 @@
|
|||
|
||||
/*
|
||||
* machinarium.
|
||||
*
|
||||
* Cooperative multitasking engine.
|
||||
*/
|
||||
|
||||
#include <machinarium.h>
|
||||
#include <machinarium_test.h>
|
||||
|
||||
#include <string.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
static void
|
||||
server(void *arg)
|
||||
{
|
||||
machine_t machine = arg;
|
||||
machine_io_t server = machine_create_io(machine);
|
||||
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_t machine = arg;
|
||||
machine_io_t client = machine_create_io(machine);
|
||||
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);
|
||||
|
||||
machine_stop(machine);
|
||||
}
|
||||
|
||||
void
|
||||
test_client_server(void)
|
||||
{
|
||||
machine_t machine = machine_create();
|
||||
test(machine != NULL);
|
||||
|
||||
int rc;
|
||||
rc = machine_create_fiber(machine, server, machine);
|
||||
test(rc != -1);
|
||||
|
||||
rc = machine_create_fiber(machine, client, machine);
|
||||
test(rc != -1);
|
||||
|
||||
machine_start(machine);
|
||||
|
||||
rc = machine_free(machine);
|
||||
test(rc != -1);
|
||||
}
|
|
@ -0,0 +1,117 @@
|
|||
|
||||
/*
|
||||
* machinarium.
|
||||
*
|
||||
* Cooperative multitasking engine.
|
||||
*/
|
||||
|
||||
#include <machinarium.h>
|
||||
#include <machinarium_test.h>
|
||||
|
||||
#include <string.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
static void
|
||||
server(void *arg)
|
||||
{
|
||||
machine_t machine = arg;
|
||||
machine_io_t server = machine_create_io(machine);
|
||||
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" "HELLO WORLD" "a" "b" "c" "333";
|
||||
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_t machine = arg;
|
||||
machine_io_t client = machine_create_io(machine);
|
||||
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);
|
||||
|
||||
rc = machine_set_readahead(client, 1024);
|
||||
test(rc == 0);
|
||||
|
||||
char buf[16];
|
||||
rc = machine_read(client, buf, 11, INT_MAX);
|
||||
test(rc == 0);
|
||||
test(memcmp(buf, "hello world", 11) == 0);
|
||||
|
||||
rc = machine_read(client, buf, 11, INT_MAX);
|
||||
test(rc == 0);
|
||||
test(memcmp(buf, "HELLO WORLD", 11) == 0);
|
||||
|
||||
rc = machine_read(client, buf, 1, INT_MAX);
|
||||
test(rc == 0);
|
||||
test(*buf == 'a');
|
||||
|
||||
rc = machine_read(client, buf, 1, INT_MAX);
|
||||
test(rc == 0);
|
||||
test(*buf == 'b');
|
||||
|
||||
rc = machine_read(client, buf, 1, INT_MAX);
|
||||
test(rc == 0);
|
||||
test(*buf == 'c');
|
||||
|
||||
rc = machine_read(client, buf, 4, INT_MAX);
|
||||
test(rc == 0);
|
||||
test(memcmp(buf, "333", 4) == 0);
|
||||
|
||||
/* eof */
|
||||
rc = machine_read(client, buf, 1, INT_MAX);
|
||||
test(rc == -1);
|
||||
|
||||
rc = machine_close(client);
|
||||
test(rc == 0);
|
||||
machine_free_io(client);
|
||||
|
||||
machine_stop(machine);
|
||||
}
|
||||
|
||||
void
|
||||
test_client_server_readahead(void)
|
||||
{
|
||||
machine_t machine = machine_create();
|
||||
test(machine != NULL);
|
||||
|
||||
int rc;
|
||||
rc = machine_create_fiber(machine, server, machine);
|
||||
test(rc != -1);
|
||||
|
||||
rc = machine_create_fiber(machine, client, machine);
|
||||
test(rc != -1);
|
||||
|
||||
machine_start(machine);
|
||||
|
||||
rc = machine_free(machine);
|
||||
test(rc != -1);
|
||||
}
|
Loading…
Reference in New Issue