async_test.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 Pilz GmbH & Co. KG
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ASYNC_TEST_H
18 #define ASYNC_TEST_H
19 
20 #include <mutex>
21 #include <condition_variable>
22 
23 #include <gtest/gtest.h>
24 
25 namespace testing
26 {
27 
64 class AsyncTest
65 {
66  public:
73  void triggerClearEvent(std::string event);
74 
81  void barricade(std::string clear_event);
82 
89  void barricade(std::initializer_list<std::string> clear_events);
90 
91  protected:
92  std::mutex m_;
93  std::condition_variable cv_;
94  std::set<std::string> clear_events_ {};
95  std::set<std::string> waitlist_ {};
96 };
97 
98 // for better readability in tests
99 #define BARRIER(...) barricade(__VA_ARGS__)
100 
101 #define ACTION_OPEN_BARRIER(str) ::testing::InvokeWithoutArgs([this](void){this->triggerClearEvent(str); return true;})
102 #define ACTION_OPEN_BARRIER_VOID(str) ::testing::InvokeWithoutArgs([this](void){this->triggerClearEvent(str);})
103 }
104 
105 #endif // ASYNC_TEST_H
void barricade(std::string clear_event)
Will block until the event given by clear_event is triggered. Unblocks immediately, if the event was triggered in advance.
Definition: async_test.cpp:26
std::condition_variable cv_
Definition: async_test.h:93
void triggerClearEvent(std::string event)
Triggeres a clear event. If a call to barricade is currently pending it will unblock as soon as all c...
Definition: async_test.cpp:52
std::set< std::string > clear_events_
Definition: async_test.h:94
Test class that allows the handling of asynchronous test objects.
Definition: async_test.h:64
std::mutex m_
Definition: async_test.h:92
std::set< std::string > waitlist_
Definition: async_test.h:95


pilz_testutils
Author(s):
autogenerated on Tue Feb 2 2021 03:50:13