-
Notifications
You must be signed in to change notification settings - Fork 4
/
ct_async.c
106 lines (84 loc) · 3.68 KB
/
ct_async.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include "adapters/libevent.h"
#include "cluster.h"
#include "test_utils.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#define CLUSTER_NODE "127.0.0.1:7000"
void getCallback(valkeyClusterAsyncContext *acc, void *r, void *privdata) {
UNUSED(privdata);
valkeyReply *reply = (valkeyReply *)r;
ASSERT_MSG(reply != NULL, acc->errstr);
/* Disconnect after receiving the first reply to GET */
valkeyClusterAsyncDisconnect(acc);
}
void setCallback(valkeyClusterAsyncContext *acc, void *r, void *privdata) {
UNUSED(privdata);
valkeyReply *reply = (valkeyReply *)r;
ASSERT_MSG(reply != NULL, acc->errstr);
}
void connectCallback(const valkeyAsyncContext *ac, int status) {
ASSERT_MSG(status == VALKEY_OK, ac->errstr);
printf("Connected to %s:%d\n", ac->c.tcp.host, ac->c.tcp.port);
}
void connectCallbackNC(valkeyAsyncContext *ac, int status) {
UNUSED(ac);
UNUSED(status);
/* The testcase expects a failure during registration of this
non-const connect callback and it should never be called. */
assert(0);
}
void disconnectCallback(const valkeyAsyncContext *ac, int status) {
ASSERT_MSG(status == VALKEY_OK, ac->errstr);
printf("Disconnected from %s:%d\n", ac->c.tcp.host, ac->c.tcp.port);
}
void eventCallback(const valkeyClusterContext *cc, int event, void *privdata) {
(void)cc;
valkeyClusterAsyncContext *acc = (valkeyClusterAsyncContext *)privdata;
/* We send our commands when the client is ready to accept commands. */
if (event == VALKEYCLUSTER_EVENT_READY) {
int status;
status = valkeyClusterAsyncCommand(acc, setCallback, (char *)"ID",
"SET key12345 value");
ASSERT_MSG(status == VALKEY_OK, acc->errstr);
/* This command will trigger a disconnect in its reply callback. */
status = valkeyClusterAsyncCommand(acc, getCallback, (char *)"ID",
"GET key12345");
ASSERT_MSG(status == VALKEY_OK, acc->errstr);
status = valkeyClusterAsyncCommand(acc, setCallback, (char *)"ID",
"SET key23456 value2");
ASSERT_MSG(status == VALKEY_OK, acc->errstr);
status = valkeyClusterAsyncCommand(acc, getCallback, (char *)"ID",
"GET key23456");
ASSERT_MSG(status == VALKEY_OK, acc->errstr);
}
}
int main(void) {
valkeyClusterAsyncContext *acc = valkeyClusterAsyncContextInit();
assert(acc);
int status;
status = valkeyClusterAsyncSetConnectCallback(acc, connectCallback);
assert(status == VALKEY_OK);
status = valkeyClusterAsyncSetConnectCallback(acc, connectCallback);
assert(status == VALKEY_ERR); /* Re-registration not accepted */
status = valkeyClusterAsyncSetConnectCallbackNC(acc, connectCallbackNC);
assert(status == VALKEY_ERR); /* Re-registration not accepted */
status = valkeyClusterAsyncSetDisconnectCallback(acc, disconnectCallback);
assert(status == VALKEY_OK);
status = valkeyClusterSetEventCallback(acc->cc, eventCallback, acc);
assert(status == VALKEY_OK);
status = valkeyClusterSetOptionAddNodes(acc->cc, CLUSTER_NODE);
assert(status == VALKEY_OK);
/* Expect error when connecting without an attached event library. */
status = valkeyClusterAsyncConnect2(acc);
assert(status == VALKEY_ERR);
struct event_base *base = event_base_new();
status = valkeyClusterLibeventAttach(acc, base);
assert(status == VALKEY_OK);
status = valkeyClusterAsyncConnect2(acc);
assert(status == VALKEY_OK);
event_base_dispatch(base);
valkeyClusterAsyncFree(acc);
event_base_free(base);
return 0;
}