summaryrefslogtreecommitdiffstats
path: root/filedistribution/src/tests/zkfacade/test-zkfacade.cpp
blob: 46db660d4523cb3892d14c9d4873972c192a9021 (plain) (blame)
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
155
156
157
158
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MAIN
#define BOOST_TEST_MODULE zkfacade test
#include <vespa/fastos/fastos.h>
#include <boost/test/unit_test.hpp>

#include <iostream>

#include <boost/thread/barrier.hpp>
#include <boost/checked_delete.hpp>

#include <vespa/filedistribution/common/componentsdeleter.h>
#include <vespa/filedistribution/model/zkfacade.h>

#include <zookeeper/zookeeper.h>


using namespace std::literals;
using namespace filedistribution;

namespace {


struct Watcher : public ZKFacade::NodeChangedWatcher {
    boost::barrier _barrier;

    Watcher() :
        _barrier(2) {}

    void operator()() override {
        _barrier.wait();
    }
};

struct Fixture {
    ComponentsDeleter _componentsDeleter;
    std::shared_ptr<ZKFacade> zk;
    Path testNode;

    Fixture() {
        zoo_set_debug_level(ZOO_LOG_LEVEL_WARN);
        zk = _componentsDeleter.track(new ZKFacade("test1-tonyv:2181", false));

        testNode = "/test-node";
        zk->removeIfExists(testNode);
    }

    ~Fixture() {
        if (zk) {
            zk->removeIfExists(testNode);
        }
    }
};

} //anonymous namespace


BOOST_FIXTURE_TEST_SUITE(ZKFacadeTests, Fixture)

BOOST_AUTO_TEST_CASE(hasNode)
{
    zk->setData(testNode, "", 0);
    BOOST_CHECK(zk->hasNode(testNode));

    zk->remove(testNode);
    BOOST_CHECK(!zk->hasNode(testNode));
}

BOOST_AUTO_TEST_CASE(getValidZKServers)
{
    BOOST_CHECK_EQUAL("localhost:22", ZKFacade::getValidZKServers("localhost:22", false));
    BOOST_CHECK_EQUAL("localhost:22", ZKFacade::getValidZKServers("localhost:22", true));
    BOOST_CHECK_EQUAL("idonotexist:22", ZKFacade::getValidZKServers("idonotexist:22", false));
    BOOST_CHECK_EQUAL("", ZKFacade::getValidZKServers("idonotexist:22", true));
    BOOST_CHECK_EQUAL("localhost:22,idonotexist:22", ZKFacade::getValidZKServers("localhost:22,idonotexist:22", false));
    BOOST_CHECK_EQUAL("localhost:22", ZKFacade::getValidZKServers("localhost:22,idonotexist:22", true));
    BOOST_CHECK_EQUAL("idonotexist:22,localhost:22", ZKFacade::getValidZKServers("idonotexist:22,localhost:22", false));
    BOOST_CHECK_EQUAL("localhost:22", ZKFacade::getValidZKServers("idonotexist:22,localhost:22", true));
}

BOOST_AUTO_TEST_CASE(hasNodeNotification)
{
    std::shared_ptr<Watcher> watcher(new Watcher);

    zk->hasNode(testNode, watcher);
    zk->setData(testNode, "", 0);
    watcher->_barrier.wait();

    //after the notification has returned, the watcher must no longer reside in watchers map.
    for (int i=0; i<20 && !watcher.unique(); ++i)  {
        std::this_thread::sleep_for(100ms);
    }
    BOOST_CHECK(watcher.unique());
}

BOOST_AUTO_TEST_CASE(getAndSetData)
{
    std::string inputString = "test data.";
    Buffer inputBuffer(inputString.begin(), inputString.end());

    zk->setData(testNode, inputBuffer);

    Buffer outputBuffer(zk->getData(testNode));
    std::string outputString(outputBuffer.begin(), outputBuffer.end());

    BOOST_CHECK(outputString == inputString);

    outputString = zk->getString(testNode);
    BOOST_CHECK(outputString == inputString);
}

BOOST_AUTO_TEST_CASE(setDataMustExist)
{
    bool mustExist = true;
    BOOST_REQUIRE_THROW(zk->setData(testNode, "", 0, mustExist),  ZKNodeDoesNotExistsException);
}

BOOST_AUTO_TEST_CASE(createSequenceNode)
{
    zk->setData(testNode, "", 0);

    Path prefix = testNode / "prefix";
    zk->createSequenceNode(prefix, "test", 4);
    zk->createSequenceNode(prefix, "test", 4);
    zk->createSequenceNode(prefix, "test", 4);

    std::vector<std::string> children = zk->getChildren(testNode);
    BOOST_CHECK(children.size() == 3);
    BOOST_CHECK(children.begin()->substr(0,6) == "prefix");

    Buffer buffer(zk->getData(testNode / *children.begin()));
    std::string bufferContent(buffer.begin(), buffer.end());

    BOOST_CHECK(bufferContent == "test");
}

BOOST_AUTO_TEST_CASE(retainOnly)
{
    zk->setData(testNode, "", 0);

    zk->setData(testNode / "a", "", 0);
    zk->setData(testNode / "b", "", 0);
    zk->setData(testNode / "c", "", 0);
    zk->setData(testNode / "d", "", 0);

    std::vector<std::string> toRetain;
    toRetain.push_back("a");
    toRetain.push_back("c");

    zk->retainOnly(testNode, toRetain);
    std::vector<std::string> children = zk->getChildren(testNode);

    std::sort(children.begin(), children.end());
    BOOST_CHECK(children == toRetain);
}



BOOST_AUTO_TEST_CASE(addEphemeralNode)
{
    Path ephemeralNode = "/test-ephemeral-node";
    zk->removeIfExists(ephemeralNode);

    //Checked deleter is ok here since we're not installing any watchers
    ZKFacade::SP zk2(new ZKFacade("test1-tonyv:2181", false), boost::checked_deleter<ZKFacade>());
    zk2->addEphemeralNode(ephemeralNode);

    BOOST_CHECK(zk->hasNode(ephemeralNode));
    zk2.reset();
    BOOST_CHECK(!zk->hasNode(ephemeralNode));
}



BOOST_AUTO_TEST_CASE(dataChangedNotification)
{
    std::shared_ptr<Watcher> watcher(new Watcher);

    zk->setData(testNode, "", 0);
    Buffer buffer(zk->getData(testNode, watcher));
    BOOST_CHECK(buffer.size() == 0);

    bool mustExist = true;
    zk->setData(testNode, "test",  4, mustExist);
    watcher->_barrier.wait();
}

BOOST_AUTO_TEST_CASE(getChildrenNotification)
{
    std::shared_ptr<Watcher> watcher(new Watcher);

    zk->setData(testNode, "", 0);
    zk->getChildren(testNode, watcher);

    zk->setData(testNode / "child", "", 0);
    watcher->_barrier.wait();
}

BOOST_AUTO_TEST_CASE(require_that_zkfacade_can_be_deleted_from_callback)
{
    struct DeleteZKFacadeWatcher : public Watcher {
        std::shared_ptr<ZKFacade> _zk;

        DeleteZKFacadeWatcher(const std::shared_ptr<ZKFacade>& zk)
            :_zk(zk)
        {}

        void operator()() override {
            BOOST_CHECK(_zk.use_count() == 2);
            _zk.reset();
            Watcher::operator()();
        }
    };

    std::shared_ptr<Watcher> watcher((Watcher*)new DeleteZKFacadeWatcher(zk));

    zk->setData(testNode, "", 0);
    zk->getData(testNode, watcher);

    ZKFacade* unprotectedZk = zk.get();
    zk.reset();

    unprotectedZk->setData(testNode, "t", 1);
    watcher->_barrier.wait();

    //Must wait longer than the zookeeper_close timeout to catch
    //problems due to closing zookeeper in a zookeeper watcher thread.
    sleep(3);
}

BOOST_AUTO_TEST_SUITE_END()