/
replica_set_test.c
154 lines (117 loc) · 3.7 KB
/
replica_set_test.c
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/* test.c */
#include "test.h"
#include "mongo.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#ifndef SEED_START_PORT
#define SEED_START_PORT 30000
#endif
#ifndef REPLICA_SET_NAME
#define REPLICA_SET_NAME "replica-set-foo"
#endif
int test_connect( const char *set_name ) {
mongo conn[1];
int res;
INIT_SOCKETS_FOR_WINDOWS;
mongo_replset_init( conn, set_name );
mongo_replset_add_seed( conn, TEST_SERVER, SEED_START_PORT + 1 );
mongo_replset_add_seed( conn, TEST_SERVER, SEED_START_PORT );
res = mongo_replset_connect( conn );
if( res != MONGO_OK ) {
res = conn->err;
return res;
}
ASSERT( conn->primary->port == SEED_START_PORT ||
conn->primary->port == SEED_START_PORT + 1 ||
conn->primary->port == SEED_START_PORT + 2 );
mongo_destroy( conn );
return res;
}
int test_reconnect( const char *set_name ) {
mongo conn[1];
int res;
int e = 0;
bson b;
INIT_SOCKETS_FOR_WINDOWS;
mongo_replset_init( conn, set_name );
mongo_replset_add_seed( conn, TEST_SERVER, SEED_START_PORT );
mongo_replset_add_seed( conn, TEST_SERVER, SEED_START_PORT + 1 );
if( ( mongo_replset_connect( conn ) != MONGO_OK ) ) {
mongo_destroy( conn );
return res;
} else {
fprintf( stderr, "Disconnect now:\n" );
sleep( 10 );
e = 1;
do {
res = mongo_find_one( conn, "foo.bar", bson_empty( &b ), bson_empty( &b ), NULL );
if( res == MONGO_ERROR && conn->err == MONGO_IO_ERROR ) {
sleep( 2 );
if( e++ < 30 ) {
fprintf( stderr, "Attempting reconnect %d.\n", e );
mongo_reconnect( conn );
} else {
fprintf( stderr, "Fail.\n" );
return -1;
}
}
} while( 1 );
}
return 0;
}
int test_insert_limits( const char *set_name ) {
char version[10];
mongo conn[1];
mongo_write_concern wc[1];
int i;
char key[10];
int res = 0;
bson b[1], b2[1];
bson *objs[2];
mongo_write_concern_init( wc );
wc->w = 1;
mongo_write_concern_finish( wc );
/* We'll perform the full test if we're running v2.0 or later. */
if( mongo_get_server_version( version ) != -1 && version[0] <= '1' )
return 0;
mongo_replset_init( conn, set_name );
mongo_replset_add_seed( conn, TEST_SERVER, SEED_START_PORT + 1 );
mongo_replset_add_seed( conn, TEST_SERVER, SEED_START_PORT );
res = mongo_replset_connect( conn );
if( res != MONGO_OK ) {
res = conn->err;
return res;
}
ASSERT( conn->max_bson_size > MONGO_DEFAULT_MAX_BSON_SIZE );
bson_init( b );
for(i=0; i<1200000; i++) {
sprintf( key, "%d", i + 10000000 );
bson_append_int( b, key, i );
}
bson_finish( b );
ASSERT( bson_size( b ) > conn->max_bson_size );
ASSERT( mongo_insert( conn, "test.foo", b, wc ) == MONGO_ERROR );
ASSERT( conn->err == MONGO_BSON_TOO_LARGE );
mongo_clear_errors( conn );
ASSERT( conn->err == 0 );
bson_init( b2 );
bson_append_int( b2, "foo", 1 );
bson_finish( b2 );
objs[0] = b;
objs[1] = b2;
ASSERT( mongo_insert_batch( conn, "test.foo", (const bson**)objs, 2, wc, 0 ) == MONGO_ERROR );
ASSERT( conn->err == MONGO_BSON_TOO_LARGE );
mongo_write_concern_destroy( wc );
return 0;
}
int main() {
ASSERT( test_connect( REPLICA_SET_NAME ) == MONGO_OK );
ASSERT( test_connect( "test-foobar" ) == MONGO_CONN_BAD_SET_NAME );
ASSERT( test_insert_limits( REPLICA_SET_NAME ) == MONGO_OK );
/*
ASSERT( test_reconnect( "test-rs" ) == 0 );
*/
return 0;
}