summaryrefslogtreecommitdiff
path: root/tests/unit/mongo/sync/sync_cmd_query.c
blob: da7c69348f396886cda7b13362c33b2fae81d032 (plain)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include "test.h"
#include "mongo.h"

#include <errno.h>
#include <sys/socket.h>
#include "libmongo-private.h"

void
test_mongo_sync_cmd_query (void)
{
  mongo_packet *p;
  mongo_sync_connection *c;
  bson *q, *s;

  c = test_make_fake_sync_conn (-1, FALSE);
  q = test_bson_generate_full ();
  s = test_bson_generate_full ();

  ok (mongo_sync_cmd_query (NULL, "test.ns", 0, 0, 1, q, s) == NULL,
      "mongo_sync_cmd_query() fails with a NULL connection");
  ok (mongo_sync_cmd_query (c, NULL, 0, 0, 1, q, s) == NULL,
      "mongo_sync_cmd_query() fails with a NULL namespace");
  ok (mongo_sync_cmd_query (c, "test.ns", 0, 0, 1, NULL, s) == NULL,
      "mongo_sync_cmd_query() fails with a NULL query");

  ok (mongo_sync_cmd_query (c, "test.ns", 0, 0, 1, q, s) == NULL,
      "mongo_sync_cmd_query() fails with a bogus FD");
  mongo_sync_conn_set_slaveok (c, TRUE);
  ok (mongo_sync_cmd_query (c, "test.ns", 0, 0, 1, q, s) == NULL,
      "mongo_sync_cmd_query() fails with a bogus FD");

  mongo_sync_disconnect (c);

  bson_free (q);
  bson_free (s);

  begin_network_tests (7);

  q = bson_new ();
  bson_append_boolean (q, "sync_cmd_query_test", TRUE);
  bson_finish (q);

  s = bson_new ();
  bson_append_boolean (s, "sync_cmd_query_test", FALSE);
  bson_finish (s);

  c = mongo_sync_connect (config.primary_host, config.primary_port, TRUE);
  mongo_sync_conn_set_auto_reconnect (c, TRUE);
  mongo_sync_cmd_insert (c, config.ns, q, NULL);

  p = mongo_sync_cmd_query (c, config.ns, 0, 0, 1, q, NULL);
  ok (p != NULL,
      "mongo_sync_cmd_query() works");
  mongo_wire_packet_free (p);

  errno = 0;
  p = mongo_sync_cmd_query (c, config.ns, 0, 0, 1, s, NULL);
  ok (p == NULL && errno == ENOENT,
      "mongo_sync_cmd_query() sets errno to ENOENT when there's "
      "nothing to return");
  mongo_wire_packet_free (p);

  shutdown (c->super.fd, SHUT_RDWR);
  sleep (3);

  p = mongo_sync_cmd_query (c, config.ns, 0, 0, 1, q, NULL);
  ok (p != NULL,
      "mongo_sync_cmd_query() automatically reconnects");
  mongo_wire_packet_free (p);

  mongo_sync_disconnect (c);

  /*
   * Test request/response pairing, by sending a crafted query first,
   * and another, without reading the response for the first before
   * that.
   */
  c = mongo_sync_connect (config.primary_host, config.primary_port, TRUE);
  p = mongo_wire_cmd_query (12345, config.ns, MONGO_WIRE_FLAG_QUERY_SLAVE_OK,
                            0, 1, s, NULL);
  mongo_packet_send ((mongo_connection *)c, p);
  mongo_wire_packet_free (p);

  errno = 0;
  p = mongo_sync_cmd_query (c, config.ns, 0, 0, 1, s, NULL);
  ok (p == NULL && errno == EPROTO,
      "mongo_sync_cmd_query() fails if the reply is not a response to "
      "the current query");
  mongo_wire_packet_free (p);

  mongo_sync_disconnect (c);

  /*
   * Tests involving a secondary
   */
  skip (!config.secondary_host, 3, "Secondary host not set up");

  c = mongo_sync_connect (config.secondary_host, config.secondary_port, TRUE);
  mongo_sync_conn_set_auto_reconnect (c, TRUE);

  ok (c && mongo_sync_cmd_is_master (c) == FALSE,
      "Connected to a secondary");
  p = mongo_sync_cmd_query (c, config.ns, 0, 0, 1, q, NULL);
  ok (p != NULL,
      "mongo_sync_cmd_query() works on secondary");
  mongo_wire_packet_free (p);

  mongo_sync_conn_set_slaveok (c, FALSE);

  p = mongo_sync_cmd_query (c, config.ns, 0, 0, 1, q, NULL);
  ok (p != NULL && mongo_sync_cmd_is_master (c) == TRUE,
      "mongo_sync_cmd_query() can resync to master");
  mongo_wire_packet_free (p);

  mongo_sync_disconnect (c);

  endskip;

  bson_free (q);
  bson_free (s);

  end_network_tests ();
}

RUN_TEST (12, mongo_sync_cmd_query);