forked from rsocket/rsocket-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseSubscriber.cpp
More file actions
112 lines (95 loc) · 2.99 KB
/
BaseSubscriber.cpp
File metadata and controls
112 lines (95 loc) · 2.99 KB
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
// Copyright 2004-present Facebook. All Rights Reserved.
#include "tck-test/BaseSubscriber.h"
#include <thread>
#include <folly/Format.h>
using namespace folly;
namespace rsocket {
namespace tck {
void BaseSubscriber::awaitTerminalEvent() {
std::unique_lock<std::mutex> lock(mutex_);
if (!terminatedCV_.wait_for(lock, std::chrono::seconds(5), [&] {
return completed_ || errored_;
})) {
throw std::runtime_error("Timed out while waiting for terminating event");
}
}
void BaseSubscriber::awaitAtLeast(int numItems) {
std::unique_lock<std::mutex> lock(mutex_);
if (!valuesCV_.wait_for(lock, std::chrono::seconds(5), [&] {
return valuesCount_ >= numItems;
})) {
throw std::runtime_error("Timed out while waiting for items");
}
}
void BaseSubscriber::awaitNoEvents(int waitTime) {
int valuesCount = valuesCount_;
bool completed = completed_;
bool errored = errored_;
/* sleep override */
std::this_thread::sleep_for(std::chrono::milliseconds(waitTime));
if (valuesCount != valuesCount_ || completed != completed_ ||
errored != errored_) {
throw std::runtime_error(
folly::sformat("Events occured within {}ms", waitTime));
}
}
void BaseSubscriber::assertNoErrors() {
if (errored_) {
throw std::runtime_error("Subscription completed with unexpected errors");
}
}
void BaseSubscriber::assertError() {
if (!errored_) {
throw std::runtime_error("Subscriber did not receive onError");
}
}
void BaseSubscriber::assertValues(
const std::vector<std::pair<std::string, std::string>>& values) {
assertValueCount(values.size());
std::unique_lock<std::mutex> lock(mutex_);
for (size_t i = 0; i < values.size(); i++) {
if (values_[i] != values[i]) {
throw std::runtime_error(folly::sformat(
"Unexpected element {}:{}. Expected element {}:{}",
values_[i].first,
values_[i].second,
values[i].first,
values[i].second));
}
}
}
void BaseSubscriber::assertValueCount(size_t valueCount) {
std::unique_lock<std::mutex> lock(mutex_);
if (values_.size() != valueCount) {
throw std::runtime_error(folly::sformat(
"Did not receive expected number of values! Expected={} Actual={}",
valueCount,
values_.size()));
}
}
void BaseSubscriber::assertReceivedAtLeast(size_t valueCount) {
std::unique_lock<std::mutex> lock(mutex_);
if (values_.size() < valueCount) {
throw std::runtime_error(folly::sformat(
"Did not receive the minimum number of values! Expected={} Actual={}",
valueCount,
values_.size()));
}
}
void BaseSubscriber::assertCompleted() {
if (!completed_) {
throw std::runtime_error("Subscriber did not completed");
}
}
void BaseSubscriber::assertNotCompleted() {
if (completed_) {
throw std::runtime_error("Subscriber unexpectedly completed");
}
}
void BaseSubscriber::assertCanceled() {
if (!canceled_) {
throw std::runtime_error("Subscription should be canceled");
}
}
} // tck
} // reactivesocket