Skip to content

Commit b6b1225

Browse files
author
Christian Hergert
committed
tests: add basic write command test with splitting.
1 parent 07240c1 commit b6b1225

File tree

4 files changed

+115
-1
lines changed

4 files changed

+115
-1
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ set(test-libmongoc-sources
214214
${SOURCE_DIR}/tests/test-mongoc-stream.c
215215
${SOURCE_DIR}/tests/test-mongoc-uri.c
216216
${SOURCE_DIR}/tests/test-mongoc-write-concern.c
217+
${SOURCE_DIR}/tests/test-write-command.c
217218
${SOURCE_DIR}/tests/TestSuite.c
218219
)
219220

tests/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ test_libmongoc_SOURCES = \
8383
tests/test-mongoc-uri.c \
8484
tests/test-mongoc-write-concern.c \
8585
tests/test-libmongoc.h \
86+
tests/test-write-commands.c \
8687
tests/TestSuite.c \
8788
tests/TestSuite.h
8889
if ENABLE_SSL

tests/test-libmongoc.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ extern void test_read_prefs_install (TestSuite *suite);
4242
extern void test_rpc_install (TestSuite *suite);
4343
extern void test_stream_install (TestSuite *suite);
4444
extern void test_uri_install (TestSuite *suite);
45+
extern void test_write_command_install (TestSuite *suite);
4546
extern void test_write_concern_install (TestSuite *suite);
4647
#ifdef MONGOC_ENABLE_SSL
4748
extern void test_x509_install (TestSuite *suite);
@@ -127,10 +128,11 @@ main (int argc,
127128
TestSuite_Init (&suite, "", argc, argv);
128129

129130
test_array_install (&suite);
130-
test_bulk_install (&suite);
131131
test_buffer_install (&suite);
132132
test_client_install (&suite);
133133
test_client_pool_install (&suite);
134+
test_write_command_install (&suite);
135+
test_bulk_install (&suite);
134136
test_collection_install (&suite);
135137
test_cursor_install (&suite);
136138
test_database_install (&suite);

tests/test-write-commands.c

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
#include <bcon.h>
2+
#include <mongoc.h>
3+
4+
#include "mongoc-collection-private.h"
5+
#include "mongoc-write-command-private.h"
6+
7+
#include "TestSuite.h"
8+
9+
#include "test-libmongoc.h"
10+
#include "mongoc-tests.h"
11+
12+
static char *gTestUri;
13+
14+
15+
static mongoc_collection_t *
16+
get_test_collection (mongoc_client_t *client,
17+
const char *prefix)
18+
{
19+
mongoc_collection_t *ret;
20+
char *str;
21+
22+
str = gen_collection_name (prefix);
23+
ret = mongoc_client_get_collection (client, "test", str);
24+
bson_free (str);
25+
26+
return ret;
27+
}
28+
29+
30+
static void
31+
test_split_insert (void)
32+
{
33+
mongoc_write_command_t command;
34+
mongoc_write_result_t result;
35+
mongoc_collection_t *collection;
36+
mongoc_client_t *client;
37+
bson_oid_t oid;
38+
bson_t **docs;
39+
bson_t reply = BSON_INITIALIZER;
40+
bson_error_t error;
41+
const char *key;
42+
char str [12];
43+
int i;
44+
int j;
45+
bool r;
46+
47+
client = mongoc_client_new (gTestUri);
48+
assert (client);
49+
50+
collection = get_test_collection (client, "test_split_insert");
51+
assert (collection);
52+
53+
docs = bson_malloc (sizeof(bson_t*) * 3000);
54+
55+
for (i = 0; i < 3000; i++) {
56+
docs [i] = bson_new ();
57+
bson_oid_init (&oid, NULL);
58+
BSON_APPEND_OID (docs [i], "_id", &oid);
59+
for (j = 0; j < 20; j++) {
60+
bson_uint32_to_string (j, &key, str, sizeof str);
61+
BSON_APPEND_INT64 (docs [i], key, 1234);
62+
}
63+
}
64+
65+
_mongoc_write_result_init (&result);
66+
67+
_mongoc_write_command_init_insert (&command,
68+
(const bson_t * const *)docs,
69+
3000, true);
70+
71+
_mongoc_write_command_execute (&command, client, 0, collection->db,
72+
collection->collection, NULL, &result);
73+
74+
r = _mongoc_write_result_complete (&result, &reply, &error);
75+
76+
assert (r);
77+
assert (result.nInserted == 3000);
78+
79+
_mongoc_write_command_destroy (&command);
80+
_mongoc_write_result_destroy (&result);
81+
82+
r = mongoc_collection_drop (collection, &error);
83+
assert (r);
84+
85+
for (i = 0; i < 3000; i++) {
86+
bson_destroy (docs [i]);
87+
}
88+
89+
bson_free (docs);
90+
91+
mongoc_collection_destroy (collection);
92+
mongoc_client_destroy (client);
93+
}
94+
95+
96+
static void
97+
cleanup_globals (void)
98+
{
99+
bson_free (gTestUri);
100+
}
101+
102+
void
103+
test_write_command_install (TestSuite *suite)
104+
{
105+
gTestUri = bson_strdup_printf("mongodb://%s/", MONGOC_TEST_HOST);
106+
107+
TestSuite_Add (suite, "/WriteCommand/split_insert", test_split_insert);
108+
109+
atexit (cleanup_globals);
110+
}

0 commit comments

Comments
 (0)