File: test-secondary.c

package info (click to toggle)
syslog-ng 3.8.1-10
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 47,320 kB
  • ctags: 43,937
  • sloc: ansic: 159,432; yacc: 25,059; sh: 13,574; makefile: 4,669; python: 3,468; java: 3,218; xml: 2,309; perl: 318; lex: 316; awk: 184
file content (97 lines) | stat: -rw-r--r-- 2,134 bytes parent folder | download | duplicates (2)
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;
}