userver: userver/utest/stress.hpp Source File
Loading...
Searching...
No Matches
stress.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/utest/stress.hpp
4/// @brief @copybrief utest::StressLoop
5
6#include <chrono>
7
8#include <userver/engine/deadline.hpp>
9
10USERVER_NAMESPACE_BEGIN
11
12namespace utest {
13
14/// @brief Helper for stress tests for watching for
15/// "OK, we have been running for enough time, let's stop" event.
16///
17/// Example:
18/// @code
19/// for (auto _: utest::StressLoop()) {
20/// ...
21/// }
22/// @endcode
24public:
25 struct Iterator {
26 StressLoop& loop;
27
28 struct Value {
29 ~Value() {
30 (void)0; // force non-trivial destructor
31 }
32 };
33
34 void operator++() {}
35 Value operator*() { return {}; }
36 bool operator==(const Iterator&) const { return loop.ShouldStop(); }
37 bool operator!=(const Iterator&) const { return !loop.ShouldStop(); }
38 };
39
40 Iterator begin() { return {*this}; }
41
42 Iterator end() { return {*this}; }
43
44 bool ShouldStop() const { return deadline_.IsReached(); }
45
46private:
47 static constexpr std::chrono::seconds kMaxStressTestWaitTime{30};
48
49 engine::Deadline deadline_{engine::Deadline::FromDuration(kMaxStressTestWaitTime)};
50};
51
52} // namespace utest
53
54USERVER_NAMESPACE_END