1 #include "test.h"
2 #include "mongo.h"
3 
4 #include <sys/socket.h>
5 #include "libmongo-private.h"
6 
7 void
8 test_mongo_sync_cmd_custom_net_secondary (void)
9 {
10   mongo_sync_connection *conn;
11   bson *cmd;
12   mongo_packet *p;
13 
14   skip (!config.secondary_host, 1,
15         "Secondary server not configured");
16 
17   conn = mongo_sync_connect (config.secondary_host, config.secondary_port,
18                              TRUE);
19   cmd = bson_build (BSON_TYPE_INT32, "getnonce", 1,
20                     BSON_TYPE_NONE);
21   bson_finish (cmd);
22 
23   p = mongo_sync_cmd_custom (conn, config.db, cmd);
24   ok (p != NULL,
25       "mongo_sync_cmd_custom() works on the secondary too");
26   mongo_wire_packet_free (p);
27 
28   bson_free (cmd);
29   mongo_sync_disconnect (conn);
30 
31   endskip;
32 }
33 
34 void
35 test_mongo_sync_cmd_custom_net (void)
36 {
37   mongo_sync_connection *conn;
38   bson *cmd;
39   mongo_packet *p;
40 
41   begin_network_tests (3);
42 
43   conn = mongo_sync_connect (config.primary_host, config.primary_port, TRUE);
44   mongo_sync_cmd_is_master (conn);
45   mongo_sync_conn_set_auto_reconnect (conn, TRUE);
46 
47   cmd = bson_build (BSON_TYPE_INT32, "getnonce", 1,
48                     BSON_TYPE_NONE);
49   bson_finish (cmd);
50 
51   p = mongo_sync_cmd_custom (conn, config.db, cmd);
52   ok (p != NULL,
53       "mongo_sync_cmd_custom() works");
54   mongo_wire_packet_free (p);
55 
56   shutdown (conn->super.fd, SHUT_RDWR);
57   sleep (3);
58 
59   p = mongo_sync_cmd_custom (conn, config.db, cmd);
60   ok (p != NULL,
61       "mongo_sync_cmd_custom() automatically reconnects");
62   mongo_wire_packet_free (p);
63 
64   bson_free (cmd);
65   mongo_sync_disconnect (conn);
66 
67   test_mongo_sync_cmd_custom_net_secondary ();
68 
69   end_network_tests ();
70 }
71 
72 void
73 test_mongo_sync_cmd_custom (void)
74 {
75   mongo_sync_connection *c;
76   bson *cmd;
77 
78   c = test_make_fake_sync_conn (-1, FALSE);
79   cmd = bson_new ();
80   bson_append_int32 (cmd, "getnonce", 1);
81   bson_finish (cmd);
82 
83   ok (mongo_sync_cmd_custom (NULL, "test", cmd) == NULL,
84       "mongo_sync_cmd_custom() fails with a NULL connection");
85   ok (mongo_sync_cmd_custom (c, NULL, cmd) == NULL,
86       "mongo_sync_cmd_custom() fails with a NULL namespace");
87 
88   ok (mongo_sync_cmd_custom (c, "test", cmd) == NULL,
89       "mongo_sync_cmd_custom() fails with a bogus FD");
90   mongo_sync_conn_set_slaveok (c, TRUE);
91   ok (mongo_sync_cmd_custom (c, "test", cmd) == NULL,
92       "mongo_sync_cmd_custom() fails with a bogus FD");
93 
94   bson_free (cmd);
95   mongo_sync_disconnect (c);
96 
97   test_mongo_sync_cmd_custom_net ();
98 }
99 
100 RUN_TEST (7, mongo_sync_cmd_custom);
101