TestClient.cc 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #include <cppunit/extensions/HelperMacros.h>
  19. #include "CppAssertHelper.h"
  20. #include <signal.h>
  21. #include <stdlib.h>
  22. #include <unistd.h>
  23. #include <sys/select.h>
  24. #include "CollectionUtil.h"
  25. #include "ThreadingUtil.h"
  26. using namespace Util;
  27. #include "Vector.h"
  28. using namespace std;
  29. #include <cstring>
  30. #include <list>
  31. #include <zookeeper.h>
  32. #include <errno.h>
  33. #include <recordio.h>
  34. #include "Util.h"
  35. #include "ZKMocks.h"
  36. struct buff_struct_2 {
  37. int32_t len;
  38. int32_t off;
  39. char *buffer;
  40. };
  41. // TODO(br33d): the vast majority of this test is not usable with single threaded.
  42. // it needs a overhaul to work properly with both threaded and single
  43. // threaded (ZOOKEEPER-2640)
  44. #ifdef THREADED
  45. // For testing LogMessage Callback functionality
  46. list<string> logMessages;
  47. void logMessageHandler(const char* message) {
  48. cout << "Log Message Received: [" << message << "]" << endl;
  49. logMessages.push_back(message);
  50. }
  51. static int Stat_eq(struct Stat* a, struct Stat* b)
  52. {
  53. if (a->czxid != b->czxid) return 0;
  54. if (a->mzxid != b->mzxid) return 0;
  55. if (a->ctime != b->ctime) return 0;
  56. if (a->mtime != b->mtime) return 0;
  57. if (a->version != b->version) return 0;
  58. if (a->cversion != b->cversion) return 0;
  59. if (a->aversion != b->aversion) return 0;
  60. if (a->ephemeralOwner != b->ephemeralOwner) return 0;
  61. if (a->dataLength != b->dataLength) return 0;
  62. if (a->numChildren != b->numChildren) return 0;
  63. if (a->pzxid != b->pzxid) return 0;
  64. return 1;
  65. }
  66. #ifdef THREADED
  67. static void yield(zhandle_t *zh, int i)
  68. {
  69. sleep(i);
  70. }
  71. #else
  72. static void yield(zhandle_t *zh, int seconds)
  73. {
  74. int fd;
  75. int interest;
  76. int events;
  77. struct timeval tv;
  78. int rc;
  79. time_t expires = time(0) + seconds;
  80. time_t timeLeft = seconds;
  81. fd_set rfds, wfds, efds;
  82. FD_ZERO(&rfds);
  83. FD_ZERO(&wfds);
  84. FD_ZERO(&efds);
  85. while(timeLeft >= 0) {
  86. zookeeper_interest(zh, &fd, &interest, &tv);
  87. if (fd != -1) {
  88. if (interest&ZOOKEEPER_READ) {
  89. FD_SET(fd, &rfds);
  90. } else {
  91. FD_CLR(fd, &rfds);
  92. }
  93. if (interest&ZOOKEEPER_WRITE) {
  94. FD_SET(fd, &wfds);
  95. } else {
  96. FD_CLR(fd, &wfds);
  97. }
  98. } else {
  99. fd = 0;
  100. }
  101. FD_SET(0, &rfds);
  102. if (tv.tv_sec > timeLeft) {
  103. tv.tv_sec = timeLeft;
  104. }
  105. rc = select(fd+1, &rfds, &wfds, &efds, &tv);
  106. timeLeft = expires - time(0);
  107. events = 0;
  108. if (FD_ISSET(fd, &rfds)) {
  109. events |= ZOOKEEPER_READ;
  110. }
  111. if (FD_ISSET(fd, &wfds)) {
  112. events |= ZOOKEEPER_WRITE;
  113. }
  114. zookeeper_process(zh, events);
  115. }
  116. }
  117. #endif
  118. typedef struct evt {
  119. string path;
  120. int type;
  121. } evt_t;
  122. typedef struct watchCtx {
  123. private:
  124. list<evt_t> events;
  125. watchCtx(const watchCtx&);
  126. watchCtx& operator=(const watchCtx&);
  127. public:
  128. bool connected;
  129. zhandle_t *zh;
  130. Mutex mutex;
  131. watchCtx() {
  132. connected = false;
  133. zh = 0;
  134. }
  135. ~watchCtx() {
  136. if (zh) {
  137. zookeeper_close(zh);
  138. zh = 0;
  139. }
  140. }
  141. evt_t getEvent() {
  142. evt_t evt;
  143. mutex.acquire();
  144. CPPUNIT_ASSERT( events.size() > 0);
  145. evt = events.front();
  146. events.pop_front();
  147. mutex.release();
  148. return evt;
  149. }
  150. int countEvents() {
  151. int count;
  152. mutex.acquire();
  153. count = events.size();
  154. mutex.release();
  155. return count;
  156. }
  157. void putEvent(evt_t evt) {
  158. mutex.acquire();
  159. events.push_back(evt);
  160. mutex.release();
  161. }
  162. bool waitForConnected(zhandle_t *zh) {
  163. time_t expires = time(0) + 10;
  164. while(!connected && time(0) < expires) {
  165. yield(zh, 1);
  166. }
  167. return connected;
  168. }
  169. bool waitForDisconnected(zhandle_t *zh) {
  170. time_t expires = time(0) + 15;
  171. while(connected && time(0) < expires) {
  172. yield(zh, 1);
  173. }
  174. return !connected;
  175. }
  176. } watchctx_t;
  177. class Zookeeper_simpleSystem : public CPPUNIT_NS::TestFixture
  178. {
  179. CPPUNIT_TEST_SUITE(Zookeeper_simpleSystem);
  180. CPPUNIT_TEST(testLogCallbackSet);
  181. CPPUNIT_TEST(testLogCallbackInit);
  182. CPPUNIT_TEST(testLogCallbackClear);
  183. CPPUNIT_TEST(testAsyncWatcherAutoReset);
  184. CPPUNIT_TEST(testDeserializeString);
  185. CPPUNIT_TEST(testFirstServerDown);
  186. CPPUNIT_TEST(testNonexistentHost);
  187. #ifdef THREADED
  188. CPPUNIT_TEST(testNullData);
  189. #ifdef ZOO_IPV6_ENABLED
  190. CPPUNIT_TEST(testIPV6);
  191. #endif
  192. #ifdef HAVE_OPENSSL_H
  193. CPPUNIT_TEST(testSSL);
  194. #endif
  195. CPPUNIT_TEST(testCreate);
  196. CPPUNIT_TEST(testCreateContainer);
  197. CPPUNIT_TEST(testCreateTtl);
  198. CPPUNIT_TEST(testPath);
  199. CPPUNIT_TEST(testPathValidation);
  200. CPPUNIT_TEST(testPing);
  201. CPPUNIT_TEST(testAcl);
  202. CPPUNIT_TEST(testChroot);
  203. CPPUNIT_TEST(testAuth);
  204. CPPUNIT_TEST(testHangingClient);
  205. CPPUNIT_TEST(testWatcherAutoResetWithGlobal);
  206. CPPUNIT_TEST(testWatcherAutoResetWithLocal);
  207. CPPUNIT_TEST(testGetChildren2);
  208. CPPUNIT_TEST(testLastZxid);
  209. CPPUNIT_TEST(testRemoveWatchers);
  210. #endif
  211. CPPUNIT_TEST_SUITE_END();
  212. static void watcher(zhandle_t *, int type, int state, const char *path,void*v){
  213. watchctx_t *ctx = (watchctx_t*)v;
  214. if (state == ZOO_CONNECTED_STATE) {
  215. ctx->connected = true;
  216. } else {
  217. ctx->connected = false;
  218. }
  219. if (type != ZOO_SESSION_EVENT) {
  220. evt_t evt;
  221. evt.path = path;
  222. evt.type = type;
  223. ctx->putEvent(evt);
  224. }
  225. }
  226. static const char hostPorts[];
  227. const char *getHostPorts() {
  228. return hostPorts;
  229. }
  230. zhandle_t *createClient(watchctx_t *ctx) {
  231. return createClient(hostPorts, ctx);
  232. }
  233. zhandle_t *createClient(watchctx_t *ctx, log_callback_fn logCallback) {
  234. zhandle_t *zk = zookeeper_init2(hostPorts, watcher, 10000, 0, ctx, 0, logCallback);
  235. ctx->zh = zk;
  236. sleep(1);
  237. return zk;
  238. }
  239. zhandle_t *createClient(const char *hp, watchctx_t *ctx) {
  240. zhandle_t *zk = zookeeper_init(hp, watcher, 10000, 0, ctx, 0);
  241. ctx->zh = zk;
  242. sleep(1);
  243. return zk;
  244. }
  245. #ifdef HAVE_OPENSSL_H
  246. zhandle_t *createSSLClient(const char *hp, const char *cert, watchctx_t *ctx) {
  247. zhandle_t *zk = zookeeper_init_ssl(hp, cert, watcher, 30000, 0, ctx, 0);
  248. ctx->zh = zk;
  249. sleep(1);
  250. return zk;
  251. }
  252. #endif
  253. zhandle_t *createchClient(watchctx_t *ctx, const char* chroot) {
  254. zhandle_t *zk = zookeeper_init(chroot, watcher, 10000, 0, ctx, 0);
  255. ctx->zh = zk;
  256. sleep(1);
  257. return zk;
  258. }
  259. FILE *logfile;
  260. public:
  261. Zookeeper_simpleSystem() {
  262. logfile = openlogfile("Zookeeper_simpleSystem");
  263. }
  264. ~Zookeeper_simpleSystem() {
  265. if (logfile) {
  266. fflush(logfile);
  267. fclose(logfile);
  268. logfile = 0;
  269. }
  270. }
  271. void setUp()
  272. {
  273. zoo_set_log_stream(logfile);
  274. }
  275. void startServer() {
  276. char cmd[1024];
  277. sprintf(cmd, "%s start %s", ZKSERVER_CMD, getHostPorts());
  278. CPPUNIT_ASSERT(system(cmd) == 0);
  279. }
  280. void stopServer() {
  281. char cmd[1024];
  282. sprintf(cmd, "%s stop %s", ZKSERVER_CMD, getHostPorts());
  283. CPPUNIT_ASSERT(system(cmd) == 0);
  284. }
  285. void tearDown()
  286. {
  287. }
  288. /** have a callback in the default watcher **/
  289. static void default_zoo_watcher(zhandle_t *zzh, int type, int state, const char *path, void *context){
  290. int zrc = 0;
  291. struct String_vector str_vec = {0, NULL};
  292. zrc = zoo_wget_children(zzh, "/mytest", default_zoo_watcher, NULL, &str_vec);
  293. }
  294. /** ZOOKEEPER-1057 This checks that the client connects to the second server when the first is not reachable **/
  295. void testFirstServerDown() {
  296. watchctx_t ctx;
  297. zoo_deterministic_conn_order(true);
  298. zhandle_t* zk = createClient("127.0.0.1:22182,127.0.0.1:22181", &ctx);
  299. CPPUNIT_ASSERT(zk != 0);
  300. CPPUNIT_ASSERT(ctx.waitForConnected(zk));
  301. }
  302. /* Checks that a non-existent host will not block the connection*/
  303. void testNonexistentHost() {
  304. char hosts[] = "jimmy:5555,127.0.0.1:22181";
  305. watchctx_t ctx;
  306. zoo_deterministic_conn_order(true /* disable permute */);
  307. zhandle_t *zh = createClient(hosts, &ctx);
  308. CPPUNIT_ASSERT(ctx.waitForConnected(zh));
  309. zoo_deterministic_conn_order(false /* enable permute */);
  310. }
  311. /** this checks for a deadlock in calling zookeeper_close and calls from a default watcher that might get triggered just when zookeeper_close() is in progress **/
  312. void testHangingClient() {
  313. int zrc = 0;
  314. char buff[10] = "testall";
  315. char path[512];
  316. watchctx_t *ctx;
  317. struct String_vector str_vec = {0, NULL};
  318. zhandle_t *zh = zookeeper_init(hostPorts, NULL, 10000, 0, ctx, 0);
  319. sleep(1);
  320. zrc = zoo_create(zh, "/mytest", buff, 10, &ZOO_OPEN_ACL_UNSAFE, 0, path, 512);
  321. zrc = zoo_wget_children(zh, "/mytest", default_zoo_watcher, NULL, &str_vec);
  322. zrc = zoo_create(zh, "/mytest/test1", buff, 10, &ZOO_OPEN_ACL_UNSAFE, 0, path, 512);
  323. zrc = zoo_wget_children(zh, "/mytest", default_zoo_watcher, NULL, &str_vec);
  324. zrc = zoo_delete(zh, "/mytest/test1", -1);
  325. zookeeper_close(zh);
  326. }
  327. void testBadDescriptor() {
  328. int zrc = 0;
  329. watchctx_t *ctx;
  330. zhandle_t *zh = zookeeper_init(hostPorts, NULL, 10000, 0, ctx, 0);
  331. sleep(1);
  332. zh->io_count = 0;
  333. //close socket
  334. close_zsock(zh->fd);
  335. sleep(1);
  336. //Check that doIo isn't spinning
  337. CPPUNIT_ASSERT(zh->io_count < 2);
  338. zookeeper_close(zh);
  339. }
  340. void testPing()
  341. {
  342. watchctx_t ctxIdle;
  343. watchctx_t ctxWC;
  344. zhandle_t *zkIdle = createClient(&ctxIdle);
  345. zhandle_t *zkWatchCreator = createClient(&ctxWC);
  346. CPPUNIT_ASSERT(zkIdle);
  347. CPPUNIT_ASSERT(zkWatchCreator);
  348. char path[80];
  349. sprintf(path, "/testping");
  350. int rc = zoo_create(zkWatchCreator, path, "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0);
  351. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  352. for(int i = 0; i < 30; i++) {
  353. sprintf(path, "/testping/%i", i);
  354. rc = zoo_create(zkWatchCreator, path, "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0);
  355. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  356. }
  357. for(int i = 0; i < 30; i++) {
  358. sprintf(path, "/testping/%i", i);
  359. struct Stat stat;
  360. rc = zoo_exists(zkIdle, path, 1, &stat);
  361. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  362. }
  363. for(int i = 0; i < 30; i++) {
  364. sprintf(path, "/testping/%i", i);
  365. usleep(500000);
  366. rc = zoo_delete(zkWatchCreator, path, -1);
  367. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  368. }
  369. struct Stat stat;
  370. CPPUNIT_ASSERT_EQUAL((int)ZNONODE, zoo_exists(zkIdle, "/testping/0", 0, &stat));
  371. }
  372. bool waitForEvent(zhandle_t *zh, watchctx_t *ctx, int seconds) {
  373. time_t expires = time(0) + seconds;
  374. while(ctx->countEvents() == 0 && time(0) < expires) {
  375. yield(zh, 1);
  376. }
  377. return ctx->countEvents() > 0;
  378. }
  379. #define COUNT 100
  380. static zhandle_t *async_zk;
  381. static volatile int count;
  382. static const char* hp_chroot;
  383. static void statCompletion(int rc, const struct Stat *stat, const void *data) {
  384. int tmp = (int) (long) data;
  385. CPPUNIT_ASSERT_EQUAL(tmp, rc);
  386. }
  387. static void stringCompletion(int rc, const char *value, const void *data) {
  388. char *path = (char*)data;
  389. if (rc == ZCONNECTIONLOSS && path) {
  390. // Try again
  391. rc = zoo_acreate(async_zk, path, "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, stringCompletion, 0);
  392. } else if (rc != ZOK) {
  393. // fprintf(stderr, "rc = %d with path = %s\n", rc, (path ? path : "null"));
  394. }
  395. if (path) {
  396. free(path);
  397. }
  398. }
  399. static void stringStatCompletion(int rc, const char *value, const struct Stat *stat,
  400. const void *data) {
  401. stringCompletion(rc, value, data);
  402. CPPUNIT_ASSERT(stat != 0);
  403. }
  404. static void create_completion_fn(int rc, const char* value, const void *data) {
  405. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  406. if (data) {
  407. const char *expected_value = (const char *)data;
  408. CPPUNIT_ASSERT_EQUAL(string(expected_value), string(value));
  409. }
  410. count++;
  411. }
  412. static void waitForCreateCompletion(int seconds) {
  413. time_t expires = time(0) + seconds;
  414. while(count == 0 && time(0) < expires) {
  415. sleep(1);
  416. }
  417. count--;
  418. }
  419. static void watcher_chroot_fn(zhandle_t *zh, int type,
  420. int state, const char *path,void *watcherCtx) {
  421. // check for path
  422. char *client_path = (char *) watcherCtx;
  423. CPPUNIT_ASSERT(strcmp(client_path, path) == 0);
  424. count ++;
  425. }
  426. static void waitForChrootWatch(int seconds) {
  427. time_t expires = time(0) + seconds;
  428. while (count == 0 && time(0) < expires) {
  429. sleep(1);
  430. }
  431. count--;
  432. }
  433. static void waitForVoidCompletion(int seconds) {
  434. time_t expires = time(0) + seconds;
  435. while(count == 0 && time(0) < expires) {
  436. sleep(1);
  437. }
  438. count--;
  439. }
  440. static void voidCompletion(int rc, const void *data) {
  441. int tmp = (int) (long) data;
  442. CPPUNIT_ASSERT_EQUAL(tmp, rc);
  443. count++;
  444. }
  445. static void verifyCreateFails(const char *path, zhandle_t *zk) {
  446. CPPUNIT_ASSERT_EQUAL((int)ZBADARGUMENTS, zoo_create(zk,
  447. path, "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0));
  448. }
  449. static void verifyCreateOk(const char *path, zhandle_t *zk) {
  450. CPPUNIT_ASSERT_EQUAL((int)ZOK, zoo_create(zk,
  451. path, "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0));
  452. }
  453. static void verifyCreateFailsSeq(const char *path, zhandle_t *zk) {
  454. CPPUNIT_ASSERT_EQUAL((int)ZBADARGUMENTS, zoo_create(zk,
  455. path, "", 0, &ZOO_OPEN_ACL_UNSAFE, ZOO_SEQUENCE, 0, 0));
  456. }
  457. static void verifyCreateOkSeq(const char *path, zhandle_t *zk) {
  458. CPPUNIT_ASSERT_EQUAL((int)ZOK, zoo_create(zk,
  459. path, "", 0, &ZOO_OPEN_ACL_UNSAFE, ZOO_SEQUENCE, 0, 0));
  460. }
  461. /**
  462. returns false if the vectors dont match
  463. **/
  464. bool compareAcl(struct ACL_vector acl1, struct ACL_vector acl2) {
  465. if (acl1.count != acl2.count) {
  466. return false;
  467. }
  468. struct ACL *aclval1 = acl1.data;
  469. struct ACL *aclval2 = acl2.data;
  470. if (aclval1->perms != aclval2->perms) {
  471. return false;
  472. }
  473. struct Id id1 = aclval1->id;
  474. struct Id id2 = aclval2->id;
  475. if (strcmp(id1.scheme, id2.scheme) != 0) {
  476. return false;
  477. }
  478. if (strcmp(id1.id, id2.id) != 0) {
  479. return false;
  480. }
  481. return true;
  482. }
  483. void testDeserializeString() {
  484. char *val_str;
  485. int rc = 0;
  486. int val = -1;
  487. struct iarchive *ia;
  488. struct buff_struct_2 *b;
  489. struct oarchive *oa = create_buffer_oarchive();
  490. oa->serialize_Int(oa, "int", &val);
  491. b = (struct buff_struct_2 *) oa->priv;
  492. ia = create_buffer_iarchive(b->buffer, b->len);
  493. rc = ia->deserialize_String(ia, "string", &val_str);
  494. CPPUNIT_ASSERT_EQUAL(-EINVAL, rc);
  495. }
  496. void testAcl() {
  497. int rc;
  498. struct ACL_vector aclvec;
  499. struct Stat stat;
  500. watchctx_t ctx;
  501. zhandle_t *zk = createClient(&ctx);
  502. rc = zoo_create(zk, "/acl", "", 0,
  503. &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0);
  504. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  505. rc = zoo_get_acl(zk, "/acl", &aclvec, &stat );
  506. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  507. bool cmp = compareAcl(ZOO_OPEN_ACL_UNSAFE, aclvec);
  508. CPPUNIT_ASSERT_EQUAL(true, cmp);
  509. rc = zoo_set_acl(zk, "/acl", -1, &ZOO_READ_ACL_UNSAFE);
  510. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  511. rc = zoo_get_acl(zk, "/acl", &aclvec, &stat);
  512. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  513. cmp = compareAcl(ZOO_READ_ACL_UNSAFE, aclvec);
  514. CPPUNIT_ASSERT_EQUAL(true, cmp);
  515. }
  516. void testAuth() {
  517. int rc;
  518. count = 0;
  519. watchctx_t ctx1, ctx2, ctx3, ctx4, ctx5;
  520. zhandle_t *zk = createClient(&ctx1);
  521. struct ACL_vector nodeAcl;
  522. struct ACL acl_val;
  523. rc = zoo_add_auth(0, "", 0, 0, voidCompletion, (void*)-1);
  524. CPPUNIT_ASSERT_EQUAL((int) ZBADARGUMENTS, rc);
  525. rc = zoo_add_auth(zk, 0, 0, 0, voidCompletion, (void*)-1);
  526. CPPUNIT_ASSERT_EQUAL((int) ZBADARGUMENTS, rc);
  527. // auth as pat, create /tauth1, close session
  528. rc = zoo_add_auth(zk, "digest", "pat:passwd", 10, voidCompletion,
  529. (void*)ZOK);
  530. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  531. waitForVoidCompletion(3);
  532. CPPUNIT_ASSERT(count == 0);
  533. rc = zoo_create(zk, "/tauth1", "", 0, &ZOO_CREATOR_ALL_ACL, 0, 0, 0);
  534. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  535. {
  536. //create a new client
  537. zk = createClient(&ctx4);
  538. rc = zoo_add_auth(zk, "digest", "", 0, voidCompletion, (void*)ZOK);
  539. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  540. waitForVoidCompletion(3);
  541. CPPUNIT_ASSERT(count == 0);
  542. rc = zoo_add_auth(zk, "digest", "", 0, voidCompletion, (void*)ZOK);
  543. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  544. waitForVoidCompletion(3);
  545. CPPUNIT_ASSERT(count == 0);
  546. }
  547. //create a new client
  548. zk = createClient(&ctx2);
  549. rc = zoo_add_auth(zk, "digest", "pat:passwd2", 11, voidCompletion,
  550. (void*)ZOK);
  551. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  552. waitForVoidCompletion(3);
  553. CPPUNIT_ASSERT(count == 0);
  554. char buf[1024];
  555. int blen = sizeof(buf);
  556. struct Stat stat;
  557. rc = zoo_get(zk, "/tauth1", 0, buf, &blen, &stat);
  558. CPPUNIT_ASSERT_EQUAL((int)ZNOAUTH, rc);
  559. // add auth pat w/correct pass verify success
  560. rc = zoo_add_auth(zk, "digest", "pat:passwd", 10, voidCompletion,
  561. (void*)ZOK);
  562. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  563. rc = zoo_get(zk, "/tauth1", 0, buf, &blen, &stat);
  564. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  565. waitForVoidCompletion(3);
  566. CPPUNIT_ASSERT(count == 0);
  567. //create a new client
  568. zk = createClient(&ctx3);
  569. rc = zoo_add_auth(zk, "digest", "pat:passwd", 10, voidCompletion, (void*) ZOK);
  570. waitForVoidCompletion(3);
  571. CPPUNIT_ASSERT(count == 0);
  572. rc = zoo_add_auth(zk, "ip", "none", 4, voidCompletion, (void*)ZOK);
  573. //make the server forget the auths
  574. waitForVoidCompletion(3);
  575. CPPUNIT_ASSERT(count == 0);
  576. stopServer();
  577. CPPUNIT_ASSERT(ctx3.waitForDisconnected(zk));
  578. startServer();
  579. CPPUNIT_ASSERT(ctx3.waitForConnected(zk));
  580. // now try getting the data
  581. rc = zoo_get(zk, "/tauth1", 0, buf, &blen, &stat);
  582. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  583. // also check for get
  584. rc = zoo_get_acl(zk, "/", &nodeAcl, &stat);
  585. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  586. // check if the acl has all the perms
  587. CPPUNIT_ASSERT_EQUAL((int)1, (int)nodeAcl.count);
  588. acl_val = *(nodeAcl.data);
  589. CPPUNIT_ASSERT_EQUAL((int) acl_val.perms, ZOO_PERM_ALL);
  590. // verify on root node
  591. rc = zoo_set_acl(zk, "/", -1, &ZOO_CREATOR_ALL_ACL);
  592. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  593. rc = zoo_set_acl(zk, "/", -1, &ZOO_OPEN_ACL_UNSAFE);
  594. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  595. //[ZOOKEEPER-1108], test that auth info is sent to server, if client is not
  596. //connected to server when zoo_add_auth was called.
  597. zhandle_t *zk_auth = zookeeper_init(hostPorts, NULL, 10000, 0, NULL, 0);
  598. rc = zoo_add_auth(zk_auth, "digest", "pat:passwd", 10, voidCompletion, (void*)ZOK);
  599. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  600. sleep(2);
  601. CPPUNIT_ASSERT(count == 1);
  602. count = 0;
  603. CPPUNIT_ASSERT_EQUAL((int) ZOK, zookeeper_close(zk_auth));
  604. struct sockaddr addr;
  605. socklen_t addr_len = sizeof(addr);
  606. zk = createClient(&ctx5);
  607. stopServer();
  608. CPPUNIT_ASSERT(ctx5.waitForDisconnected(zk));
  609. CPPUNIT_ASSERT(zookeeper_get_connected_host(zk, &addr, &addr_len) == NULL);
  610. addr_len = sizeof(addr);
  611. startServer();
  612. CPPUNIT_ASSERT(ctx5.waitForConnected(zk));
  613. CPPUNIT_ASSERT(zookeeper_get_connected_host(zk, &addr, &addr_len) != NULL);
  614. }
  615. void testCreate() {
  616. watchctx_t ctx;
  617. int rc = 0;
  618. zhandle_t *zk = createClient(&ctx);
  619. CPPUNIT_ASSERT(zk);
  620. char pathbuf[80];
  621. struct Stat stat_a = {0};
  622. struct Stat stat_b = {0};
  623. rc = zoo_create2(zk, "/testcreateA", "", 0,
  624. &ZOO_OPEN_ACL_UNSAFE, 0, pathbuf, sizeof(pathbuf), &stat_a);
  625. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  626. CPPUNIT_ASSERT(strcmp(pathbuf, "/testcreateA") == 0);
  627. CPPUNIT_ASSERT(stat_a.czxid > 0);
  628. CPPUNIT_ASSERT(stat_a.mtime > 0);
  629. rc = zoo_create2(zk, "/testcreateB", "", 0,
  630. &ZOO_OPEN_ACL_UNSAFE, 0, pathbuf, sizeof(pathbuf), &stat_b);
  631. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  632. CPPUNIT_ASSERT(strcmp(pathbuf, "/testcreateB") == 0);
  633. CPPUNIT_ASSERT(stat_b.czxid > 0);
  634. CPPUNIT_ASSERT(stat_b.mtime > 0);
  635. // Should get different Stats back from different creates
  636. CPPUNIT_ASSERT(Stat_eq(&stat_a, &stat_b) != 1);
  637. }
  638. void testCreateContainer() {
  639. watchctx_t ctx;
  640. int rc = 0;
  641. zhandle_t *zk = createClient(&ctx);
  642. CPPUNIT_ASSERT(zk);
  643. char pathbuf[80];
  644. struct Stat stat = {0};
  645. rc = zoo_create2(zk, "/testContainer", "", 0, &ZOO_OPEN_ACL_UNSAFE,
  646. ZOO_CONTAINER, pathbuf, sizeof(pathbuf), &stat);
  647. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  648. }
  649. void testCreateTtl() {
  650. watchctx_t ctx;
  651. int rc = 0;
  652. zhandle_t *zk = createClient(&ctx);
  653. CPPUNIT_ASSERT(zk);
  654. char pathbuf[80];
  655. struct Stat stat = {0};
  656. rc = zoo_create2_ttl(zk, "/testTtl", "", 0, &ZOO_OPEN_ACL_UNSAFE,
  657. ZOO_PERSISTENT_WITH_TTL, 1, pathbuf, sizeof(pathbuf), &stat);
  658. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  659. sleep(1);
  660. rc = zoo_exists(zk, "/testTtl", 1, &stat);
  661. CPPUNIT_ASSERT_EQUAL((int) ZNONODE, rc);
  662. }
  663. void testGetChildren2() {
  664. int rc;
  665. watchctx_t ctx;
  666. zhandle_t *zk = createClient(&ctx);
  667. rc = zoo_create(zk, "/parent", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0);
  668. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  669. rc = zoo_create(zk, "/parent/child_a", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0);
  670. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  671. rc = zoo_create(zk, "/parent/child_b", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0);
  672. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  673. rc = zoo_create(zk, "/parent/child_c", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0);
  674. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  675. rc = zoo_create(zk, "/parent/child_d", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0);
  676. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  677. struct String_vector strings;
  678. struct Stat stat_a, stat_b;
  679. rc = zoo_get_children2(zk, "/parent", 0, &strings, &stat_a);
  680. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  681. rc = zoo_exists(zk, "/parent", 0, &stat_b);
  682. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  683. CPPUNIT_ASSERT(Stat_eq(&stat_a, &stat_b));
  684. CPPUNIT_ASSERT(stat_a.numChildren == 4);
  685. }
  686. void testIPV6() {
  687. watchctx_t ctx;
  688. zhandle_t *zk = createClient("::1:22181", &ctx);
  689. CPPUNIT_ASSERT(zk);
  690. int rc = 0;
  691. rc = zoo_create(zk, "/ipv6", NULL, -1,
  692. &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0);
  693. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  694. }
  695. #ifdef HAVE_OPENSSL_H
  696. void testSSL() {
  697. watchctx_t ctx;
  698. zoo_set_debug_level(ZOO_LOG_LEVEL_DEBUG);
  699. zhandle_t *zk = createSSLClient("127.0.0.1:22281", "/tmp/certs/server.crt,/tmp/certs/client.crt,/tmp/certs/clientkey.pem,password", &ctx);
  700. CPPUNIT_ASSERT(zk);
  701. int rc = 0;
  702. rc = zoo_create(zk, "/ssl", NULL, -1,
  703. &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0);
  704. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  705. }
  706. #endif
  707. void testNullData() {
  708. watchctx_t ctx;
  709. zhandle_t *zk = createClient(&ctx);
  710. CPPUNIT_ASSERT(zk);
  711. int rc = 0;
  712. rc = zoo_create(zk, "/mahadev", NULL, -1,
  713. &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0);
  714. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  715. char buffer[512];
  716. struct Stat stat;
  717. int len = 512;
  718. rc = zoo_wget(zk, "/mahadev", NULL, NULL, buffer, &len, &stat);
  719. CPPUNIT_ASSERT_EQUAL( -1, len);
  720. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  721. rc = zoo_set(zk, "/mahadev", NULL, -1, -1);
  722. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  723. rc = zoo_wget(zk, "/mahadev", NULL, NULL, buffer, &len, &stat);
  724. CPPUNIT_ASSERT_EQUAL( -1, len);
  725. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  726. }
  727. void testPath() {
  728. watchctx_t ctx;
  729. char pathbuf[20];
  730. zhandle_t *zk = createClient(&ctx);
  731. CPPUNIT_ASSERT(zk);
  732. int rc = 0;
  733. memset(pathbuf, 'X', 20);
  734. rc = zoo_create(zk, "/testpathpath0", "", 0,
  735. &ZOO_OPEN_ACL_UNSAFE, 0, pathbuf, 0);
  736. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  737. CPPUNIT_ASSERT_EQUAL('X', pathbuf[0]);
  738. rc = zoo_create(zk, "/testpathpath1", "", 0,
  739. &ZOO_OPEN_ACL_UNSAFE, 0, pathbuf, 1);
  740. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  741. CPPUNIT_ASSERT(strlen(pathbuf) == 0);
  742. rc = zoo_create(zk, "/testpathpath2", "", 0,
  743. &ZOO_OPEN_ACL_UNSAFE, 0, pathbuf, 2);
  744. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  745. CPPUNIT_ASSERT(strcmp(pathbuf, "/") == 0);
  746. rc = zoo_create(zk, "/testpathpath3", "", 0,
  747. &ZOO_OPEN_ACL_UNSAFE, 0, pathbuf, 3);
  748. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  749. CPPUNIT_ASSERT(strcmp(pathbuf, "/t") == 0);
  750. rc = zoo_create(zk, "/testpathpath7", "", 0,
  751. &ZOO_OPEN_ACL_UNSAFE, 0, pathbuf, 15);
  752. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  753. CPPUNIT_ASSERT(strcmp(pathbuf, "/testpathpath7") == 0);
  754. rc = zoo_create(zk, "/testpathpath8", "", 0,
  755. &ZOO_OPEN_ACL_UNSAFE, 0, pathbuf, 16);
  756. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  757. CPPUNIT_ASSERT(strcmp(pathbuf, "/testpathpath8") == 0);
  758. }
  759. void testPathValidation() {
  760. watchctx_t ctx;
  761. zhandle_t *zk = createClient(&ctx);
  762. CPPUNIT_ASSERT(zk);
  763. verifyCreateFails(0, zk);
  764. verifyCreateFails("", zk);
  765. verifyCreateFails("//", zk);
  766. verifyCreateFails("///", zk);
  767. verifyCreateFails("////", zk);
  768. verifyCreateFails("/.", zk);
  769. verifyCreateFails("/..", zk);
  770. verifyCreateFails("/./", zk);
  771. verifyCreateFails("/../", zk);
  772. verifyCreateFails("/foo/./", zk);
  773. verifyCreateFails("/foo/../", zk);
  774. verifyCreateFails("/foo/.", zk);
  775. verifyCreateFails("/foo/..", zk);
  776. verifyCreateFails("/./.", zk);
  777. verifyCreateFails("/../..", zk);
  778. verifyCreateFails("/foo/bar/", zk);
  779. verifyCreateFails("/foo//bar", zk);
  780. verifyCreateFails("/foo/bar//", zk);
  781. verifyCreateFails("foo", zk);
  782. verifyCreateFails("a", zk);
  783. // verify that trailing fails, except for seq which adds suffix
  784. verifyCreateOk("/createseq", zk);
  785. verifyCreateFails("/createseq/", zk);
  786. verifyCreateOkSeq("/createseq/", zk);
  787. verifyCreateOkSeq("/createseq/.", zk);
  788. verifyCreateOkSeq("/createseq/..", zk);
  789. verifyCreateFailsSeq("/createseq//", zk);
  790. verifyCreateFailsSeq("/createseq/./", zk);
  791. verifyCreateFailsSeq("/createseq/../", zk);
  792. verifyCreateOk("/.foo", zk);
  793. verifyCreateOk("/.f.", zk);
  794. verifyCreateOk("/..f", zk);
  795. verifyCreateOk("/..f..", zk);
  796. verifyCreateOk("/f.c", zk);
  797. verifyCreateOk("/f", zk);
  798. verifyCreateOk("/f/.f", zk);
  799. verifyCreateOk("/f/f.", zk);
  800. verifyCreateOk("/f/..f", zk);
  801. verifyCreateOk("/f/f..", zk);
  802. verifyCreateOk("/f/.f/f", zk);
  803. verifyCreateOk("/f/f./f", zk);
  804. }
  805. void testChroot() {
  806. // the c client async callbacks do
  807. // not callback with the path, so
  808. // we dont need to test taht for now
  809. // we should fix that though soon!
  810. watchctx_t ctx, ctx_ch;
  811. zhandle_t *zk, *zk_ch;
  812. char buf[60];
  813. int rc, len;
  814. struct Stat stat;
  815. const char* data = "garbage";
  816. const char* retStr = "/chroot";
  817. const char* root= "/";
  818. zk_ch = createchClient(&ctx_ch, "127.0.0.1:22181/testch1/mahadev");
  819. CPPUNIT_ASSERT(zk_ch != NULL);
  820. zk = createClient(&ctx);
  821. // first test with a NULL zk handle, make sure client library does not
  822. // dereference a null pointer, but instead returns ZBADARGUMENTS
  823. rc = zoo_create(NULL, "/testch1", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0);
  824. CPPUNIT_ASSERT_EQUAL((int) ZBADARGUMENTS, rc);
  825. rc = zoo_create(zk, "/testch1", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0);
  826. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  827. rc = zoo_create(zk, "/testch1/mahadev", data, 7, &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0);
  828. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  829. // try an exists with /
  830. len = 60;
  831. rc = zoo_get(zk_ch, "/", 0, buf, &len, &stat);
  832. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  833. //check if the data is the same
  834. CPPUNIT_ASSERT(strncmp(buf, data, 7) == 0);
  835. //check for watches
  836. rc = zoo_wexists(zk_ch, "/chroot", watcher_chroot_fn, (void *) retStr, &stat);
  837. //now check if we can do create/delete/get/sets/acls/getChildren and others
  838. //check create
  839. rc = zoo_create(zk_ch, "/chroot", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, 0,0);
  840. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  841. waitForChrootWatch(3);
  842. CPPUNIT_ASSERT(count == 0);
  843. rc = zoo_create(zk_ch, "/chroot/child", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0);
  844. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  845. rc = zoo_exists(zk, "/testch1/mahadev/chroot/child", 0, &stat);
  846. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  847. rc = zoo_delete(zk_ch, "/chroot/child", -1);
  848. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  849. rc = zoo_exists(zk, "/testch1/mahadev/chroot/child", 0, &stat);
  850. CPPUNIT_ASSERT_EQUAL((int) ZNONODE, rc);
  851. rc = zoo_wget(zk_ch, "/chroot", watcher_chroot_fn, (char*) retStr,
  852. buf, &len, &stat);
  853. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  854. rc = zoo_set(zk_ch, "/chroot",buf, 3, -1);
  855. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  856. waitForChrootWatch(3);
  857. CPPUNIT_ASSERT(count == 0);
  858. // check for getchildren
  859. struct String_vector children;
  860. rc = zoo_get_children(zk_ch, "/", 0, &children);
  861. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  862. CPPUNIT_ASSERT_EQUAL((int)1, (int)children.count);
  863. //check if te child if chroot
  864. CPPUNIT_ASSERT(strcmp((retStr+1), children.data[0]) == 0);
  865. // check for get/set acl
  866. struct ACL_vector acl;
  867. rc = zoo_get_acl(zk_ch, "/", &acl, &stat);
  868. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  869. CPPUNIT_ASSERT_EQUAL((int)1, (int)acl.count);
  870. CPPUNIT_ASSERT_EQUAL((int)ZOO_PERM_ALL, (int)acl.data->perms);
  871. // set acl
  872. rc = zoo_set_acl(zk_ch, "/chroot", -1, &ZOO_READ_ACL_UNSAFE);
  873. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  874. // see if you add children
  875. rc = zoo_create(zk_ch, "/chroot/child1", "",0, &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0);
  876. CPPUNIT_ASSERT_EQUAL((int)ZNOAUTH, rc);
  877. //add wget children test
  878. rc = zoo_wget_children(zk_ch, "/", watcher_chroot_fn, (char*) root, &children);
  879. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  880. //now create a node
  881. rc = zoo_create(zk_ch, "/child2", "",0, &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0);
  882. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  883. waitForChrootWatch(3);
  884. CPPUNIT_ASSERT(count == 0);
  885. //check for one async call just to make sure
  886. rc = zoo_acreate(zk_ch, "/child3", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0,
  887. create_completion_fn, 0);
  888. waitForCreateCompletion(3);
  889. CPPUNIT_ASSERT(count == 0);
  890. //ZOOKEEPER-1027 correctly return path_buffer without prefixed chroot
  891. const char* path = "/zookeeper1027";
  892. char path_buffer[1024];
  893. int path_buffer_len=sizeof(path_buffer);
  894. rc = zoo_create(zk_ch, path, "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, path_buffer, path_buffer_len);
  895. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  896. CPPUNIT_ASSERT_EQUAL(string(path), string(path_buffer));
  897. const char* path2282 = "/zookeeper2282";
  898. rc = zoo_acreate(zk_ch, path2282, "", 0, &ZOO_OPEN_ACL_UNSAFE, 0,
  899. create_completion_fn, path2282);
  900. waitForCreateCompletion(3);
  901. CPPUNIT_ASSERT(count == 0);
  902. }
  903. // Test creating normal handle via zookeeper_init then explicitly setting callback
  904. void testLogCallbackSet()
  905. {
  906. watchctx_t ctx;
  907. CPPUNIT_ASSERT(logMessages.empty());
  908. zhandle_t *zk = createClient(&ctx);
  909. zoo_set_log_callback(zk, &logMessageHandler);
  910. CPPUNIT_ASSERT_EQUAL(zoo_get_log_callback(zk), &logMessageHandler);
  911. // Log 10 messages and ensure all go to callback
  912. int expected = 10;
  913. for (int i = 0; i < expected; i++)
  914. {
  915. LOG_INFO(LOGCALLBACK(zk), "%s #%d", __FUNCTION__, i);
  916. }
  917. CPPUNIT_ASSERT(expected == logMessages.size());
  918. }
  919. // Test creating handle via zookeeper_init2 to ensure all connection messages go to callback
  920. void testLogCallbackInit()
  921. {
  922. logMessages.clear();
  923. watchctx_t ctx;
  924. zhandle_t *zk = createClient(&ctx, &logMessageHandler);
  925. CPPUNIT_ASSERT_EQUAL(zoo_get_log_callback(zk), &logMessageHandler);
  926. // All the connection messages should have gone to the callback -- don't
  927. // want this to be a maintenance issue so we're not asserting exact count
  928. int numBefore = logMessages.size();
  929. CPPUNIT_ASSERT(numBefore != 0);
  930. // Log 10 messages and ensure all go to callback
  931. int expected = 10;
  932. for (int i = 0; i < expected; i++)
  933. {
  934. LOG_INFO(LOGCALLBACK(zk), "%s #%d", __FUNCTION__, i);
  935. }
  936. CPPUNIT_ASSERT(logMessages.size() == numBefore + expected);
  937. }
  938. // Test clearing log callback -- logging should resume going to logstream
  939. void testLogCallbackClear()
  940. {
  941. logMessages.clear();
  942. watchctx_t ctx;
  943. zhandle_t *zk = createClient(&ctx, &logMessageHandler);
  944. CPPUNIT_ASSERT_EQUAL(zoo_get_log_callback(zk), &logMessageHandler);
  945. // All the connection messages should have gone to the callback -- again, we don't
  946. // want this to be a maintenance issue so we're not asserting exact count
  947. int numBefore = logMessages.size();
  948. CPPUNIT_ASSERT(numBefore > 0);
  949. // Clear log_callback
  950. zoo_set_log_callback(zk, NULL);
  951. // Future log messages should go to logstream not callback
  952. LOG_INFO(LOGCALLBACK(zk), __FUNCTION__);
  953. int numAfter = logMessages.size();
  954. CPPUNIT_ASSERT_EQUAL(numBefore, numAfter);
  955. }
  956. void testAsyncWatcherAutoReset()
  957. {
  958. watchctx_t ctx;
  959. zhandle_t *zk = createClient(&ctx);
  960. watchctx_t lctx[COUNT];
  961. int i;
  962. char path[80];
  963. int rc;
  964. evt_t evt;
  965. async_zk = zk;
  966. for(i = 0; i < COUNT; i++) {
  967. sprintf(path, "/awar%d", i);
  968. rc = zoo_awexists(zk, path, watcher, &lctx[i], statCompletion, (void*)ZNONODE);
  969. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  970. }
  971. yield(zk, 0);
  972. for(i = 0; i < COUNT/4; i++) {
  973. sprintf(path, "/awar%d", i);
  974. rc = zoo_acreate(zk, path, "", 0, &ZOO_OPEN_ACL_UNSAFE, 0,
  975. stringCompletion, strdup(path));
  976. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  977. }
  978. for(i = COUNT/4; i < COUNT/2; i++) {
  979. sprintf(path, "/awar%d", i);
  980. rc = zoo_acreate2(zk, path, "", 0, &ZOO_OPEN_ACL_UNSAFE, 0,
  981. stringStatCompletion, strdup(path));
  982. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  983. }
  984. yield(zk, 3);
  985. for(i = 0; i < COUNT/2; i++) {
  986. sprintf(path, "/awar%d", i);
  987. CPPUNIT_ASSERT_MESSAGE(path, waitForEvent(zk, &lctx[i], 5));
  988. evt = lctx[i].getEvent();
  989. CPPUNIT_ASSERT_EQUAL_MESSAGE(evt.path.c_str(), ZOO_CREATED_EVENT, evt.type);
  990. CPPUNIT_ASSERT_EQUAL(string(path), evt.path);
  991. }
  992. for(i = COUNT/2 + 1; i < COUNT*10; i++) {
  993. sprintf(path, "/awar%d", i);
  994. rc = zoo_acreate(zk, path, "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, stringCompletion, strdup(path));
  995. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  996. }
  997. yield(zk, 1);
  998. stopServer();
  999. CPPUNIT_ASSERT(ctx.waitForDisconnected(zk));
  1000. startServer();
  1001. CPPUNIT_ASSERT(ctx.waitForConnected(zk));
  1002. yield(zk, 3);
  1003. for(i = COUNT/2+1; i < COUNT; i++) {
  1004. sprintf(path, "/awar%d", i);
  1005. CPPUNIT_ASSERT_MESSAGE(path, waitForEvent(zk, &lctx[i], 5));
  1006. evt = lctx[i].getEvent();
  1007. CPPUNIT_ASSERT_EQUAL_MESSAGE(evt.path, ZOO_CREATED_EVENT, evt.type);
  1008. CPPUNIT_ASSERT_EQUAL(string(path), evt.path);
  1009. }
  1010. }
  1011. void testWatcherAutoReset(zhandle_t *zk, watchctx_t *ctxGlobal,
  1012. watchctx_t *ctxLocal)
  1013. {
  1014. bool isGlobal = (ctxGlobal == ctxLocal);
  1015. int rc;
  1016. struct Stat stat;
  1017. char buf[1024];
  1018. int blen;
  1019. struct String_vector strings;
  1020. const char *testName;
  1021. rc = zoo_create(zk, "/watchtest", "", 0,
  1022. &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0);
  1023. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  1024. rc = zoo_create(zk, "/watchtest/child", "", 0,
  1025. &ZOO_OPEN_ACL_UNSAFE, ZOO_EPHEMERAL, 0, 0);
  1026. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  1027. if (isGlobal) {
  1028. testName = "GlobalTest";
  1029. rc = zoo_get_children(zk, "/watchtest", 1, &strings);
  1030. deallocate_String_vector(&strings);
  1031. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  1032. blen = sizeof(buf);
  1033. rc = zoo_get(zk, "/watchtest/child", 1, buf, &blen, &stat);
  1034. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  1035. rc = zoo_exists(zk, "/watchtest/child2", 1, &stat);
  1036. CPPUNIT_ASSERT_EQUAL((int)ZNONODE, rc);
  1037. } else {
  1038. testName = "LocalTest";
  1039. rc = zoo_wget_children(zk, "/watchtest", watcher, ctxLocal,
  1040. &strings);
  1041. deallocate_String_vector(&strings);
  1042. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  1043. blen = sizeof(buf);
  1044. rc = zoo_wget(zk, "/watchtest/child", watcher, ctxLocal,
  1045. buf, &blen, &stat);
  1046. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  1047. rc = zoo_wexists(zk, "/watchtest/child2", watcher, ctxLocal,
  1048. &stat);
  1049. CPPUNIT_ASSERT_EQUAL((int)ZNONODE, rc);
  1050. }
  1051. CPPUNIT_ASSERT(ctxLocal->countEvents() == 0);
  1052. stopServer();
  1053. CPPUNIT_ASSERT_MESSAGE(testName, ctxGlobal->waitForDisconnected(zk));
  1054. startServer();
  1055. CPPUNIT_ASSERT_MESSAGE(testName, ctxLocal->waitForConnected(zk));
  1056. CPPUNIT_ASSERT(ctxLocal->countEvents() == 0);
  1057. rc = zoo_set(zk, "/watchtest/child", "1", 1, -1);
  1058. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  1059. struct Stat stat1, stat2;
  1060. rc = zoo_set2(zk, "/watchtest/child", "1", 1, -1, &stat1);
  1061. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  1062. CPPUNIT_ASSERT(stat1.version >= 0);
  1063. rc = zoo_set2(zk, "/watchtest/child", "1", 1, stat1.version, &stat2);
  1064. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  1065. rc = zoo_set(zk, "/watchtest/child", "1", 1, stat2.version);
  1066. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  1067. rc = zoo_create(zk, "/watchtest/child2", "", 0,
  1068. &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0);
  1069. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  1070. CPPUNIT_ASSERT_MESSAGE(testName, waitForEvent(zk, ctxLocal, 5));
  1071. evt_t evt = ctxLocal->getEvent();
  1072. CPPUNIT_ASSERT_EQUAL_MESSAGE(evt.path, ZOO_CHANGED_EVENT, evt.type);
  1073. CPPUNIT_ASSERT_EQUAL(string("/watchtest/child"), evt.path);
  1074. CPPUNIT_ASSERT_MESSAGE(testName, waitForEvent(zk, ctxLocal, 5));
  1075. // The create will trigget the get children and the
  1076. // exists watches
  1077. evt = ctxLocal->getEvent();
  1078. CPPUNIT_ASSERT_EQUAL_MESSAGE(evt.path, ZOO_CREATED_EVENT, evt.type);
  1079. CPPUNIT_ASSERT_EQUAL(string("/watchtest/child2"), evt.path);
  1080. CPPUNIT_ASSERT_MESSAGE(testName, waitForEvent(zk, ctxLocal, 5));
  1081. evt = ctxLocal->getEvent();
  1082. CPPUNIT_ASSERT_EQUAL_MESSAGE(evt.path, ZOO_CHILD_EVENT, evt.type);
  1083. CPPUNIT_ASSERT_EQUAL(string("/watchtest"), evt.path);
  1084. // Make sure Pings are giving us problems
  1085. sleep(5);
  1086. CPPUNIT_ASSERT(ctxLocal->countEvents() == 0);
  1087. stopServer();
  1088. CPPUNIT_ASSERT_MESSAGE(testName, ctxGlobal->waitForDisconnected(zk));
  1089. startServer();
  1090. CPPUNIT_ASSERT_MESSAGE(testName, ctxGlobal->waitForConnected(zk));
  1091. if (isGlobal) {
  1092. testName = "GlobalTest";
  1093. rc = zoo_get_children(zk, "/watchtest", 1, &strings);
  1094. deallocate_String_vector(&strings);
  1095. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  1096. blen = sizeof(buf);
  1097. rc = zoo_get(zk, "/watchtest/child", 1, buf, &blen, &stat);
  1098. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  1099. rc = zoo_exists(zk, "/watchtest/child2", 1, &stat);
  1100. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  1101. } else {
  1102. testName = "LocalTest";
  1103. rc = zoo_wget_children(zk, "/watchtest", watcher, ctxLocal,
  1104. &strings);
  1105. deallocate_String_vector(&strings);
  1106. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  1107. blen = sizeof(buf);
  1108. rc = zoo_wget(zk, "/watchtest/child", watcher, ctxLocal,
  1109. buf, &blen, &stat);
  1110. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  1111. rc = zoo_wexists(zk, "/watchtest/child2", watcher, ctxLocal,
  1112. &stat);
  1113. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  1114. }
  1115. zoo_delete(zk, "/watchtest/child2", -1);
  1116. CPPUNIT_ASSERT_MESSAGE(testName, waitForEvent(zk, ctxLocal, 5));
  1117. evt = ctxLocal->getEvent();
  1118. CPPUNIT_ASSERT_EQUAL_MESSAGE(evt.path, ZOO_DELETED_EVENT, evt.type);
  1119. CPPUNIT_ASSERT_EQUAL(string("/watchtest/child2"), evt.path);
  1120. CPPUNIT_ASSERT_MESSAGE(testName, waitForEvent(zk, ctxLocal, 5));
  1121. evt = ctxLocal->getEvent();
  1122. CPPUNIT_ASSERT_EQUAL_MESSAGE(evt.path, ZOO_CHILD_EVENT, evt.type);
  1123. CPPUNIT_ASSERT_EQUAL(string("/watchtest"), evt.path);
  1124. stopServer();
  1125. CPPUNIT_ASSERT_MESSAGE(testName, ctxGlobal->waitForDisconnected(zk));
  1126. startServer();
  1127. CPPUNIT_ASSERT_MESSAGE(testName, ctxLocal->waitForConnected(zk));
  1128. zoo_delete(zk, "/watchtest/child", -1);
  1129. zoo_delete(zk, "/watchtest", -1);
  1130. CPPUNIT_ASSERT_MESSAGE(testName, waitForEvent(zk, ctxLocal, 5));
  1131. evt = ctxLocal->getEvent();
  1132. CPPUNIT_ASSERT_EQUAL_MESSAGE(evt.path, ZOO_DELETED_EVENT, evt.type);
  1133. CPPUNIT_ASSERT_EQUAL(string("/watchtest/child"), evt.path);
  1134. // Make sure nothing is straggling
  1135. sleep(1);
  1136. CPPUNIT_ASSERT(ctxLocal->countEvents() == 0);
  1137. }
  1138. void testWatcherAutoResetWithGlobal()
  1139. {
  1140. {
  1141. watchctx_t ctx;
  1142. zhandle_t *zk = createClient(&ctx);
  1143. int rc = zoo_create(zk, "/testarwg", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0);
  1144. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  1145. rc = zoo_create(zk, "/testarwg/arwg", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0);
  1146. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  1147. }
  1148. {
  1149. watchctx_t ctx;
  1150. zhandle_t *zk = createchClient(&ctx, "127.0.0.1:22181/testarwg/arwg");
  1151. testWatcherAutoReset(zk, &ctx, &ctx);
  1152. }
  1153. }
  1154. void testWatcherAutoResetWithLocal()
  1155. {
  1156. {
  1157. watchctx_t ctx;
  1158. zhandle_t *zk = createClient(&ctx);
  1159. int rc = zoo_create(zk, "/testarwl", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0);
  1160. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  1161. rc = zoo_create(zk, "/testarwl/arwl", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0);
  1162. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  1163. }
  1164. {
  1165. watchctx_t ctx;
  1166. watchctx_t lctx;
  1167. zhandle_t *zk = createchClient(&ctx, "127.0.0.1:22181/testarwl/arwl");
  1168. testWatcherAutoReset(zk, &ctx, &lctx);
  1169. }
  1170. }
  1171. void testLastZxid() {
  1172. // ZOOKEEPER-1417: Test that c-client only update last zxid upon
  1173. // receiving request response only.
  1174. const int timeout = 5000;
  1175. int rc;
  1176. watchctx_t ctx1, ctx2;
  1177. zhandle_t *zk1 = createClient(&ctx1);
  1178. zhandle_t *zk2 = createClient(&ctx2);
  1179. CPPUNIT_ASSERT(zk1);
  1180. CPPUNIT_ASSERT(zk2);
  1181. int64_t original = zk2->last_zxid;
  1182. // Create txn to increase system zxid
  1183. rc = zoo_create(zk1, "/lastzxid", "", 0,
  1184. &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0);
  1185. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  1186. // This should be enough time for zk2 to receive ping request
  1187. usleep(timeout/2 * 1000);
  1188. // check that zk1's last zxid is updated
  1189. struct Stat stat;
  1190. rc = zoo_exists(zk1, "/lastzxid", 0, &stat);
  1191. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  1192. CPPUNIT_ASSERT_EQUAL((int64_t) zk1->last_zxid, stat.czxid);
  1193. // zk2's last zxid should remain the same
  1194. CPPUNIT_ASSERT_EQUAL(original, (int64_t) zk2->last_zxid);
  1195. // Perform read and also register a watch
  1196. rc = zoo_wexists(zk2, "/lastzxid", watcher, &ctx2, &stat);
  1197. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  1198. int64_t updated = zk2->last_zxid;
  1199. // check that zk2's last zxid is updated
  1200. CPPUNIT_ASSERT_EQUAL(updated, stat.czxid);
  1201. // Increment system zxid again
  1202. rc = zoo_set(zk1, "/lastzxid", NULL, -1, -1);
  1203. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  1204. // Wait for zk2 to get watch event
  1205. CPPUNIT_ASSERT(waitForEvent(zk2, &ctx2, 5));
  1206. // zk2's last zxid should remain the same
  1207. CPPUNIT_ASSERT_EQUAL(updated, (int64_t) zk2->last_zxid);
  1208. }
  1209. static void watcher_rw(zhandle_t *zh,
  1210. int type,
  1211. int state,
  1212. const char *path,
  1213. void *ctx) {
  1214. count++;
  1215. }
  1216. static void watcher_rw2(zhandle_t *zh,
  1217. int type,
  1218. int state,
  1219. const char *path,
  1220. void *ctx) {
  1221. count++;
  1222. }
  1223. void testRemoveWatchers() {
  1224. const char *path = "/something";
  1225. char buf[1024];
  1226. int blen = sizeof(buf);
  1227. int rc;
  1228. watchctx_t ctx;
  1229. zhandle_t *zk;
  1230. /* setup path */
  1231. zk = createClient(&ctx);
  1232. CPPUNIT_ASSERT(zk);
  1233. rc = zoo_create(zk, path, "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0);
  1234. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  1235. rc = zoo_create(zk, "/something2", "", 0,
  1236. &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0);
  1237. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  1238. /* remove all watchers */
  1239. count = 0;
  1240. rc = zoo_wget(zk, path, watcher_rw, NULL, buf, &blen, NULL);
  1241. rc = zoo_wget(zk, path, watcher_rw2, NULL, buf, &blen, NULL);
  1242. rc = zoo_remove_all_watches(zk, path, ZWATCHTYPE_ANY, 0);
  1243. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  1244. rc = zoo_set(zk, path, "nowatch", 7, -1);
  1245. CPPUNIT_ASSERT(count == 0);
  1246. /* remove a specific watcher before it's added (should fail) */
  1247. rc = zoo_remove_watches(zk, path, ZWATCHTYPE_DATA,
  1248. watcher_rw, NULL, 0);
  1249. CPPUNIT_ASSERT_EQUAL((int)ZNOWATCHER, rc);
  1250. /* now add a specific watcher and then remove it */
  1251. rc = zoo_wget(zk, path, watcher_rw, NULL,
  1252. buf, &blen, NULL);
  1253. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  1254. rc = zoo_remove_watches(zk, path, ZWATCHTYPE_DATA,
  1255. watcher_rw, NULL, 0);
  1256. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  1257. /* ditto for children watcher */
  1258. rc = zoo_remove_watches(zk, path, ZWATCHTYPE_CHILD,
  1259. watcher_rw, NULL, 0);
  1260. CPPUNIT_ASSERT_EQUAL((int)ZNOWATCHER, rc);
  1261. struct String_vector str_vec = {0, NULL};
  1262. rc = zoo_wget_children(zk, path, watcher_rw, NULL,
  1263. &str_vec);
  1264. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  1265. rc = zoo_remove_watches(zk, path, ZWATCHTYPE_CHILD,
  1266. watcher_rw, NULL, 0);
  1267. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  1268. /* add a watch, stop the server, and have remove fail */
  1269. rc = zoo_wget(zk, path, watcher_rw, NULL,
  1270. buf, &blen, NULL);
  1271. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  1272. stopServer();
  1273. ctx.waitForDisconnected(zk);
  1274. rc = zoo_remove_watches(zk, path, ZWATCHTYPE_DATA,
  1275. watcher_rw, NULL, 0);
  1276. CPPUNIT_ASSERT_EQUAL((int)ZCONNECTIONLOSS, rc);
  1277. zookeeper_close(zk);
  1278. /* bring the server back */
  1279. startServer();
  1280. zk = createClient(&ctx);
  1281. /* add a watch, stop the server, and remove it locally */
  1282. void* ctx1=(void*)0x1;
  1283. void* ctx2=(void*)0x2;
  1284. rc = zoo_wget(zk, path, watcher_rw, ctx1,
  1285. buf, &blen, NULL);
  1286. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  1287. rc = zoo_wget(zk, "/something2", watcher_rw, ctx2,
  1288. buf, &blen, NULL);
  1289. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  1290. stopServer();
  1291. rc = zoo_remove_watches(zk, path, ZWATCHTYPE_DATA,
  1292. watcher_rw, ctx1, 1);
  1293. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  1294. rc = zoo_remove_watches(zk, path, ZWATCHTYPE_DATA,
  1295. watcher_rw, ctx1, 1);
  1296. CPPUNIT_ASSERT_EQUAL((int)ZNOWATCHER, rc);
  1297. rc = zoo_remove_watches(zk, "/something2", ZWATCHTYPE_DATA,
  1298. watcher_rw, ctx2, 1);
  1299. CPPUNIT_ASSERT_EQUAL((int)ZOK,rc);
  1300. }
  1301. };
  1302. volatile int Zookeeper_simpleSystem::count;
  1303. zhandle_t *Zookeeper_simpleSystem::async_zk;
  1304. const char Zookeeper_simpleSystem::hostPorts[] = "127.0.0.1:22181";
  1305. CPPUNIT_TEST_SUITE_REGISTRATION(Zookeeper_simpleSystem);
  1306. #endif