TestZookeeperInit.cc 9.64 KB
Newer Older
1
2
3
4
5
6
7
8
/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
Andrew Kornev's avatar
Andrew Kornev committed
9
10
11
12
13
14
15
16
17
18
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

Andrew Kornev's avatar
Andrew Kornev committed
19
#include <cppunit/extensions/HelperMacros.h>
20
#include <sys/types.h>
Andrew Kornev's avatar
Andrew Kornev committed
21
22
23
24
#include <netinet/in.h>
#include <errno.h>

#include "Util.h"
25
#include "LibCMocks.h"
Andrew Kornev's avatar
Andrew Kornev committed
26
#include "ZKMocks.h"
27

Andrew Kornev's avatar
Andrew Kornev committed
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#ifdef THREADED
#include "PthreadMocks.h"
#else
class MockPthreadsNull;
#endif

using namespace std;

class Zookeeper_init : public CPPUNIT_NS::TestFixture
{
	CPPUNIT_TEST_SUITE(Zookeeper_init);
	CPPUNIT_TEST(testBasic);
    CPPUNIT_TEST(testAddressResolution);
    CPPUNIT_TEST(testMultipleAddressResolution);
42
43
44
45
    CPPUNIT_TEST(testNullAddressString);
    CPPUNIT_TEST(testEmptyAddressString);
    CPPUNIT_TEST(testOneSpaceAddressString);
    CPPUNIT_TEST(testTwoSpacesAddressString);
Andrew Kornev's avatar
Andrew Kornev committed
46
47
48
49
50
51
52
53
54
55
    CPPUNIT_TEST(testInvalidAddressString1);
    CPPUNIT_TEST(testInvalidAddressString2);
    CPPUNIT_TEST(testNonexistentHost);
    CPPUNIT_TEST(testOutOfMemory_init);
    CPPUNIT_TEST(testOutOfMemory_getaddrs1);
    CPPUNIT_TEST(testOutOfMemory_getaddrs2);
    CPPUNIT_TEST(testPermuteAddrsList);
	CPPUNIT_TEST_SUITE_END();
    zhandle_t *zh;
    MockPthreadsNull* pthreadMock;   
56
    static void watcher(zhandle_t *, int , int , const char *,void*){}
Andrew Kornev's avatar
Andrew Kornev committed
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
public: 
    Zookeeper_init():zh(0),pthreadMock(0){}
    
    void setUp()
    {
    	zoo_set_debug_level((ZooLogLevel)0); // disable logging
        zoo_deterministic_conn_order(0);
#ifdef THREADED
        // disable threading
        pthreadMock=new MockPthreadZKNull;
#endif        
        zh=0;
    }
    
    void tearDown()
    {
        zookeeper_close(zh);
#ifdef THREADED
        delete pthreadMock;
#endif
    }

    void testBasic()
    {
    	const string EXPECTED_HOST("localhost:2121");
    	const int EXPECTED_ADDRS_COUNT =1;
    	const int EXPECTED_RECV_TIMEOUT=10000;
        clientid_t cid;
    	memset(&cid,0xFE,sizeof(cid));
        
    	zh=zookeeper_init(EXPECTED_HOST.c_str(),watcher,EXPECTED_RECV_TIMEOUT,
    	        &cid,(void*)1,0);
        
        CPPUNIT_ASSERT(zh!=0);
    	CPPUNIT_ASSERT(zh->fd == -1);
    	CPPUNIT_ASSERT(zh->hostname!=0);
    	CPPUNIT_ASSERT_EQUAL(EXPECTED_ADDRS_COUNT,zh->addrs_count);
    	CPPUNIT_ASSERT_EQUAL(EXPECTED_HOST,string(zh->hostname));
        CPPUNIT_ASSERT(zh->state == 0);
        CPPUNIT_ASSERT(zh->context == (void*)1);
        CPPUNIT_ASSERT_EQUAL(EXPECTED_RECV_TIMEOUT,zh->recv_timeout);
        CPPUNIT_ASSERT(zh->watcher == watcher);
        CPPUNIT_ASSERT(zh->connect_index==0);
        CPPUNIT_ASSERT(zh->primer_buffer.buffer==zh->primer_storage_buffer);
        CPPUNIT_ASSERT(zh->primer_buffer.curr_offset ==0);
        CPPUNIT_ASSERT(zh->primer_buffer.len == sizeof(zh->primer_storage_buffer));
        CPPUNIT_ASSERT(zh->primer_buffer.next == 0);
        CPPUNIT_ASSERT(zh->last_zxid ==0);
        CPPUNIT_ASSERT(memcmp(&zh->client_id,&cid,sizeof(cid))==0);

#ifdef THREADED
        // thread specific checks
        adaptor_threads* adaptor=(adaptor_threads*)zh->adaptor_priv;
        CPPUNIT_ASSERT(adaptor!=0);
        CPPUNIT_ASSERT(pthreadMock->pthread_createCounter==2);
        CPPUNIT_ASSERT(MockPthreadsNull::isInitialized(adaptor->io));
        CPPUNIT_ASSERT(MockPthreadsNull::isInitialized(adaptor->completion));
        CPPUNIT_ASSERT(MockPthreadsNull::isInitialized(&zh->to_process.lock));
        CPPUNIT_ASSERT(MockPthreadsNull::isInitialized(&zh->to_send.lock));
        CPPUNIT_ASSERT(MockPthreadsNull::isInitialized(&zh->sent_requests.lock));
        CPPUNIT_ASSERT(MockPthreadsNull::isInitialized(&zh->completions_to_process.lock));
        CPPUNIT_ASSERT(MockPthreadsNull::isInitialized(&zh->sent_requests.cond));
        CPPUNIT_ASSERT(MockPthreadsNull::isInitialized(&zh->completions_to_process.cond));
#endif
    }
    void testAddressResolution()
    {
124
        const char EXPECTED_IPS[][4]={{127,0,0,1}};
Andrew Kornev's avatar
Andrew Kornev committed
125
126
127
        const int EXPECTED_ADDRS_COUNT =COUNTOF(EXPECTED_IPS);

        zoo_deterministic_conn_order(1);
128
        zh=zookeeper_init("127.0.0.1:2121",0,10000,0,0,0);
Andrew Kornev's avatar
Andrew Kornev committed
129
130
131
132
133
134
135
136
137
138
139
        
        CPPUNIT_ASSERT(zh!=0);
        CPPUNIT_ASSERT_EQUAL(EXPECTED_ADDRS_COUNT,zh->addrs_count);
        for(int i=0;i<zh->addrs_count;i++){
            sockaddr_in* addr=(struct sockaddr_in*)&zh->addrs[i];
            CPPUNIT_ASSERT(memcmp(EXPECTED_IPS[i],&addr->sin_addr,sizeof(addr->sin_addr))==0);
            CPPUNIT_ASSERT_EQUAL(2121,(int)ntohs(addr->sin_port));
        }
    }
    void testMultipleAddressResolution()
    {
140
141
        const string EXPECTED_HOST("127.0.0.1:2121,127.0.0.2:3434");
        const char EXPECTED_IPS[][4]={{127,0,0,1},{127,0,0,2}};
Andrew Kornev's avatar
Andrew Kornev committed
142
143
144
145
146
147
148
149
150
151
152
        const int EXPECTED_ADDRS_COUNT =COUNTOF(EXPECTED_IPS);

        zoo_deterministic_conn_order(1);
        zh=zookeeper_init(EXPECTED_HOST.c_str(),0,1000,0,0,0);

        CPPUNIT_ASSERT(zh!=0);
        CPPUNIT_ASSERT_EQUAL(EXPECTED_ADDRS_COUNT,zh->addrs_count);

        for(int i=0;i<zh->addrs_count;i++){
            sockaddr_in* addr=(struct sockaddr_in*)&zh->addrs[i];
            CPPUNIT_ASSERT(memcmp(EXPECTED_IPS[i],&addr->sin_addr,sizeof(addr->sin_addr))==0);
153
            if(i<1)
Andrew Kornev's avatar
Andrew Kornev committed
154
155
156
157
158
                CPPUNIT_ASSERT_EQUAL(2121,(int)ntohs(addr->sin_port));
            else
                CPPUNIT_ASSERT_EQUAL(3434,(int)ntohs(addr->sin_port));
        }
    }
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
    void testNullAddressString()
    {
        zh=zookeeper_init(NULL,0,0,0,0,0);
        CPPUNIT_ASSERT(zh==0);
        CPPUNIT_ASSERT_EQUAL(EINVAL,errno);
    }
    void testEmptyAddressString()
    {
        const string INVALID_HOST("");
        zh=zookeeper_init(INVALID_HOST.c_str(),0,0,0,0,0);
        CPPUNIT_ASSERT(zh==0);
        CPPUNIT_ASSERT_EQUAL(EINVAL,errno);
    }
    void testOneSpaceAddressString()
    {
        const string INVALID_HOST(" ");
        zh=zookeeper_init(INVALID_HOST.c_str(),0,0,0,0,0);
        CPPUNIT_ASSERT(zh==0);
        CPPUNIT_ASSERT_EQUAL(EINVAL,errno);
    }
    void testTwoSpacesAddressString()
    {
        const string INVALID_HOST("  ");
        zh=zookeeper_init(INVALID_HOST.c_str(),0,0,0,0,0);
        CPPUNIT_ASSERT(zh==0);
        CPPUNIT_ASSERT_EQUAL(EINVAL,errno);
    }
Andrew Kornev's avatar
Andrew Kornev committed
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
    void testInvalidAddressString1()
    {
        const string INVALID_HOST("host1");
        zh=zookeeper_init(INVALID_HOST.c_str(),0,0,0,0,0);
        CPPUNIT_ASSERT(zh==0);
        CPPUNIT_ASSERT_EQUAL(EINVAL,errno);
    }
    void testInvalidAddressString2()
    {
        const string INVALID_HOST("host1:1111+host:123");
        zh=zookeeper_init(INVALID_HOST.c_str(),0,0,0,0,0);
        CPPUNIT_ASSERT(zh==0);
        CPPUNIT_ASSERT_EQUAL(EINVAL,errno);
    }
    void testNonexistentHost()
    {
202
        const string EXPECTED_HOST("host1.blabadibla.bla.:1111");
Andrew Kornev's avatar
Andrew Kornev committed
203
204
205
206
        
        zh=zookeeper_init(EXPECTED_HOST.c_str(),0,0,0,0,0);
        
        CPPUNIT_ASSERT(zh==0);
207
208
209
210
        //With the switch to thread safe getaddrinfo, we don't get
        //these global variables
        //CPPUNIT_ASSERT_EQUAL(EINVAL,errno);
        //CPPUNIT_ASSERT_EQUAL(HOST_NOT_FOUND,h_errno);
Andrew Kornev's avatar
Andrew Kornev committed
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
    }
    void testOutOfMemory_init()
    {
        Mock_calloc mock;
        mock.callsBeforeFailure=0; // fail first calloc in init()
        
        zh=zookeeper_init("ahost:123",watcher,10000,0,0,0);
        
        CPPUNIT_ASSERT(zh==0);
        CPPUNIT_ASSERT_EQUAL(ENOMEM,errno);
    }
    void testOutOfMemory_getaddrs1()
    {
        Mock_realloc reallocMock;
        reallocMock.callsBeforeFailure=0; // fail on first call to realloc

227
        zh=zookeeper_init("127.0.0.1:123",0,0,0,0,0);
Andrew Kornev's avatar
Andrew Kornev committed
228
229
230
231
232
233
234
235
236
        
        CPPUNIT_ASSERT(zh==0);
        CPPUNIT_ASSERT_EQUAL(ENOMEM,errno);
    }
    void testOutOfMemory_getaddrs2()
    {
        Mock_realloc reallocMock;
        reallocMock.callsBeforeFailure=1; // fail on the second call to realloc

237
        zh=zookeeper_init("127.0.0.1:123,127.0.0.2:123,127.0.0.3:123,127.0.0.4:123,127.0.0.5:123,127.0.0.6:123,127.0.0.7:123,127.0.0.8:123,127.0.0.9:123,127.0.0.10:123,127.0.0.11:123,127.0.0.12:123,127.0.0.13:123,127.0.0.14:123,127.0.0.15:123,127.0.0.16:123,127.0.0.17:123",0,0,0,0,0);
Andrew Kornev's avatar
Andrew Kornev committed
238
239
240
241
242
243
244
245
246
247
248
249
250
        
        CPPUNIT_ASSERT(zh==0);
        CPPUNIT_ASSERT_EQUAL(ENOMEM,errno);
    }
    void testPermuteAddrsList()
    {
        const char EXPECTED[][5]={"\0\0\0\0","\1\1\1\1","\2\2\2\2","\3\3\3\3"};
        const int EXPECTED_ADDR_COUNT=COUNTOF(EXPECTED);
        
        const int RAND_SEQ[]={0,1,2,3,1,3,2,0,-1};
        const int RAND_SIZE=COUNTOF(RAND_SEQ);
        Mock_random randomMock;
        randomMock.randomReturns.assign(RAND_SEQ,RAND_SEQ+RAND_SIZE-1);
251
        zh=zookeeper_init("0.0.0.0:123,1.1.1.1:123,2.2.2.2:123,3.3.3.3:123",0,1000,0,0,0);
Andrew Kornev's avatar
Andrew Kornev committed
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
        
        CPPUNIT_ASSERT(zh!=0);
        CPPUNIT_ASSERT_EQUAL(EXPECTED_ADDR_COUNT,zh->addrs_count);
        const string EXPECTED_SEQ("3210");
        char ACTUAL_SEQ[EXPECTED_ADDR_COUNT+1]; ACTUAL_SEQ[EXPECTED_ADDR_COUNT]=0;
        for(int i=0;i<zh->addrs_count;i++){
            sockaddr_in* addr=(struct sockaddr_in*)&zh->addrs[i];
            // match the first byte of the EXPECTED and of the actual address
            ACTUAL_SEQ[i]=((char*)&addr->sin_addr)[0]+'0';
        }
        CPPUNIT_ASSERT_EQUAL(EXPECTED_SEQ,string(ACTUAL_SEQ));
    }
};

CPPUNIT_TEST_SUITE_REGISTRATION(Zookeeper_init);