forked from newrelic/newrelic-php-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_redis.c
241 lines (203 loc) · 7.39 KB
/
test_redis.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
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
231
232
233
234
235
236
237
238
239
240
241
/*
* Copyright 2020 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
#include "tlib_php.h"
#include "tlib_datastore.h"
#include "php_agent.h"
#include "php_redis.h"
#include "php_redis_private.h"
#include "util_system.h"
tlib_parallel_info_t parallel_info
= {.suggested_nthreads = -1, .state_size = 0};
static char* default_database;
static char* system_host_name;
static void test_create_datastore_instance(void) {
/*
* Test : Bad parameters.
*/
tlib_pass_if_null("NULL host_or_socket",
nr_php_redis_create_datastore_instance(NULL, 0));
/*
* Test : Normal operation.
*/
assert_datastore_instance_equals_destroy(
"UNIX socket with 0 port",
&((nr_datastore_instance_t){
.database_name = default_database,
.host = system_host_name,
.port_path_or_id = "/tmp/redis.sock",
}),
nr_php_redis_create_datastore_instance("/tmp/redis.sock", 0));
assert_datastore_instance_equals_destroy(
"UNIX socket with port set",
&((nr_datastore_instance_t){
.database_name = default_database,
.host = system_host_name,
.port_path_or_id = "/tmp/redis.sock",
}),
nr_php_redis_create_datastore_instance("/tmp/redis.sock", 6379));
assert_datastore_instance_equals_destroy(
"empty host and 0 port",
&((nr_datastore_instance_t){
.database_name = default_database,
.host = "unknown",
.port_path_or_id = "0",
}),
nr_php_redis_create_datastore_instance("", 0));
assert_datastore_instance_equals_destroy(
"empty host and port",
&((nr_datastore_instance_t){
.database_name = default_database,
.host = "unknown",
.port_path_or_id = "6379",
}),
nr_php_redis_create_datastore_instance("", 6379));
assert_datastore_instance_equals_destroy(
"localhost and 0 port",
&((nr_datastore_instance_t){
.database_name = default_database,
.host = system_host_name,
.port_path_or_id = "0",
}),
nr_php_redis_create_datastore_instance("localhost", 0));
assert_datastore_instance_equals_destroy(
"localhost and port",
&((nr_datastore_instance_t){
.database_name = default_database,
.host = system_host_name,
.port_path_or_id = "6379",
}),
nr_php_redis_create_datastore_instance("localhost", 6379));
assert_datastore_instance_equals_destroy(
"host and 0 port",
&((nr_datastore_instance_t){
.database_name = default_database,
.host = "host.name",
.port_path_or_id = "0",
}),
nr_php_redis_create_datastore_instance("host.name", 0));
assert_datastore_instance_equals_destroy(
"host and port",
&((nr_datastore_instance_t){
.database_name = default_database,
.host = "host.name",
.port_path_or_id = "6379",
}),
nr_php_redis_create_datastore_instance("host.name", 6379));
}
static void test_is_unix_socket(void) {
tlib_pass_if_int_equal("NULL", 0, nr_php_redis_is_unix_socket(NULL));
tlib_pass_if_int_equal("empty", 0, nr_php_redis_is_unix_socket(""));
tlib_pass_if_int_equal("host", 0, nr_php_redis_is_unix_socket("localhost"));
tlib_fail_if_int_equal("socket", 0, nr_php_redis_is_unix_socket("/"));
tlib_fail_if_int_equal("socket", 0, nr_php_redis_is_unix_socket("/tmp/foo"));
}
static void test_remove_datastore_instance(TSRMLS_D) {
zval* redis;
tlib_php_request_start();
redis = tlib_php_request_eval_expr("new Redis" TSRMLS_CC);
nr_php_redis_save_datastore_instance(redis, "host.name", 6379 TSRMLS_CC);
/*
* Test : Bad parameters.
*/
nr_php_redis_remove_datastore_instance(NULL TSRMLS_CC);
tlib_pass_if_size_t_equal("NULL redis_conn", 1,
nr_hashmap_count(NRPRG(datastore_connections)));
/*
* Test : Normal operation.
*/
nr_php_redis_remove_datastore_instance(redis TSRMLS_CC);
tlib_pass_if_size_t_equal("valid redis_conn", 0,
nr_hashmap_count(NRPRG(datastore_connections)));
nr_php_zval_free(&redis);
tlib_php_request_end();
}
static void test_retrieve_datastore_instance(TSRMLS_D) {
nr_datastore_instance_t* instance;
zval* redis;
tlib_php_request_start();
redis = tlib_php_request_eval_expr("new Redis" TSRMLS_CC);
/*
* Test : Bad parameters.
*/
tlib_pass_if_null("NULL redis_conn",
nr_php_redis_retrieve_datastore_instance(NULL TSRMLS_CC));
/*
* Test : Normal operation.
*/
tlib_pass_if_null("unsaved redis_conn",
nr_php_redis_retrieve_datastore_instance(redis TSRMLS_CC));
instance = nr_php_redis_save_datastore_instance(redis, "host.name",
6379 TSRMLS_CC);
tlib_pass_if_ptr_equal(
"saved redis_conn", instance,
nr_php_redis_retrieve_datastore_instance(redis TSRMLS_CC));
nr_php_zval_free(&redis);
tlib_php_request_end();
}
static void test_save_datastore_instance(TSRMLS_D) {
zval* redis;
tlib_php_request_start();
redis = tlib_php_request_eval_expr("new Redis" TSRMLS_CC);
/*
* Test : Bad parameters.
*/
tlib_pass_if_null("NULL host_or_socket", nr_php_redis_save_datastore_instance(
redis, NULL, 6379 TSRMLS_CC));
tlib_pass_if_size_t_equal("NULL host_or_socket", 0,
nr_hashmap_count(NRPRG(datastore_connections)));
/*
* Test : Normal operation.
*/
assert_datastore_instance_equals(
"NULL instance",
&((nr_datastore_instance_t){
.database_name = default_database,
.host = "host.name",
.port_path_or_id = "6379",
}),
nr_php_redis_save_datastore_instance(NULL, "host.name", 6379 TSRMLS_CC));
tlib_pass_if_size_t_equal("NULL instance", 1,
nr_hashmap_count(NRPRG(datastore_connections)));
assert_datastore_instance_equals(
"new instance",
&((nr_datastore_instance_t){
.database_name = default_database,
.host = "host.name",
.port_path_or_id = "6379",
}),
nr_php_redis_save_datastore_instance(redis, "host.name", 6379 TSRMLS_CC));
tlib_pass_if_size_t_equal("new instance", 2,
nr_hashmap_count(NRPRG(datastore_connections)));
assert_datastore_instance_equals(
"updated instance",
&((nr_datastore_instance_t){
.database_name = default_database,
.host = system_host_name,
.port_path_or_id = "/foo",
}),
nr_php_redis_save_datastore_instance(redis, "/foo", 6379 TSRMLS_CC));
tlib_pass_if_size_t_equal("updated instance", 2,
nr_hashmap_count(NRPRG(datastore_connections)));
nr_php_zval_free(&redis);
tlib_php_request_end();
}
void test_main(void* p NRUNUSED) {
#if defined(ZTS) && !defined(PHP7)
void*** tsrm_ls = NULL;
#endif /* ZTS && !PHP7 */
default_database = nr_strdup(nr_php_redis_default_database);
system_host_name = nr_system_get_hostname();
test_create_datastore_instance();
test_is_unix_socket();
tlib_php_engine_create("" PTSRMLS_CC);
if (tlib_php_require_extension("redis" TSRMLS_CC)) {
test_remove_datastore_instance(TSRMLS_C);
test_retrieve_datastore_instance(TSRMLS_C);
test_save_datastore_instance(TSRMLS_C);
}
tlib_php_engine_destroy(TSRMLS_C);
nr_free(default_database);
nr_free(system_host_name);
}