1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
#include <mongoc.h>
static bool gExpectingFailure;
static bool gShutdown;
static void
query_collection (mongoc_collection_t *col)
{
mongoc_cursor_t *cursor;
const bson_t *doc;
bson_error_t error;
bson_t q;
mongoc_init ();
bson_init(&q);
bson_append_utf8(&q, "hello", -1, "world", -1);
cursor = mongoc_collection_find(col,
MONGOC_QUERY_NONE,
0,
0,
0,
&q,
NULL,
NULL);
while (mongoc_cursor_next(cursor, &doc)) {
char *str;
str = bson_as_json(doc, NULL);
fprintf(stderr, "%s\n", str);
bson_free(str);
}
if (mongoc_cursor_error(cursor, &error)) {
if (gExpectingFailure) {
if ((error.domain != MONGOC_ERROR_STREAM) ||
(error.code != MONGOC_ERROR_STREAM_SOCKET)) {
abort();
}
gExpectingFailure = false;
} else {
fprintf (stderr, "%s", error.message);
abort();
}
}
bson_destroy(&q);
}
static void
test_secondary (mongoc_client_t *client)
{
mongoc_collection_t *col;
col = mongoc_client_get_collection(client, "test", "test");
while (!gShutdown) {
query_collection(col);
}
mongoc_collection_destroy(col);
}
int
main (int argc,
char *argv[])
{
mongoc_read_prefs_t *read_prefs;
mongoc_client_t *client;
mongoc_uri_t *uri;
if (argc < 2) {
fprintf(stderr, "usage: %s mongodb://...\n", argv[0]);
return EXIT_FAILURE;
}
uri = mongoc_uri_new(argv[1]);
if (!uri) {
fprintf(stderr, "Invalid URI: \"%s\"\n", argv[1]);
return EXIT_FAILURE;
}
client = mongoc_client_new_from_uri(uri);
read_prefs = mongoc_read_prefs_new(MONGOC_READ_SECONDARY);
mongoc_client_set_read_prefs(client, read_prefs);
mongoc_read_prefs_destroy(read_prefs);
test_secondary(client);
mongoc_client_destroy(client);
mongoc_uri_destroy(uri);
return EXIT_SUCCESS;
}
|