TestReadOnlyClient.cc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 <sys/socket.h>
  21. #include <unistd.h>
  22. #include <zookeeper.h>
  23. #include "Util.h"
  24. #include "WatchUtil.h"
  25. #ifdef THREADED
  26. class Zookeeper_readOnly : public CPPUNIT_NS::TestFixture {
  27. CPPUNIT_TEST_SUITE(Zookeeper_readOnly);
  28. CPPUNIT_TEST(testReadOnly);
  29. CPPUNIT_TEST_SUITE_END();
  30. static void watcher(zhandle_t* zh, int type, int state,
  31. const char* path, void* v) {
  32. watchctx_t *ctx = (watchctx_t*)v;
  33. if (state==ZOO_CONNECTED_STATE || state==ZOO_READONLY_STATE) {
  34. ctx->connected = true;
  35. } else {
  36. ctx->connected = false;
  37. }
  38. if (type != ZOO_SESSION_EVENT) {
  39. evt_t evt;
  40. evt.path = path;
  41. evt.type = type;
  42. ctx->putEvent(evt);
  43. }
  44. }
  45. FILE *logfile;
  46. public:
  47. Zookeeper_readOnly() {
  48. logfile = openlogfile("Zookeeper_readOnly");
  49. }
  50. ~Zookeeper_readOnly() {
  51. if (logfile) {
  52. fflush(logfile);
  53. fclose(logfile);
  54. logfile = 0;
  55. }
  56. }
  57. void setUp() {
  58. zoo_set_log_stream(logfile);
  59. }
  60. void startReadOnly() {
  61. char cmd[1024];
  62. sprintf(cmd, "%s startReadOnly", ZKSERVER_CMD);
  63. CPPUNIT_ASSERT(system(cmd) == 0);
  64. }
  65. void stopPeer() {
  66. char cmd[1024];
  67. sprintf(cmd, "%s stop", ZKSERVER_CMD);
  68. CPPUNIT_ASSERT(system(cmd) == 0);
  69. }
  70. void testReadOnly() {
  71. startReadOnly();
  72. watchctx_t watch;
  73. zhandle_t* zh = zookeeper_init("localhost:22181",
  74. watcher,
  75. 10000,
  76. NULL,
  77. &watch,
  78. ZOO_READONLY);
  79. watch.zh = zh;
  80. CPPUNIT_ASSERT(zh != 0);
  81. sleep(1);
  82. int len = 1024;
  83. char buf[len];
  84. int res = zoo_get(zh, "/", 0, buf, &len, 0);
  85. CPPUNIT_ASSERT_EQUAL((int)ZOK, res);
  86. char path[1024];
  87. res = zoo_create(zh, "/test", buf, 10, &ZOO_OPEN_ACL_UNSAFE, 0, path,
  88. 512);
  89. CPPUNIT_ASSERT_EQUAL((int)ZNOTREADONLY, res);
  90. stopPeer();
  91. }
  92. };
  93. CPPUNIT_TEST_SUITE_REGISTRATION(Zookeeper_readOnly);
  94. #endif