forked from ydb-platform/nbs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_conductor.cpp
127 lines (101 loc) · 2.56 KB
/
test_conductor.cpp
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
#include "test_conductor.h"
#include <library/cpp/neh/rpc.h>
#include <util/generic/hash_set.h>
#include <util/string/printf.h>
#include <util/system/mutex.h>
namespace NCloud::NBlockStore::NDiscovery {
////////////////////////////////////////////////////////////////////////////////
struct TFakeConductor::TImpl
{
ui16 Port;
NNeh::IServicesRef Loop;
TVector<THostInfo> HostInfo;
THashSet<TString> DroppedGroups;
TVector<std::shared_ptr<NNeh::IRequest>> DroppedRequests;
TMutex Lock;
TImpl(ui16 port)
: Port(port)
{
}
~TImpl()
{
if (Loop) {
with_lock (Lock) {
for (auto& droppedRequest : DroppedRequests) {
droppedRequest->SendError(NNeh::IRequest::ServiceUnavailable);
}
DroppedRequests.clear();
}
Loop->SyncStopFork();
}
}
void ServeRequest(const NNeh::IRequestRef& req)
{
NNeh::TDataSaver ds;
TStringBuf apiUrl, group;
req->Service().RSplit('/', apiUrl, group);
if (group == "broken_group") {
req->SendError(NNeh::IRequest::ServiceUnavailable);
} else {
with_lock (Lock) {
if (DroppedGroups.contains(group)) {
DroppedRequests.emplace_back(req.Release());
return;
}
for (const auto& hostInfo : HostInfo) {
if (hostInfo.Group == group) {
ds << hostInfo.Host << Endl;
}
}
}
req->SendReply(ds);
}
}
void PreStart()
{
Loop = NNeh::CreateLoop();
Loop->Add(Sprintf("http://*:%u/*", Port), *this);
}
void ForkStart()
{
PreStart();
Loop->ForkLoop(1);
}
void Start()
{
PreStart();
Loop->Loop(1);
}
};
TFakeConductor::TFakeConductor(ui16 port)
: Impl(new TImpl(port))
{
}
TFakeConductor::~TFakeConductor()
{
}
void TFakeConductor::ForkStart()
{
Impl->ForkStart();
}
void TFakeConductor::Start()
{
Impl->Start();
}
void TFakeConductor::SetHostInfo(TVector<THostInfo> hostInfo)
{
with_lock (Impl->Lock) {
Impl->HostInfo = std::move(hostInfo);
}
}
void TFakeConductor::SetDrop(TString group, bool drop)
{
with_lock (Impl->Lock) {
if (drop) {
Impl->DroppedGroups.emplace(std::move(group));
} else {
Impl->DroppedGroups.erase(group);
}
}
}
} // namespace NCloud::NBlockStore::NDiscovery