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
|
#include "counters.h"
#include <library/cpp/testing/unittest/registar.h>
#include <util/system/event.h>
#include <util/system/thread.h>
using namespace NMonitoring;
Y_UNIT_TEST_SUITE(TDynamicCountersContentionTest) {
Y_UNIT_TEST(EnsureNonlocking) {
TDynamicCounterPtr counters = MakeIntrusive<TDynamicCounters>();
class TConsumer : public ICountableConsumer {
TAutoEvent Ev;
TAutoEvent Response;
TDynamicCounterPtr Counters;
TThread Thread;
public:
TConsumer(TDynamicCounterPtr counters)
: Counters(counters)
, Thread(std::bind(&TConsumer::ThreadFunc, this))
{
Thread.Start();
}
~TConsumer() override {
Thread.Join();
}
void OnCounter(const TString& /*labelName*/, const TString& /*labelValue*/, const TCounterForPtr* /*counter*/) override {
Ev.Signal();
Response.Wait();
}
void OnHistogram(const TString& /*labelName*/, const TString& /*labelValue*/, IHistogramSnapshotPtr /*snapshot*/, bool /*derivative*/) override {
}
void OnGroupBegin(const TString& /*labelName*/, const TString& /*labelValue*/, const TDynamicCounters* /*group*/) override {
}
void OnGroupEnd(const TString& /*labelName*/, const TString& /*labelValue*/, const TDynamicCounters* /*group*/) override {
}
private:
void ThreadFunc() {
// acts like a coroutine
Ev.Wait();
auto ctr = Counters->GetSubgroup("label", "value")->GetCounter("name");
Y_ABORT_UNLESS(*ctr == 42);
Response.Signal();
}
};
auto ctr = counters->GetSubgroup("label", "value")->GetCounter("name");
*ctr = 42;
TConsumer consumer(counters);
counters->Accept({}, {}, consumer);
}
}
|