Skip to content

Commit eaaf4b0

Browse files
author
Christian Hergert
committed
tests: compartmentalize tests and allow parallelization.
1 parent 1e5b4ef commit eaaf4b0

File tree

4 files changed

+139
-20
lines changed

4 files changed

+139
-20
lines changed

tests/test-libmongoc.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#include <stdio.h>
2222

2323
#include "TestSuite.h"
24+
#include "test-libmongoc.h"
25+
#include "mongoc-tests.h"
2426

2527

2628
extern void test_array_install (TestSuite *suite);
@@ -58,7 +60,8 @@ log_handler (mongoc_log_level_t log_level,
5860
}
5961

6062

61-
char MONGOC_TEST_HOST[1024];
63+
char MONGOC_TEST_HOST [1024];
64+
char MONGOC_TEST_UNIQUE [32];
6265

6366
static void
6467
set_mongoc_test_host(void)
@@ -86,7 +89,11 @@ main (int argc,
8689
TestSuite suite;
8790
int ret;
8891

89-
mongoc_init();
92+
mongoc_init ();
93+
94+
bson_snprintf (MONGOC_TEST_UNIQUE, sizeof MONGOC_TEST_UNIQUE,
95+
"test_%u_%u", (unsigned)time (NULL),
96+
(unsigned)gettestpid ());
9097

9198
set_mongoc_test_host ();
9299

tests/test-libmongoc.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#ifndef TEST_LIBMONGOC_H
1919
#define TEST_LIBMONGOC_H
2020

21-
extern char MONGOC_TEST_HOST[1024];
21+
extern char MONGOC_TEST_HOST [1024];
22+
extern char MONGOC_TEST_UNIQUE [32];
2223

2324
#endif

tests/test-mongoc-collection.c

Lines changed: 110 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
1+
#include <bcon.h>
12
#include <mongoc.h>
23

34
#include "TestSuite.h"
45

56
#include "test-libmongoc.h"
7+
#include "mongoc-tests.h"
68

79
static char *gTestUri;
810

911

12+
static mongoc_database_t *
13+
get_test_database (mongoc_client_t *client)
14+
{
15+
return mongoc_client_get_database (client, MONGOC_TEST_UNIQUE);
16+
}
17+
18+
1019
static void
1120
test_insert (void)
1221
{
22+
mongoc_database_t *database;
1323
mongoc_collection_t *collection;
1424
mongoc_client_t *client;
1525
bson_context_t *context;
@@ -19,10 +29,14 @@ test_insert (void)
1929
unsigned i;
2030
bson_t b;
2131

32+
2233
client = mongoc_client_new(gTestUri);
2334
ASSERT (client);
2435

25-
collection = mongoc_client_get_collection(client, "test", "test");
36+
database = get_test_database (client);
37+
ASSERT (database);
38+
39+
collection = mongoc_database_get_collection (database, "test_insert");
2640
ASSERT (collection);
2741

2842
mongoc_collection_drop(collection, &error);
@@ -53,7 +67,11 @@ test_insert (void)
5367
ASSERT (error.code == MONGOC_ERROR_BSON_INVALID);
5468
bson_destroy (&b);
5569

70+
r = mongoc_collection_drop (collection, &error);
71+
ASSERT (r);
72+
5673
mongoc_collection_destroy(collection);
74+
mongoc_database_destroy(database);
5775
bson_context_destroy(context);
5876
mongoc_client_destroy(client);
5977
}
@@ -63,6 +81,7 @@ static void
6381
test_insert_bulk (void)
6482
{
6583
mongoc_collection_t *collection;
84+
mongoc_database_t *database;
6685
mongoc_client_t *client;
6786
bson_context_t *context;
6887
bson_error_t error;
@@ -77,7 +96,10 @@ test_insert_bulk (void)
7796
client = mongoc_client_new(gTestUri);
7897
ASSERT (client);
7998

80-
collection = mongoc_client_get_collection(client, "test", "test");
99+
database = get_test_database (client);
100+
ASSERT (database);
101+
102+
collection = mongoc_database_get_collection(database, "test_insert_bulk");
81103
ASSERT (collection);
82104

83105
mongoc_collection_drop(collection, &error);
@@ -138,7 +160,11 @@ test_insert_bulk (void)
138160
bson_destroy(&b[i]);
139161
}
140162

163+
r = mongoc_collection_drop (collection, &error);
164+
ASSERT (r);
165+
141166
mongoc_collection_destroy(collection);
167+
mongoc_database_destroy(database);
142168
bson_context_destroy(context);
143169
mongoc_client_destroy(client);
144170
}
@@ -148,6 +174,7 @@ static void
148174
test_save (void)
149175
{
150176
mongoc_collection_t *collection;
177+
mongoc_database_t *database;
151178
mongoc_client_t *client;
152179
bson_context_t *context;
153180
bson_error_t error;
@@ -159,7 +186,10 @@ test_save (void)
159186
client = mongoc_client_new(gTestUri);
160187
ASSERT (client);
161188

162-
collection = mongoc_client_get_collection(client, "test", "test");
189+
database = get_test_database (client);
190+
ASSERT (database);
191+
192+
collection = mongoc_database_get_collection (database, "test_save");
163193
ASSERT (collection);
164194

165195
mongoc_collection_drop(collection, &error);
@@ -182,7 +212,11 @@ test_save (void)
182212

183213
bson_destroy (&b);
184214

215+
r = mongoc_collection_drop (collection, &error);
216+
ASSERT (r);
217+
185218
mongoc_collection_destroy(collection);
219+
mongoc_database_destroy(database);
186220
bson_context_destroy(context);
187221
mongoc_client_destroy(client);
188222
}
@@ -192,18 +226,32 @@ static void
192226
test_regex (void)
193227
{
194228
mongoc_collection_t *collection;
229+
mongoc_database_t *database;
230+
mongoc_write_concern_t *wr;
195231
mongoc_client_t *client;
196232
bson_error_t error = { 0 };
197233
int64_t count;
198234
bson_t q = BSON_INITIALIZER;
235+
bson_t *doc;
236+
bool r;
199237

200238
client = mongoc_client_new (gTestUri);
201239
ASSERT (client);
202240

203-
collection = mongoc_client_get_collection (client, "test", "test");
241+
database = get_test_database (client);
242+
ASSERT (database);
243+
244+
collection = mongoc_database_get_collection (database, "test_regex");
204245
ASSERT (collection);
205246

206-
bson_append_regex (&q, "hello", -1, "^/wo", NULL);
247+
wr = mongoc_write_concern_new ();
248+
mongoc_write_concern_set_journal (wr, true);
249+
250+
doc = BCON_NEW ("hello", "/world");
251+
r = mongoc_collection_insert (collection, MONGOC_INSERT_NONE, doc, wr, &error);
252+
ASSERT (r);
253+
254+
BSON_APPEND_REGEX (&q, "hello", "^/wo", "i");
207255

208256
count = mongoc_collection_count (collection,
209257
MONGOC_QUERY_NONE,
@@ -216,8 +264,15 @@ test_regex (void)
216264
ASSERT (count > 0);
217265
ASSERT (!error.domain);
218266

267+
r = mongoc_collection_drop (collection, &error);
268+
ASSERT (r);
269+
270+
mongoc_write_concern_destroy (wr);
271+
219272
bson_destroy (&q);
273+
bson_destroy (doc);
220274
mongoc_collection_destroy (collection);
275+
mongoc_database_destroy(database);
221276
mongoc_client_destroy (client);
222277
}
223278

@@ -226,6 +281,7 @@ static void
226281
test_update (void)
227282
{
228283
mongoc_collection_t *collection;
284+
mongoc_database_t *database;
229285
mongoc_client_t *client;
230286
bson_context_t *context;
231287
bson_error_t error;
@@ -240,7 +296,10 @@ test_update (void)
240296
client = mongoc_client_new(gTestUri);
241297
ASSERT (client);
242298

243-
collection = mongoc_client_get_collection(client, "test", "test");
299+
database = get_test_database (client);
300+
ASSERT (database);
301+
302+
collection = mongoc_database_get_collection (database, "test_update");
244303
ASSERT (collection);
245304

246305
context = bson_context_new(BSON_CONTEXT_NONE);
@@ -301,7 +360,11 @@ test_update (void)
301360
bson_destroy(&q);
302361
bson_destroy(&u);
303362

363+
r = mongoc_collection_drop (collection, &error);
364+
ASSERT (r);
365+
304366
mongoc_collection_destroy(collection);
367+
mongoc_database_destroy(database);
305368
bson_context_destroy(context);
306369
mongoc_client_destroy(client);
307370
}
@@ -311,6 +374,7 @@ static void
311374
test_delete (void)
312375
{
313376
mongoc_collection_t *collection;
377+
mongoc_database_t *database;
314378
mongoc_client_t *client;
315379
bson_context_t *context;
316380
bson_error_t error;
@@ -322,7 +386,10 @@ test_delete (void)
322386
client = mongoc_client_new(gTestUri);
323387
ASSERT (client);
324388

325-
collection = mongoc_client_get_collection(client, "test", "test");
389+
database = get_test_database (client);
390+
ASSERT (database);
391+
392+
collection = mongoc_database_get_collection (database, "test_delete");
326393
ASSERT (collection);
327394

328395
context = bson_context_new(BSON_CONTEXT_NONE);
@@ -352,7 +419,11 @@ test_delete (void)
352419
bson_destroy(&b);
353420
}
354421

422+
r = mongoc_collection_drop (collection, &error);
423+
ASSERT (r);
424+
355425
mongoc_collection_destroy(collection);
426+
mongoc_database_destroy(database);
356427
bson_context_destroy(context);
357428
mongoc_client_destroy(client);
358429
}
@@ -361,6 +432,7 @@ static void
361432
test_index (void)
362433
{
363434
mongoc_collection_t *collection;
435+
mongoc_database_t *database;
364436
mongoc_client_t *client;
365437
mongoc_index_opt_t opt;
366438
bson_error_t error;
@@ -372,7 +444,10 @@ test_index (void)
372444
client = mongoc_client_new(gTestUri);
373445
ASSERT (client);
374446

375-
collection = mongoc_client_get_collection(client, "test", "test");
447+
database = get_test_database (client);
448+
ASSERT (database);
449+
450+
collection = mongoc_database_get_collection (database, "test_index");
376451
ASSERT (collection);
377452

378453
bson_init(&keys);
@@ -388,7 +463,11 @@ test_index (void)
388463

389464
bson_destroy(&keys);
390465

466+
r = mongoc_collection_drop (collection, &error);
467+
ASSERT (r);
468+
391469
mongoc_collection_destroy(collection);
470+
mongoc_database_destroy(database);
392471
mongoc_client_destroy(client);
393472
}
394473

@@ -427,23 +506,34 @@ static void
427506
test_drop (void)
428507
{
429508
mongoc_collection_t *collection;
509+
mongoc_database_t *database;
430510
mongoc_client_t *client;
431511
bson_error_t error;
512+
bson_t *doc;
432513
bool r;
433514

434515
client = mongoc_client_new(gTestUri);
435516
ASSERT (client);
436517

437-
collection = mongoc_client_get_collection(client, "test", "test");
518+
database = get_test_database (client);
519+
ASSERT (database);
520+
521+
collection = mongoc_database_get_collection (database, "test_drop");
438522
ASSERT (collection);
439523

524+
doc = BCON_NEW("hello", "world");
525+
r = mongoc_collection_insert(collection, MONGOC_INSERT_NONE, doc, NULL, &error);
526+
bson_destroy (doc);
527+
ASSERT (r);
528+
440529
r = mongoc_collection_drop(collection, &error);
441-
assert(r == true);
530+
ASSERT (r == true);
442531

443532
r = mongoc_collection_drop(collection, &error);
444-
assert(r == false);
533+
ASSERT (r == false);
445534

446535
mongoc_collection_destroy(collection);
536+
mongoc_database_destroy(database);
447537
mongoc_client_destroy(client);
448538
}
449539

@@ -452,6 +542,7 @@ static void
452542
test_aggregate (void)
453543
{
454544
mongoc_collection_t *collection;
545+
mongoc_database_t *database;
455546
mongoc_client_t *client;
456547
mongoc_cursor_t *cursor;
457548
const bson_t *doc;
@@ -474,7 +565,10 @@ test_aggregate (void)
474565
client = mongoc_client_new(gTestUri);
475566
ASSERT (client);
476567

477-
collection = mongoc_client_get_collection(client, "test", "test");
568+
database = get_test_database (client);
569+
ASSERT (database);
570+
571+
collection = mongoc_database_get_collection (database, "test_aggregate");
478572
ASSERT (collection);
479573

480574
mongoc_collection_drop(collection, &error);
@@ -506,8 +600,12 @@ test_aggregate (void)
506600
ASSERT (!r);
507601
ASSERT (!doc);
508602

603+
r = mongoc_collection_drop(collection, &error);
604+
ASSERT (r);
605+
509606
mongoc_cursor_destroy(cursor);
510607
mongoc_collection_destroy(collection);
608+
mongoc_database_destroy(database);
511609
mongoc_client_destroy(client);
512610
bson_destroy(&b);
513611
bson_destroy(&pipeline);

0 commit comments

Comments
 (0)