15
15
*/
16
16
17
17
18
+ #include "mongoc/mongoc-client-private.h"
18
19
#include "mongoc/mongoc-config.h"
19
20
#include "mongoc/mongoc-collection-private.h"
20
21
#include "mongoc/mongoc-host-list-private.h"
@@ -85,19 +86,22 @@ started_cb (const mongoc_apm_command_started_t *event)
85
86
{
86
87
json_test_ctx_t * ctx =
87
88
(json_test_ctx_t * ) mongoc_apm_command_started_get_context (event );
88
- char * cmd_json ;
89
89
bson_t * events = & ctx -> events ;
90
90
char str [16 ];
91
91
const char * key ;
92
92
bson_t * new_event ;
93
93
94
94
if (ctx -> verbose ) {
95
+ char * cmd_json ;
96
+
95
97
cmd_json = bson_as_canonical_extended_json (event -> command , NULL );
96
98
printf ("%s\n" , cmd_json );
97
99
fflush (stdout );
98
100
bson_free (cmd_json );
99
101
}
100
102
103
+ bson_mutex_lock (& ctx -> mutex );
104
+
101
105
/* Track the last two lsid's */
102
106
if (bson_has_field (event -> command , "lsid" )) {
103
107
bson_t lsid ;
@@ -132,6 +136,7 @@ started_cb (const mongoc_apm_command_started_t *event)
132
136
ctx -> n_events ++ ;
133
137
134
138
bson_destroy (new_event );
139
+ bson_mutex_unlock (& ctx -> mutex );
135
140
}
136
141
137
142
@@ -140,18 +145,22 @@ succeeded_cb (const mongoc_apm_command_succeeded_t *event)
140
145
{
141
146
json_test_ctx_t * ctx =
142
147
(json_test_ctx_t * ) mongoc_apm_command_succeeded_get_context (event );
143
- char * reply_json ;
144
148
char str [16 ];
145
149
const char * key ;
146
150
bson_t * new_event ;
147
151
148
152
if (ctx -> verbose ) {
153
+ char * reply_json ;
154
+
149
155
reply_json = bson_as_canonical_extended_json (event -> reply , NULL );
150
- printf ("\t\t<-- %s\n" , reply_json );
151
- fflush (stdout );
156
+ MONGOC_DEBUG ("<-- COMMAND SUCCEEDED: %s\n" , reply_json );
152
157
bson_free (reply_json );
153
158
}
154
159
160
+ if (ctx -> config -> command_started_events_only ) {
161
+ return ;
162
+ }
163
+
155
164
BSON_ASSERT (mongoc_apm_command_succeeded_get_request_id (event ) > 0 );
156
165
BSON_ASSERT (mongoc_apm_command_succeeded_get_server_id (event ) > 0 );
157
166
assert_host_in_uri (event -> host , ctx -> test_framework_uri );
@@ -165,12 +174,14 @@ succeeded_cb (const mongoc_apm_command_succeeded_t *event)
165
174
BCON_INT64 (event -> operation_id ),
166
175
"}" );
167
176
177
+ bson_mutex_lock (& ctx -> mutex );
168
178
bson_uint32_to_string (ctx -> n_events , & key , str , sizeof str );
169
179
BSON_APPEND_DOCUMENT (& ctx -> events , key , new_event );
170
180
171
181
ctx -> n_events ++ ;
172
182
173
183
bson_destroy (new_event );
184
+ bson_mutex_unlock (& ctx -> mutex );
174
185
}
175
186
176
187
@@ -179,15 +190,23 @@ failed_cb (const mongoc_apm_command_failed_t *event)
179
190
{
180
191
json_test_ctx_t * ctx =
181
192
(json_test_ctx_t * ) mongoc_apm_command_failed_get_context (event );
182
- bson_t reply = BSON_INITIALIZER ;
183
193
char str [16 ];
184
194
const char * key ;
185
195
bson_t * new_event ;
186
196
187
197
if (ctx -> verbose ) {
188
- printf (
189
- "\t\t<-- %s FAILED: %s\n" , event -> command_name , event -> error -> message );
190
- fflush (stdout );
198
+ char * reply_json ;
199
+
200
+ reply_json = bson_as_canonical_extended_json (event -> reply , NULL );
201
+ MONGOC_DEBUG ("<-- %s COMMAND FAILED: %s\nREPLY: %s\n" ,
202
+ event -> command_name ,
203
+ event -> error -> message ,
204
+ reply_json );
205
+ bson_free (reply_json );
206
+ }
207
+
208
+ if (ctx -> config -> command_started_events_only ) {
209
+ return ;
191
210
}
192
211
193
212
BSON_ASSERT (mongoc_apm_command_failed_get_request_id (event ) > 0 );
@@ -202,15 +221,44 @@ failed_cb (const mongoc_apm_command_failed_t *event)
202
221
BCON_INT64 (event -> operation_id ),
203
222
"}" );
204
223
224
+ bson_mutex_lock (& ctx -> mutex );
205
225
bson_uint32_to_string (ctx -> n_events , & key , str , sizeof str );
206
226
BSON_APPEND_DOCUMENT (& ctx -> events , key , new_event );
207
227
208
228
ctx -> n_events ++ ;
209
229
210
230
bson_destroy (new_event );
211
- bson_destroy (& reply );
231
+ bson_mutex_unlock (& ctx -> mutex );
212
232
}
213
233
234
+ static void
235
+ server_changed_cb (const mongoc_apm_server_changed_t * event )
236
+ {
237
+ json_test_ctx_t * ctx ;
238
+ const mongoc_server_description_t * sd ;
239
+ const mongoc_server_description_t * prev_sd ;
240
+
241
+ ctx = mongoc_apm_server_changed_get_context (event );
242
+ sd = mongoc_apm_server_changed_get_new_description (event );
243
+ prev_sd = mongoc_apm_server_changed_get_previous_description (event );
244
+ if (ctx -> verbose ) {
245
+ MONGOC_DEBUG ("SERVER CHANGED: (%s) %s --> %s\n" ,
246
+ sd -> host .host_and_port ,
247
+ mongoc_server_description_type (prev_sd ),
248
+ mongoc_server_description_type (sd ));
249
+ }
250
+
251
+ bson_mutex_lock (& ctx -> mutex );
252
+ if (sd -> type == MONGOC_SERVER_UNKNOWN ) {
253
+ ctx -> total_ServerMarkedUnknownEvent ++ ;
254
+ }
255
+ if (sd -> type == MONGOC_SERVER_RS_PRIMARY &&
256
+ !_mongoc_host_list_compare_one (& sd -> host , & ctx -> primary_host )) {
257
+ ctx -> total_PrimaryChangedEvent ++ ;
258
+ memcpy (& ctx -> primary_host , & sd -> host , sizeof (mongoc_host_list_t ));
259
+ }
260
+ bson_mutex_unlock (& ctx -> mutex );
261
+ }
214
262
215
263
void
216
264
set_apm_callbacks (json_test_ctx_t * ctx , mongoc_client_t * client )
@@ -219,13 +267,27 @@ set_apm_callbacks (json_test_ctx_t *ctx, mongoc_client_t *client)
219
267
220
268
callbacks = mongoc_apm_callbacks_new ();
221
269
mongoc_apm_set_command_started_cb (callbacks , started_cb );
270
+ /* Even if test only checks command started events (i.e.
271
+ * command_started_events_only is set on test config), set callbacks for the
272
+ * benefit of logging. */
273
+ mongoc_apm_set_command_succeeded_cb (callbacks , succeeded_cb );
274
+ mongoc_apm_set_command_failed_cb (callbacks , failed_cb );
275
+ mongoc_client_set_apm_callbacks (ctx -> client , callbacks , ctx );
222
276
223
- if (!ctx -> config -> command_started_events_only ) {
224
- mongoc_apm_set_command_succeeded_cb (callbacks , succeeded_cb );
225
- mongoc_apm_set_command_failed_cb (callbacks , failed_cb );
226
- }
277
+ mongoc_apm_callbacks_destroy (callbacks );
278
+ }
227
279
228
- mongoc_client_set_apm_callbacks (client , callbacks , ctx );
280
+ void
281
+ set_apm_callbacks_pooled (json_test_ctx_t * ctx , mongoc_client_pool_t * pool )
282
+ {
283
+ mongoc_apm_callbacks_t * callbacks ;
284
+
285
+ callbacks = mongoc_apm_callbacks_new ();
286
+ mongoc_apm_set_command_started_cb (callbacks , started_cb );
287
+ mongoc_apm_set_server_changed_cb (callbacks , server_changed_cb );
288
+ mongoc_apm_set_command_succeeded_cb (callbacks , succeeded_cb );
289
+ mongoc_apm_set_command_failed_cb (callbacks , failed_cb );
290
+ mongoc_client_pool_set_apm_callbacks (pool , callbacks , ctx );
229
291
mongoc_apm_callbacks_destroy (callbacks );
230
292
}
231
293
0 commit comments