File: server.c

package info (click to toggle)
cowsql 1.15.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,704 kB
  • sloc: ansic: 23,583; makefile: 137; python: 11
file content (173 lines) | stat: -rw-r--r-- 4,129 bytes parent folder | download | duplicates (3)
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
#include <unistd.h>

#include "fs.h"
#include "server.h"

static int endpointConnect(void *data, const char *address, int *fd)
{
	struct sockaddr_un addr;
	int rv;
	(void)address;
	(void)data;
	memset(&addr, 0, sizeof addr);
	addr.sun_family = AF_UNIX;
	strcpy(addr.sun_path + 1, address + 1);
	*fd = socket(AF_UNIX, SOCK_STREAM, 0);
	munit_assert_int(*fd, !=, -1);
	rv = connect(*fd, (struct sockaddr *)&addr,
		     sizeof(sa_family_t) + strlen(address + 1) + 1);
	return rv;
}

void test_server_setup(struct test_server *s,
		       const unsigned id,
		       const MunitParameter params[])
{
	(void)params;

	s->id = id;
	sprintf(s->address, "@%u", id);

	s->dir = test_dir_setup();
	s->role_management = false;

	memset(s->others, 0, sizeof s->others);
}

void test_server_stop(struct test_server *s)
{
	int rv;

	test_server_client_close(s, &s->client);

	if (s->role_management) {
		cowsql_node_handover(s->cowsql);
		rv = cowsql_node_stop(s->cowsql);
	} else {
		rv = cowsql_node_stop(s->cowsql);
	}
	munit_assert_int(rv, ==, 0);

	cowsql_node_destroy(s->cowsql);
}

void test_server_tear_down(struct test_server *s)
{
	test_server_stop(s);
	test_dir_tear_down(s->dir);
}

void test_server_start(struct test_server *s, const MunitParameter params[])
{
	int rv;

	rv = cowsql_node_create(s->id, s->address, s->dir, &s->cowsql);
	munit_assert_int(rv, ==, 0);

	rv = cowsql_node_set_bind_address(s->cowsql, s->address);
	munit_assert_int(rv, ==, 0);

	rv = cowsql_node_set_connect_func(s->cowsql, endpointConnect, s);
	munit_assert_int(rv, ==, 0);

	rv = cowsql_node_set_network_latency_ms(s->cowsql, 30);
	munit_assert_int(rv, ==, 0);

	const char *snapshot_threshold_param =
	    munit_parameters_get(params, SNAPSHOT_THRESHOLD_PARAM);
	if (snapshot_threshold_param != NULL) {
		unsigned threshold = (unsigned)atoi(snapshot_threshold_param);
		rv = cowsql_node_set_snapshot_params(s->cowsql, threshold,
						     threshold);
		munit_assert_int(rv, ==, 0);
	}

	if (getenv("CI") != NULL) {
		const char *target_voters_param =
		    munit_parameters_get(params, "target_voters");
		if (target_voters_param != NULL) {
			int n = atoi(target_voters_param);
			rv = cowsql_node_set_target_voters(s->cowsql, n);
			munit_assert_int(rv, ==, 0);
		}

		const char *target_standbys_param =
		    munit_parameters_get(params, "target_standbys");
		if (target_standbys_param != NULL) {
			int n = atoi(target_standbys_param);
			rv = cowsql_node_set_target_standbys(s->cowsql, n);
			munit_assert_int(rv, ==, 0);
		}

		const char *role_management_param =
		    munit_parameters_get(params, "role_management");
		if (role_management_param != NULL) {
			bool role_management =
			    (bool)atoi(role_management_param);
			s->role_management = role_management;
			if (role_management) {
				rv = cowsql_node_enable_role_management(
				    s->cowsql);
				munit_assert_int(rv, ==, 0);
			}
		}
	}

	rv = cowsql_node_start(s->cowsql);
	munit_assert_int(rv, ==, 0);

	test_server_client_connect(s, &s->client);
}

struct client_proto *test_server_client(struct test_server *s)
{
	return &s->client;
}

void test_server_client_reconnect(struct test_server *s, struct client_proto *c)
{
	test_server_client_close(s, c);
	test_server_client_connect(s, c);
}

void test_server_client_connect(struct test_server *s, struct client_proto *c)
{
	int rv;
	int fd;

	rv = endpointConnect(NULL, s->address, &fd);
	munit_assert_int(rv, ==, 0);

	memset(c, 0, sizeof *c);
	buffer__init(&c->read);
	buffer__init(&c->write);
	c->fd = fd;
}

void test_server_client_close(struct test_server *s, struct client_proto *c)
{
	(void)s;
	clientClose(c);
}

static void setOther(struct test_server *s, struct test_server *other)
{
	unsigned i = other->id - 1;
	munit_assert_ptr_null(s->others[i]);
	s->others[i] = other;
}
void test_server_network(struct test_server *servers, unsigned n_servers)
{
	unsigned i;
	unsigned j;
	for (i = 0; i < n_servers; i++) {
		for (j = 0; j < n_servers; j++) {
			struct test_server *server = &servers[i];
			struct test_server *other = &servers[j];
			if (i == j) {
				continue;
			}
			setOther(server, other);
		}
	}
}