File: client.h

package info (click to toggle)
dqlite 1.18.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,364 kB
  • sloc: ansic: 57,460; makefile: 336; sh: 243
file content (230 lines) | stat: -rw-r--r-- 9,928 bytes parent folder | download
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/* Setup a test dqlite client. */

#include "endpoint.h"

#include <inttypes.h>

#ifndef TEST_CLIENT_H
#define TEST_CLIENT_H

#define FIXTURE_CLIENT                 \
	struct client_proto client;    \
	struct test_endpoint endpoint; \
	int server

#define SETUP_CLIENT                                                    \
	{                                                               \
		int _rv;                                                \
		int _client;                                            \
		test_endpoint_setup(&f->endpoint, params);              \
		_rv = listen(f->endpoint.fd, 16);                       \
		munit_assert_int(_rv, ==, 0);                           \
		test_endpoint_pair(&f->endpoint, &f->server, &_client); \
		memset(&f->client, 0, sizeof f->client);                \
		buffer__init(&f->client.read);                          \
		buffer__init(&f->client.write);                         \
		f->client.fd = _client;                                 \
	}

#define TEAR_DOWN_CLIENT         \
	clientClose(&f->client); \
	test_endpoint_tear_down(&f->endpoint)

/******************************************************************************
 *
 * Helper macros.
 *
 ******************************************************************************/

/* Send the initial client handshake. */
#define HANDSHAKE                                           \
	{                                                   \
		int rv_;                                    \
		rv_ = clientSendHandshake(f->client, NULL); \
		munit_assert_int(rv_, ==, 0);               \
	}

/* Send the initial client handshake for a specific client. */
#define HANDSHAKE_C(CLIENT)                              \
	{                                                \
		int rv_;                                 \
		rv_ = clientSendHandshake(CLIENT, NULL); \
		munit_assert_int(rv_, ==, 0);            \
	}

/* Send an add request. */
#define ADD(ID, ADDRESS)                                           \
	{                                                          \
		int rv_;                                           \
		rv_ = clientSendAdd(f->client, ID, ADDRESS, NULL); \
		munit_assert_int(rv_, ==, 0);                      \
		rv_ = clientRecvEmpty(f->client, NULL);            \
		munit_assert_int(rv_, ==, 0);                      \
	}

/* Send an assign role request. */
#define ASSIGN(ID, ROLE)                                           \
	{                                                          \
		int rv_;                                           \
		rv_ = clientSendAssign(f->client, ID, ROLE, NULL); \
		munit_assert_int(rv_, ==, 0);                      \
		rv_ = clientRecvEmpty(f->client, NULL);            \
		munit_assert_int(rv_, ==, 0);                      \
	}

/* Send a remove request. */
#define REMOVE(ID)                                           \
	{                                                    \
		int rv_;                                     \
		rv_ = clientSendRemove(f->client, ID, NULL); \
		munit_assert_int(rv_, ==, 0);                \
		rv_ = clientRecvEmpty(f->client, NULL);      \
		munit_assert_int(rv_, ==, 0);                \
	}

/* Send a transfer request. */
#define TRANSFER(ID, CLIENT)                                \
	{                                                   \
		int rv_;                                    \
		rv_ = clientSendTransfer(CLIENT, ID, NULL); \
		munit_assert_int(rv_, ==, 0);               \
		rv_ = clientRecvEmpty(CLIENT, NULL);        \
		munit_assert_int(rv_, ==, 0);               \
	}

#define OPEN_C(CLIENT, NAME)                        \
	{                                               \
		int rv_;                                    \
		rv_ = clientSendOpen(CLIENT, NAME, NULL);   \
		munit_assert_int(rv_, ==, 0);               \
		rv_ = clientRecvDb(CLIENT, NULL);           \
		munit_assert_int(rv_, ==, 0);               \
	}

/* Open a test database. */
#define OPEN OPEN_C(f->client, "test")

/* Open a test database with a specific name. */
#define OPEN_NAME(NAME)                                      \
	{                                                    \
		int rv_;                                     \
		rv_ = clientSendOpen(f->client, NAME, NULL); \
		munit_assert_int(rv_, ==, 0);                \
		rv_ = clientRecvDb(f->client, NULL);         \
		munit_assert_int(rv_, ==, 0);                \
	}

#define PREPARE_C(CLIENT, SQL, STMT_ID)                                                \
	{                                                                   \
		int rv_;                                                    \
		rv_ = clientSendPrepare(CLIENT, SQL, NULL);              \
		munit_assert_int(rv_, ==, 0);                               \
		rv_ = clientRecvStmt(CLIENT, STMT_ID, NULL, NULL, NULL); \
		munit_assert_int(rv_, ==, 0);                               \
	}

/* Prepare a statement. */
#define PREPARE(SQL, STMT_ID) PREPARE_C(f->client, SQL, STMT_ID)

#define PREPARE_FAIL(SQL, STMT_ID, RV, MSG)                        \
	{                                                          \
		int rv_;                                           \
		rv_ = clientSendPrepare(f->client, SQL, NULL);     \
		munit_assert_int(rv_, ==, 0);                      \
		rv_ = clientRecvFailure(f->client, RV, MSG, NULL); \
		munit_assert_int(rv_, ==, 0);                      \
	}

#define EXEC_C(CLIENT , STMT_ID, LAST_INSERT_ID, ROWS_AFFECTED)  \
	{                                                            \
		int rv_;                                                 \
		rv_ = clientSendExec(CLIENT, STMT_ID, NULL, 0, NULL); \
		munit_assert_int(rv_, ==, 0);                            \
		rv_ = clientRecvResult(CLIENT, LAST_INSERT_ID,        \
				       ROWS_AFFECTED, NULL);                     \
		munit_assert_int(rv_, ==, 0);                            \
	}

/* Execute a statement. */
#define EXEC(STMT_ID, LAST_INSERT_ID, ROWS_AFFECTED) EXEC_C(f->client, STMT_ID, LAST_INSERT_ID, ROWS_AFFECTED)

#define EXEC_PARAMS(STMT_ID, LAST_INSERT_ID, ROWS_AFFECTED, ...) \
	{ \
		int rv_; \
		struct value vals_[] = {__VA_ARGS__}; \
		size_t len_ = sizeof(vals_) / sizeof(vals_[0]); \
		rv_ = clientSendExec(f->client, STMT_ID, vals_, len_, NULL); \
		munit_assert_int(rv_, ==, 0); \
		rv_ = clientRecvResult(f->client, LAST_INSERT_ID, \
				       ROWS_AFFECTED, NULL); \
		munit_assert_int(rv_, ==, 0); \
	}

#define EXEC_SQL(SQL, LAST_INSERT_ID, ROWS_AFFECTED)                    \
	{                                                               \
		int rv_;                                                \
		rv_ = clientSendExecSQL(f->client, SQL, NULL, 0, NULL); \
		munit_assert_int(rv_, ==, 0);                           \
		rv_ = clientRecvResult(f->client, LAST_INSERT_ID,       \
				       ROWS_AFFECTED, NULL);            \
		munit_assert_int(rv_, ==, 0);                           \
	}

/* Perform a query. */
#define QUERY(STMT_ID, ROWS)                                              \
	{                                                                 \
		int rv_;                                                  \
		rv_ = clientSendQuery(f->client, STMT_ID, NULL, 0, NULL); \
		munit_assert_int(rv_, ==, 0);                             \
		rv_ = clientRecvRows(f->client, ROWS, NULL, NULL);        \
		munit_assert_int(rv_, ==, 0);                             \
	}

#define QUERY_DONE_C(CLIENT, STMT_ID, ROWS, ROWS_HOOK)                     \
	{                                                                  \
		int rv_;                                                   \
		bool done;                                                 \
		rv_ = clientSendQuery(CLIENT, STMT_ID, NULL, 0, NULL);     \
		munit_assert_int(rv_, ==, 0);                              \
		do {                                                       \
			rv_ = clientRecvRows(CLIENT, (ROWS), &done, NULL); \
			if (rv_ == DQLITE_CLIENT_PROTO_RECEIVED_FAILURE) { \
				munit_errorf("failure: [%" PRIu64 "] %s",           \
					     (CLIENT)->errcode,            \
					     (CLIENT)->errmsg);            \
			} else {                                           \
				munit_assert_int(rv_, ==, DQLITE_OK);      \
			}                                                  \
			ROWS_HOOK;                                         \
			clientCloseRows((ROWS));                           \
			*(ROWS) = (struct rows){};                         \
		} while (!done);                                           \
	}

#define QUERY_DONE(STMT_ID, ROWS, ROWS_HOOK) QUERY_DONE_C(f->client, STMT_ID, ROWS, ROWS_HOOK)

#define QUERY_SQL(SQL, ROWS)                                             \
	{                                                                \
		int rv_;                                                 \
		rv_ = clientSendQuerySQL(f->client, SQL, NULL, 0, NULL); \
		munit_assert_int(rv_, ==, 0);                            \
		rv_ = clientRecvRows(f->client, ROWS, NULL, NULL);       \
		munit_assert_int(rv_, ==, 0);                            \
	}

#define QUERY_SQL_DONE(SQL, ROWS, ROWS_HOOK)                                  \
	{                                                                     \
		int rv_;                                                      \
		bool done;                                                    \
		rv_ = clientSendQuerySQL(f->client, SQL, NULL, 0, NULL);      \
		munit_assert_int(rv_, ==, 0);                                 \
		do {                                                          \
			rv_ = clientRecvRows(f->client, (ROWS), &done, NULL); \
			munit_assert_int(rv_, ==, 0);                         \
			ROWS_HOOK;                                            \
			clientCloseRows((ROWS));                              \
			*(ROWS) = (struct rows){};                            \
		} while (!done);                                              \
	}

#endif /* TEST_CLIENT_H */