Moved main to test
Moved esc.h to include Added append_to_stream on connection
This commit is contained in:
89
test/main.c
Normal file
89
test/main.c
Normal file
@@ -0,0 +1,89 @@
|
||||
#include <stdio.h>
|
||||
#include "../src/utils/debug.h"
|
||||
#include "../include/esc.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <winsock.h>
|
||||
#include <windows.h>
|
||||
#define usleep Sleep
|
||||
#define sleep(x) Sleep(x*1000)
|
||||
#endif
|
||||
#ifdef __linux__
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
int main() {
|
||||
#ifdef _WIN32
|
||||
WSADATA wsaData;
|
||||
WSAStartup(MAKEWORD(2,0), &wsaData);
|
||||
#endif
|
||||
|
||||
esc_connection_t* conn = esc_connection_create(esc_default_connection_settings, "tcp://127.0.0.1:1113", NULL);
|
||||
if (esc_connection_connect(conn) != 0) {
|
||||
error_t* err = esc_connection_last_error(conn);
|
||||
fprintf(stderr, "Error: %s code=%d file=%s line=%d\n", err->message, err->code, err->file, err->line);
|
||||
return -2;
|
||||
}
|
||||
|
||||
uuid_t *event_id = uuid_create();
|
||||
buffer_t *data = buffer_from_string("{\"a\":\"1\"}");
|
||||
esc_event_data_t* event_data = esc_event_data_create(event_id, "test_event", BOOL_TRUE, data, 0);
|
||||
array_t* events = array_create(1, event_data);
|
||||
esc_write_result_t* write_result = esc_append_to_stream(conn, "test-123", ESC_VERSION_ANY, events);
|
||||
if (write_result == 0) {
|
||||
error_t* err = esc_connection_last_error(conn);
|
||||
fprintf(stderr, "Error: %s code=%d file=%s line=%d\n", err->message, err->code, err->file, err->line);
|
||||
return -3;
|
||||
}
|
||||
esc_write_result_destroy(write_result);
|
||||
array_destroy(events, (array_deallocator)esc_event_data_destroy);
|
||||
uuid_destroy(event_id);
|
||||
buffer_destroy(data);
|
||||
|
||||
esc_credentials_t* credentials = esc_credentials_create("admin", "changeit");
|
||||
|
||||
esc_all_events_slice_t* result = 0;
|
||||
do {
|
||||
esc_all_events_slice_t* old_result = result;
|
||||
result = esc_connection_read_all_forward(conn, old_result ? old_result->next_position : NULL, 100, credentials);
|
||||
if (old_result) esc_all_events_slice_destroy(old_result);
|
||||
if (result == 0) {
|
||||
error_t* err = esc_connection_last_error(conn);
|
||||
fprintf(stderr, "Error: %s code=%d file=%s line=%d\n", err->message, err->code, err->file, err->line);
|
||||
break;
|
||||
}
|
||||
char posbuf1[44];
|
||||
char posbuf2[44];
|
||||
printf("%s %s %s %u\n", result->read_direction,
|
||||
esc_position_format(result->from_position, posbuf1, 44),
|
||||
esc_position_format(result->next_position, posbuf2, 44),
|
||||
result->is_end_of_stream);
|
||||
char uuid_buf[37];
|
||||
for (size_t i = 0; i < result->n_events; i++) {
|
||||
#ifdef _WIN32
|
||||
printf("%s %s %lld@%s %llu %llu\n", esc_uuid_format(result->events[i]->event->event_id, uuid_buf, 37),
|
||||
#else
|
||||
printf("%s %s %ld@%s %lu %lu\n", uuid_format(result->events[i]->event->event_id, uuid_buf, 37),
|
||||
#endif
|
||||
result->events[i]->event->event_type,
|
||||
result->events[i]->event->event_number,
|
||||
result->events[i]->event->event_stream_id,
|
||||
buffer_size(result->events[i]->event->data),
|
||||
buffer_size(result->events[i]->event->metadata));
|
||||
}
|
||||
} while(result->is_end_of_stream == 0);
|
||||
|
||||
if (result) esc_all_events_slice_destroy(result);
|
||||
|
||||
sleep(5);
|
||||
|
||||
esc_connection_destroy(conn);
|
||||
esc_credentials_destroy(credentials);
|
||||
|
||||
dbg_list_allocs();
|
||||
|
||||
#ifdef _WIN32
|
||||
WSACleanup();
|
||||
#endif
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user