aboutsummaryrefslogtreecommitdiffstats
path: root/documentapi/src/tests/policies/testframe.h
blob: a609e4b5b5c97f9ed9eb91ae37f08d580a315321 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include <vespa/documentapi/messagebus/documentprotocol.h>
#include <vespa/messagebus/messagebus.h>
#include <vespa/messagebus/network/identity.h>
#include <vespa/messagebus/network/inetwork.h>
#include <vespa/messagebus/testlib/receptor.h>
#include <vespa/messagebus/testlib/slobrok.h>

using documentapi::string;

class TestFrame : public mbus::IReplyHandler {
private:
    string                            _identity;
    std::shared_ptr<mbus::Slobrok>    _slobrok;
    std::shared_ptr<mbus::INetwork>   _net;
    std::shared_ptr<mbus::MessageBus> _mbus;
    mbus::Message::UP                 _msg;
    mbus::HopSpec                     _hop;
    mbus::Receptor                    _handler;

public:
    /**
     * Convenience typedefs.
     */
    using ReplyMap = std::map<string, uint32_t>;

    /**
     * Create a named test frame.
     *
     * @param identity The identity to use for the server.
     */
    explicit TestFrame(const std::shared_ptr<const document::DocumentTypeRepo> &repo,
                       const string &ident = "anonymous");

    TestFrame &operator=(const TestFrame &) = delete;

    /**
     * Create a test frame running on the same slobrok and mbus as another.
     *
     * @param frame The frame whose internals to share.
     */
    TestFrame(TestFrame &frame);

    ~TestFrame() override;

    /**
     * Routes the contained message based on the current setup, and returns the leaf send contexts.
     *
     * @param selected    The list to add the selected recipients to.
     * @param numExpected The expected number of contexts.
     * @return True if everything was ok.
     */
    bool select(std::vector<mbus::RoutingNode*> &selected, uint32_t numExpected);

    /**
     * Ensures that the current setup selects a given set of routes.
     *
     * @param expected A list of expected route leaf nodes.
     * @return True if everything was ok.
     */
    bool testSelect(const std::vector<string> &expected);

    /**
     * This is a convenience method for invoking {@link #assertMerge(std::map,std::vector,std::vector)} with
     * no expected value.
     *
     * @param replies        The errors to set in the leaf node replies.
     * @param expectedErrors The list of expected errors in the merged reply.
     * @return True if everything was ok.
     */
    bool testMergeError(const ReplyMap &replies, const std::vector<uint32_t> &expectedErrors);

    /**
     * This is a convenience method for invoking {@link #assertMerge(std::map,std::vector,std::vector)} with
     * no expected errors.
     *
     * @param replies       The errors to set in the leaf node replies.
     * @param allowedValues The list of allowed values in the final reply.
     * @return True if everything was ok.
     */
    bool testMergeOk(const ReplyMap &replies, const std::vector<string> &allowedValues);

    /**
     * Ensures that the current setup generates as many leaf nodes as there are members of the errors argument. Each
     * error is then given one of these errors, and the method then ensures that the single returned reply contains the
     * given list of expected errors. Finally, if the expected value argument is non-null, this method ensures that the
     * reply is a SimpleReply whose string value exists in the allowed list.
     *
     * @param replies        The errors to set in the leaf node replies.
     * @param expectedErrors The list of expected errors in the merged reply.
     * @param allowedValues  The list of allowed values in the final reply.
     * @return True if everything was ok.
     */
    bool testMerge(const ReplyMap &replies,
                   const std::vector<uint32_t> &expectedErrors,
                   const std::vector<string> &allowedValues);

    /**
     * Ensures that the current setup chooses a single recipient, and that it merges similarly to how the
     * {@link DocumentProtocol} would merge these.
     *
     * @param recipient The expected recipient.
     * @return True if everything was ok.
     */
    bool testMergeOneReply(const string &recipient);

    /**
     * Ensures that the current setup will choose the two given recipients, and that it merges similarly to how the
     * {@link DocumentProtocol} would merge these.
     *
     * @param recipientOne The first expected recipient.
     * @param recipientTwo The second expected recipient.
     */
    bool testMergeTwoReplies(const string &recipientOne, const string &recipientTwo);

    /**
     * Waits for a given service pattern to resolve to the given number of hits in the local slobrok.
     *
     * @param pattern The pattern to lookup.
     * @param cnt     The number of entries to wait for.
     * @return True if the expected number of entries was found.
     */
    bool waitSlobrok(const string &pattern, uint32_t cnt);

    /**
     * Returns the identity of this frame.
     *
     * @return The ident string.
     */
    const string &getIdentity() { return _identity; }

    /**
     * Returns the private message bus.
     *
     * @return The bus.
     */
    mbus::MessageBus &getMessageBus() { return *_mbus; }

    /**
     * Returns the private network layer.
     *
     * @return The network.
     */
    mbus::INetwork &getNetwork() { return *_net; }

    /**
     * Returns the message being tested.
     *
     * @return The message.
     */
    mbus::Message::UP getMessage() { return std::move(_msg); }

    /**
     * Sets the message being tested.
     *
     * @param msg The message to set.
     */
    mbus::Message::UP setMessage(mbus::Message::UP msg) {
        std::swap(msg, _msg);
        return msg;
    }

    /**
     * Sets the spec of the hop to test with.
     *
     * @param hop The spec to set.
     */
    void setHop(const mbus::HopSpec &hop);

    /**
     * Returns the reply receptor used by this frame. All messages tested are tagged with this receptor, so after a
     * successful select, the receptor should contain a non-null reply.
     *
     * @return The reply receptor.
     */
    mbus::Receptor &getReceptor() { return _handler; }

    void handleReply(mbus::Reply::UP reply) override;
};

class UIntList : public std::vector<uint32_t> {
public:
    UIntList &add(uint32_t err) {
        std::vector<uint32_t>::push_back(err);
        return *this;
    }
};

class StringList : public std::vector<string> {
public:
    StringList &add(const string &val) {
        std::vector<string>::push_back(val);
        return *this;
    }
};