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
10#include <userver/compiler/impl/asan.hpp>
11#include <userver/compiler/impl/tsan.hpp>
12
13USERVER_NAMESPACE_BEGIN
14
15namespace utest {
16
17/// @brief Helper for stress tests for watching for
18/// "OK, we have been running for enough time, let's stop" event.
19///
20/// Example:
21/// @code
22/// for (auto _: utest::StressLoop()) {
23/// ...
24/// }
25/// @endcode
27public:
28 struct Iterator {
29 StressLoop& loop;
30
31 struct Value {
32 ~Value() {
33 (void)0; // force non-trivial destructor
34 }
35 };
36
37 void operator++() {}
38 Value operator*() { return {}; }
39 bool operator==(const Iterator&) const { return loop.ShouldStop(); }
40 bool operator!=(const Iterator&) const { return !loop.ShouldStop(); }
41 };
42
43 Iterator begin() { return {*this}; }
44
45 Iterator end() { return {*this}; }
46
47 bool ShouldStop() const { return deadline_.IsReached(); }
48
49private:
50#if USERVER_IMPL_HAS_TSAN || USERVER_IMPL_HAS_ASAN
51 // Too many iterations with sanitizers may consume too much virtual memory and crach the application
52 static constexpr std::chrono::milliseconds kMaxStressTestWaitTime{300};
53#else
54 static constexpr std::chrono::seconds kMaxStressTestWaitTime{30};
55#endif
56
57 engine::Deadline deadline_{engine::Deadline::FromDuration(kMaxStressTestWaitTime)};
58};
59
60} // namespace utest
61
62USERVER_NAMESPACE_END