static void test_bson_oid_init_with_threads (void) { bson_context_t *context; int i; { bson_context_flags_t flags = 0; bson_context_t *contexts[N_THREADS]; bson_thread_t threads[N_THREADS]; #if defined(__linux__) flags |= BSON_CONTEXT_USE_TASK_ID; #endif for (i = 0; i < N_THREADS; i++) { contexts[i] = bson_context_new(flags); bson_thread_create(&threads[i], NULL, oid_worker, contexts[i]); } for (i = 0; i < N_THREADS; i++) { bson_thread_join(threads[i], NULL); } for (i = 0; i < N_THREADS; i++) { bson_context_destroy(contexts[i]); } } /* * Test threaded generation of oids using a single context; */ { bson_thread_t threads[N_THREADS]; context = bson_context_new(BSON_CONTEXT_THREAD_SAFE); for (i = 0; i < N_THREADS; i++) { bson_thread_create(&threads[i], NULL, oid_worker, context); } for (i = 0; i < N_THREADS; i++) { bson_thread_join(threads[i], NULL); } bson_context_destroy(context); } }
static void test_bson_oid_init_from_string (void) { bson_context_t *context; bson_oid_t oid; char str[25]; int i; context = bson_context_new(BSON_CONTEXT_NONE); /* * Test successfully parsed oids. */ for (i = 0; gTestOids[i]; i++) { bson_oid_init_from_string(&oid, gTestOids[i]); bson_oid_to_string(&oid, str); assert(!strcmp(str, gTestOids[i])); } /* * Test the failures. */ for (i = 0; gTestOidsFail[i]; i++) { bson_oid_init_from_string(&oid, gTestOidsFail[i]); bson_oid_to_string(&oid, str); assert(strcmp(str, gTestOidsFail[i])); } bson_context_destroy(context); }
static void test_bson_oid_init_from_string (void) { bson_context_t *context; bson_oid_t oid; char str[25]; int i; context = bson_context_new (BSON_CONTEXT_NONE); /* * Test successfully parsed oids. */ for (i = 0; gTestOids[i]; i++) { bson_oid_init_from_string (&oid, gTestOids[i]); bson_oid_to_string (&oid, str); BSON_ASSERT (!strcmp (str, gTestOids[i])); } /* * Test successfully parsed oids (case-insensitive). */ for (i = 0; gTestOidsCase[i]; i++) { char oid_lower[25]; int j; bson_oid_init_from_string (&oid, gTestOidsCase[i]); bson_oid_to_string (&oid, str); BSON_ASSERT (!bson_strcasecmp (str, gTestOidsCase[i])); for (j = 0; gTestOidsCase[i][j]; j++) { oid_lower[j] = tolower (gTestOidsCase[i][j]); } oid_lower[24] = '\0'; BSON_ASSERT (!strcmp (str, oid_lower)); } /* * Test that sizeof(str) works (len of 25 with \0 instead of 24). */ BSON_ASSERT (bson_oid_is_valid ("ffffffffffffffffffffffff", 24)); bson_oid_init_from_string (&oid, "ffffffffffffffffffffffff"); bson_oid_to_string (&oid, str); BSON_ASSERT (bson_oid_is_valid (str, sizeof str)); /* * Test the failures. */ for (i = 0; gTestOidsFail[i]; i++) { bson_oid_init_from_string (&oid, gTestOidsFail[i]); bson_oid_to_string (&oid, str); BSON_ASSERT (strcmp (str, gTestOidsFail[i])); } bson_context_destroy (context); }
static void test_insert (void) { mongoc_database_t *database; mongoc_collection_t *collection; mongoc_client_t *client; bson_context_t *context; bson_error_t error; bool r; bson_oid_t oid; unsigned i; bson_t b; client = mongoc_client_new(gTestUri); ASSERT (client); database = get_test_database (client); ASSERT (database); collection = get_test_collection (client, "test_insert"); ASSERT (collection); mongoc_collection_drop(collection, &error); context = bson_context_new(BSON_CONTEXT_NONE); ASSERT (context); for (i = 0; i < 10; i++) { bson_init(&b); bson_oid_init(&oid, context); bson_append_oid(&b, "_id", 3, &oid); bson_append_utf8(&b, "hello", 5, "/world", 5); r = mongoc_collection_insert(collection, MONGOC_INSERT_NONE, &b, NULL, &error); if (!r) { MONGOC_WARNING("%s\n", error.message); } ASSERT (r); bson_destroy(&b); } bson_init (&b); BSON_APPEND_INT32 (&b, "$hello", 1); r = mongoc_collection_insert (collection, MONGOC_INSERT_NONE, &b, NULL, &error); ASSERT (!r); ASSERT (error.domain == MONGOC_ERROR_BSON); ASSERT (error.code == MONGOC_ERROR_BSON_INVALID); bson_destroy (&b); r = mongoc_collection_drop (collection, &error); ASSERT (r); mongoc_collection_destroy(collection); mongoc_database_destroy(database); bson_context_destroy(context); mongoc_client_destroy(client); }
static void test_delete (void) { mongoc_collection_t *collection; mongoc_database_t *database; mongoc_client_t *client; bson_context_t *context; bson_error_t error; bool r; bson_oid_t oid; bson_t b; int i; client = mongoc_client_new(gTestUri); ASSERT (client); database = get_test_database (client); ASSERT (database); collection = get_test_collection (client, "test_delete"); ASSERT (collection); context = bson_context_new(BSON_CONTEXT_NONE); ASSERT (context); for (i = 0; i < 100; i++) { bson_init(&b); bson_oid_init(&oid, context); bson_append_oid(&b, "_id", 3, &oid); bson_append_utf8(&b, "hello", 5, "world", 5); r = mongoc_collection_insert(collection, MONGOC_INSERT_NONE, &b, NULL, &error); if (!r) { MONGOC_WARNING("%s\n", error.message); } ASSERT (r); bson_destroy(&b); bson_init(&b); bson_append_oid(&b, "_id", 3, &oid); r = mongoc_collection_delete(collection, MONGOC_DELETE_NONE, &b, NULL, &error); if (!r) { MONGOC_WARNING("%s\n", error.message); } ASSERT (r); bson_destroy(&b); } r = mongoc_collection_drop (collection, &error); ASSERT (r); mongoc_collection_destroy(collection); mongoc_database_destroy(database); bson_context_destroy(context); mongoc_client_destroy(client); }
static void test_bson_oid_get_time_t (void) { bson_context_t *context; bson_oid_t oid; bson_oid_t oid2; /* * Test that the bson time_t matches the current time. This can race, but * i dont think that matters much. */ context = bson_context_new(BSON_CONTEXT_NONE); bson_oid_init(&oid, context); bson_oid_init(&oid2, context); assert(bson_oid_get_time_t(&oid) == bson_oid_get_time_t(&oid2)); assert(time(NULL) == bson_oid_get_time_t(&oid2)); bson_context_destroy(context); }
static void test_bson_oid_init_sequence_with_tid (void) { bson_context_t *context; bson_oid_t oid; bson_oid_t oid2; int i; context = bson_context_new(BSON_CONTEXT_USE_TASK_ID); bson_oid_init_sequence(&oid, context); for (i = 0; i < 10000; i++) { bson_oid_init_sequence(&oid2, context); assert(FALSE == bson_oid_equal(&oid, &oid2)); assert(0 > bson_oid_compare(&oid, &oid2)); bson_oid_copy(&oid2, &oid); } bson_context_destroy(context); }
static void test_bson_oid_init_sequence_thread_safe (void) { bson_context_t *context; bson_oid_t oid; bson_oid_t oid2; int i; context = bson_context_new(BSON_CONTEXT_THREAD_SAFE); bson_oid_init_sequence(&oid, context); for (i = 0; i < 10000; i++) { bson_oid_init_sequence(&oid2, context); assert(FALSE == bson_oid_equal(&oid, &oid2)); assert(0 > bson_oid_compare(&oid, &oid2)); bson_oid_copy(&oid2, &oid); } bson_context_destroy(context); }
static void test_bson_oid_init_sequence (void) { bson_context_t *context; bson_oid_t oid; bson_oid_t oid2; int i; context = bson_context_new (BSON_CONTEXT_NONE); bson_oid_init_sequence (&oid, context); for (i = 0; i < 10000; i++) { bson_oid_init_sequence (&oid2, context); BSON_ASSERT (false == bson_oid_equal (&oid, &oid2)); BSON_ASSERT (0 > bson_oid_compare (&oid, &oid2)); bson_oid_copy (&oid2, &oid); } bson_context_destroy (context); }
static void test_save (void) { mongoc_collection_t *collection; mongoc_client_t *client; bson_context_t *context; bson_error_t error; bool r; bson_oid_t oid; unsigned i; bson_t b; client = mongoc_client_new(gTestUri); ASSERT (client); collection = mongoc_client_get_collection(client, "test", "test"); ASSERT (collection); mongoc_collection_drop(collection, &error); context = bson_context_new(BSON_CONTEXT_NONE); ASSERT (context); for (i = 0; i < 10; i++) { bson_init(&b); bson_oid_init(&oid, context); bson_append_oid(&b, "_id", 3, &oid); bson_append_utf8(&b, "hello", 5, "/world", 5); r = mongoc_collection_save(collection, &b, NULL, &error); if (!r) { MONGOC_WARNING("%s\n", error.message); } ASSERT (r); bson_destroy(&b); } bson_destroy (&b); mongoc_collection_destroy(collection); bson_context_destroy(context); mongoc_client_destroy(client); }
static void test_bson_oid_init (void) { bson_context_t *context; bson_oid_t oid; bson_oid_t oid2; int i; context = bson_context_new(BSON_CONTEXT_NONE); bson_oid_init(&oid, context); for (i = 0; i < 10000; i++) { bson_oid_init(&oid2, context); assert(FALSE == bson_oid_equal(&oid, &oid2)); assert(0 > bson_oid_compare(&oid, &oid2)); bson_oid_copy(&oid2, &oid); } bson_context_destroy(context); /* * Test that the shared context works. */ bson_oid_init(&oid, NULL); BSON_ASSERT(bson_context_get_default()); }
static void test_insert_bulk (void) { mongoc_collection_t *collection; mongoc_client_t *client; bson_context_t *context; bson_error_t error; bool r; bson_oid_t oid; unsigned i; bson_t q; bson_t b[10]; bson_t *bptr[10]; int64_t count; client = mongoc_client_new(gTestUri); ASSERT (client); collection = mongoc_client_get_collection(client, "test", "test"); ASSERT (collection); mongoc_collection_drop(collection, &error); context = bson_context_new(BSON_CONTEXT_NONE); ASSERT (context); bson_init(&q); bson_append_int32(&q, "n", -1, 0); for (i = 0; i < 10; i++) { bson_init(&b[i]); bson_oid_init(&oid, context); bson_append_oid(&b[i], "_id", -1, &oid); bson_append_int32(&b[i], "n", -1, i % 2); bptr[i] = &b[i]; } r = mongoc_collection_insert_bulk (collection, MONGOC_INSERT_NONE, (const bson_t **)bptr, 10, NULL, &error); if (!r) { MONGOC_WARNING("%s\n", error.message); } ASSERT (r); count = mongoc_collection_count (collection, MONGOC_QUERY_NONE, &q, 0, 0, NULL, &error); ASSERT (count == 5); for (i = 8; i < 10; i++) { bson_destroy(&b[i]); bson_init(&b[i]); bson_oid_init(&oid, context); bson_append_oid(&b[i], "_id", -1, &oid); bson_append_int32(&b[i], "n", -1, i % 2); bptr[i] = &b[i]; } r = mongoc_collection_insert_bulk (collection, MONGOC_INSERT_NONE, (const bson_t **)bptr, 10, NULL, &error); ASSERT (!r); ASSERT (error.code == 11000); count = mongoc_collection_count (collection, MONGOC_QUERY_NONE, &q, 0, 0, NULL, &error); ASSERT (count == 5); r = mongoc_collection_insert_bulk (collection, MONGOC_INSERT_CONTINUE_ON_ERROR, (const bson_t **)bptr, 10, NULL, &error); ASSERT (!r); ASSERT (error.code == 11000); count = mongoc_collection_count (collection, MONGOC_QUERY_NONE, &q, 0, 0, NULL, &error); ASSERT (count == 6); bson_destroy(&q); for (i = 0; i < 10; i++) { bson_destroy(&b[i]); } mongoc_collection_destroy(collection); bson_context_destroy(context); mongoc_client_destroy(client); }
static void test_update (void) { mongoc_collection_t *collection; mongoc_client_t *client; bson_context_t *context; bson_error_t error; bool r; bson_oid_t oid; unsigned i; bson_t b; bson_t q; bson_t u; bson_t set; client = mongoc_client_new(gTestUri); ASSERT (client); collection = mongoc_client_get_collection(client, "test", "test"); ASSERT (collection); context = bson_context_new(BSON_CONTEXT_NONE); ASSERT (context); for (i = 0; i < 10; i++) { bson_init(&b); bson_oid_init(&oid, context); bson_append_oid(&b, "_id", 3, &oid); bson_append_utf8(&b, "utf8", 4, "utf8 string", 11); bson_append_int32(&b, "int32", 5, 1234); bson_append_int64(&b, "int64", 5, 12345678); bson_append_bool(&b, "bool", 4, 1); r = mongoc_collection_insert(collection, MONGOC_INSERT_NONE, &b, NULL, &error); if (!r) { MONGOC_WARNING("%s\n", error.message); } ASSERT (r); bson_init(&q); bson_append_oid(&q, "_id", 3, &oid); bson_init(&u); bson_append_document_begin(&u, "$set", 4, &set); bson_append_utf8(&set, "utf8", 4, "updated", 7); bson_append_document_end(&u, &set); r = mongoc_collection_update(collection, MONGOC_UPDATE_NONE, &q, &u, NULL, &error); if (!r) { MONGOC_WARNING("%s\n", error.message); } ASSERT (r); bson_destroy(&b); bson_destroy(&q); bson_destroy(&u); } bson_init(&q); bson_init(&u); BSON_APPEND_INT32 (&u, "abcd", 1); BSON_APPEND_INT32 (&u, "$hi", 1); r = mongoc_collection_update(collection, MONGOC_UPDATE_NONE, &q, &u, NULL, &error); ASSERT (!r); ASSERT (error.domain == MONGOC_ERROR_BSON); ASSERT (error.code == MONGOC_ERROR_BSON_INVALID); bson_destroy(&q); bson_destroy(&u); bson_init(&q); bson_init(&u); BSON_APPEND_INT32 (&u, "a.b.c.d", 1); r = mongoc_collection_update(collection, MONGOC_UPDATE_NONE, &q, &u, NULL, &error); ASSERT (!r); ASSERT (error.domain == MONGOC_ERROR_BSON); ASSERT (error.code == MONGOC_ERROR_BSON_INVALID); bson_destroy(&q); bson_destroy(&u); mongoc_collection_destroy(collection); bson_context_destroy(context); mongoc_client_destroy(client); }