eventstore-client-c/test/main.c

100 lines
3.7 KiB
C
Raw Permalink Normal View History

#ifdef _WIN32
#include <winsock.h>
#include <windows.h>
#define usleep Sleep
#define sleep(x) Sleep(x*1000)
#endif
#ifdef __linux__
#include <unistd.h>
#endif
#include <stdio.h>
#include "../src/utils/debug.h"
#include "../include/esc.h"
void my_event_appeared(void *ctx, esc_subscription_t *s, esc_resolved_event_t *e) {
printf("Event Appeared: ctx=%p s=%p e=%s\n", ctx, s, e->event ? e->event->event_type : e->link ? e->link->event_type : "<unresolved>");
}
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", 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);
esc_event_appeared_callback_t my_event_appeared_callback = {my_event_appeared, 0};
esc_subscription_t* subscription = esc_connection_subscribe_to_all(conn, BOOL_TRUE, my_event_appeared_callback, esc_empty_subscription_dropped_callback, credentials);
sleep(60);
esc_subscription_destroy(subscription);
esc_connection_destroy(conn);
esc_credentials_destroy(credentials);
2018-03-24 17:09:02 +00:00
#ifdef DEBUG
dbg_list_allocs();
2018-03-24 17:09:02 +00:00
#endif
#ifdef _WIN32
WSACleanup();
#endif
return 0;
}