Skip to content

Commit 5898470

Browse files
committed
add APM to tests
1 parent 472242b commit 5898470

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed

src/libmongoc/tests/test-mongoc-loadbalanced.c

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,119 @@
2020
#include "test-libmongoc.h"
2121
#include "TestSuite.h"
2222

23+
typedef struct {
24+
int server_changed_events;
25+
int server_opening_events;
26+
int server_closed_events;
27+
int topology_changed_events;
28+
int topology_opening_events;
29+
int topology_closed_events;
30+
} stats_t;
31+
32+
static void
33+
server_changed (const mongoc_apm_server_changed_t *event)
34+
{
35+
stats_t *context;
36+
37+
context = (stats_t *) mongoc_apm_server_changed_get_context (event);
38+
context->server_changed_events++;
39+
}
40+
41+
42+
static void
43+
server_opening (const mongoc_apm_server_opening_t *event)
44+
{
45+
stats_t *context;
46+
47+
context = (stats_t *) mongoc_apm_server_opening_get_context (event);
48+
context->server_opening_events++;
49+
}
50+
51+
52+
static void
53+
server_closed (const mongoc_apm_server_closed_t *event)
54+
{
55+
stats_t *context;
56+
57+
context = (stats_t *) mongoc_apm_server_closed_get_context (event);
58+
context->server_closed_events++;
59+
}
60+
61+
62+
static void
63+
topology_changed (const mongoc_apm_topology_changed_t *event)
64+
{
65+
stats_t *context;
66+
67+
context = (stats_t *) mongoc_apm_topology_changed_get_context (event);
68+
context->topology_changed_events++;
69+
}
70+
71+
72+
static void
73+
topology_opening (const mongoc_apm_topology_opening_t *event)
74+
{
75+
stats_t *context;
76+
77+
context = (stats_t *) mongoc_apm_topology_opening_get_context (event);
78+
context->topology_opening_events++;
79+
}
80+
81+
82+
static void
83+
topology_closed (const mongoc_apm_topology_closed_t *event)
84+
{
85+
stats_t *context;
86+
87+
context = (stats_t *) mongoc_apm_topology_closed_get_context (event);
88+
context->topology_closed_events++;
89+
}
90+
91+
static mongoc_apm_callbacks_t * make_callbacks (void) {
92+
mongoc_apm_callbacks_t *cbs;
93+
94+
cbs = mongoc_apm_callbacks_new ();
95+
mongoc_apm_set_server_changed_cb (cbs, server_changed);
96+
mongoc_apm_set_server_opening_cb (cbs, server_opening);
97+
mongoc_apm_set_server_closed_cb (cbs, server_closed);
98+
mongoc_apm_set_topology_changed_cb (cbs, topology_changed);
99+
mongoc_apm_set_topology_opening_cb (cbs, topology_opening);
100+
mongoc_apm_set_topology_closed_cb (cbs, topology_closed);
101+
return cbs;
102+
}
103+
104+
static stats_t * set_client_callbacks (mongoc_client_t *client) {
105+
mongoc_apm_callbacks_t *cbs;
106+
stats_t *stats;
107+
108+
stats = bson_malloc0 (sizeof (stats_t));
109+
cbs = make_callbacks ();
110+
mongoc_client_set_apm_callbacks (client, cbs, stats);
111+
mongoc_apm_callbacks_destroy (cbs);
112+
return stats;
113+
}
114+
115+
static stats_t * set_client_pool_callbacks (mongoc_client_pool_t *pool) {
116+
mongoc_apm_callbacks_t *cbs;
117+
stats_t *stats;
118+
119+
stats = bson_malloc0 (sizeof (stats_t));
120+
cbs = make_callbacks ();
121+
mongoc_client_pool_set_apm_callbacks (pool, cbs, stats);
122+
mongoc_apm_callbacks_destroy (cbs);
123+
return stats;
124+
}
125+
126+
static void free_and_assert_stats (stats_t *stats) {
127+
ASSERT_CMPINT (stats->topology_opening_events, ==, 1);
128+
ASSERT_CMPINT (stats->topology_changed_events, ==, 2);
129+
ASSERT_CMPINT (stats->server_opening_events, ==, 1);
130+
ASSERT_CMPINT (stats->server_changed_events, ==, 1);
131+
ASSERT_CMPINT (stats->server_closed_events, ==, 1);
132+
ASSERT_CMPINT (stats->topology_closed_events, ==, 1);
133+
bson_free (stats);
134+
}
135+
23136
static char *
24137
loadbalanced_uri (void)
25138
{
@@ -142,8 +255,10 @@ test_loadbalanced_connect_single (void *unused)
142255
bson_error_t error;
143256
bool ok;
144257
mongoc_server_description_t *monitor_sd;
258+
stats_t *stats;
145259

146260
client = mongoc_client_new (uristr);
261+
stats = set_client_callbacks (client);
147262
ok = mongoc_client_command_simple (client,
148263
"admin",
149264
tmp_bson ("{'ping': 1}"),
@@ -161,6 +276,7 @@ test_loadbalanced_connect_single (void *unused)
161276
mongoc_server_description_destroy (monitor_sd);
162277
bson_free (uristr);
163278
mongoc_client_destroy (client);
279+
free_and_assert_stats (stats);
164280
}
165281

166282
static void
@@ -173,10 +289,12 @@ test_loadbalanced_connect_pooled (void *unused)
173289
bson_error_t error;
174290
bool ok;
175291
mongoc_server_description_t *monitor_sd;
292+
stats_t *stats;
176293

177294
uristr = loadbalanced_uri ();
178295
uri = mongoc_uri_new (uristr);
179296
pool = mongoc_client_pool_new (uri);
297+
stats = set_client_pool_callbacks (pool);
180298
client = mongoc_client_pool_pop (pool);
181299

182300
ok = mongoc_client_command_simple (client,
@@ -198,6 +316,7 @@ test_loadbalanced_connect_pooled (void *unused)
198316
mongoc_uri_destroy (uri);
199317
mongoc_client_pool_push (pool, client);
200318
mongoc_client_pool_destroy (pool);
319+
free_and_assert_stats (stats);
201320
}
202321

203322
/* Ensure that server selection on single threaded clients establishes a

0 commit comments

Comments
 (0)