aboutsummaryrefslogtreecommitdiffstats
path: root/storage/src/tests/common/testnodestateupdater.h
blob: e5418c238d5f1190c10199a10cbf588d5b7caf45 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
/**
 * \class storage::TestNodeStateUpdater
 * \ingroup common
 *
 * \brief Test implementation of the node state updater.
 */

#pragma once

#include <vespa/storage/common/nodestateupdater.h>
#include <mutex>

namespace storage::lib {
    class ClusterState;
    class ClusterStateBundle;
}
namespace storage {

struct TestNodeStateUpdater : public NodeStateUpdater
{
    mutable std::mutex  _mutex;
    lib::NodeState::CSP _reported;
    lib::NodeState::CSP _current;
    std::shared_ptr<const lib::ClusterStateBundle> _clusterStateBundle;
    std::vector<StateListener*> _listeners;
    size_t _explicit_node_state_reply_send_invocations;
    size_t _requested_almost_immediate_node_state_replies;

public:
    explicit TestNodeStateUpdater(const lib::NodeType& type);
    ~TestNodeStateUpdater() override;

    lib::NodeState::CSP getReportedNodeState() const override { std::lock_guard guard(_mutex); return _reported; }
    lib::NodeState::CSP getCurrentNodeState() const override { return _current; }
    std::shared_ptr<const lib::ClusterStateBundle> getClusterStateBundle() const override;
    void addStateListener(StateListener& s) override { _listeners.push_back(&s); }
    void removeStateListener(StateListener&) override {}
    Lock::SP grabStateChangeLock() override { return std::make_shared<Lock>(); }
    void setReportedNodeState(const lib::NodeState& state) override {
        std::lock_guard guard(_mutex);
        _reported = std::make_shared<lib::NodeState>(state);
    }
    void immediately_send_get_node_state_replies() override {
        ++_explicit_node_state_reply_send_invocations;
    }

    void request_almost_immediate_node_state_replies() override {
        ++_requested_almost_immediate_node_state_replies;
    }

    void setCurrentNodeState(const lib::NodeState& state) {
        _current = std::make_shared<lib::NodeState>(state);
    }

    void setClusterState(std::shared_ptr<const lib::ClusterState> c);
    void setClusterStateBundle(std::shared_ptr<const lib::ClusterStateBundle> clusterStateBundle);

    size_t explicit_node_state_reply_send_invocations() const noexcept {
        return _explicit_node_state_reply_send_invocations;
    }

    size_t requested_almost_immediate_node_state_replies() const noexcept {
        return _requested_almost_immediate_node_state_replies;
    }
};

} // storage