Skip to content

Commit abfbb57

Browse files
Test waiting on full channel
1 parent e032524 commit abfbb57

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

.github/workflows/cmake.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,11 @@ jobs:
102102

103103
- name: Build
104104
working-directory: ${{github.workspace}}/build
105-
run: cmake --build . --config Debug --target tests
105+
run: cmake --build . --config Debug --target tests -j
106106

107107
- name: Test
108108
working-directory: ${{github.workspace}}/build
109-
run: ctest -C Debug --verbose -R channel_test
109+
run: ctest -C Debug --verbose -R channel_test -j
110110

111111
- name: Upload coverage reports to Codecov
112112
uses: codecov/codecov-action@v5

tests/channel_test.cpp

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,9 @@ TEST(ChannelTest, Iterator)
148148

149149
TEST(ChannelTest, Multithreading)
150150
{
151-
const int numbers = 10000;
152-
const std::int64_t expected = 50005000;
153-
constexpr std::size_t kThreadsToReadFrom = 100;
151+
const int numbers = 1000;
152+
const std::int64_t expected = 500500;
153+
constexpr std::size_t kThreadsToReadFrom = 50;
154154

155155
msd::channel<int> channel{10};
156156

@@ -176,6 +176,8 @@ TEST(ChannelTest, Multithreading)
176176

177177
sum_numbers += out;
178178
++count_numbers;
179+
180+
std::this_thread::sleep_for(std::chrono::milliseconds(1));
179181
}
180182
--wait_counter;
181183
cond_wait.notify_one();
@@ -186,16 +188,19 @@ TEST(ChannelTest, Multithreading)
186188
threads.emplace_back(std::thread{worker});
187189
}
188190

189-
// Send numbers to channel
190-
for (int i = 1; i <= numbers; ++i) {
191-
channel << i;
191+
auto sender = [&] {
192+
// Send numbers to channel
193+
for (int i = 1; i <= numbers; ++i) {
194+
channel << i;
192195

193-
// Notify threads than then can start reading
194-
if (!ready_to_read) {
195-
ready_to_read = true;
196-
cond_read.notify_all();
196+
// Notify threads than then can start reading
197+
if (!ready_to_read) {
198+
ready_to_read = true;
199+
cond_read.notify_all();
200+
}
197201
}
198-
}
202+
};
203+
threads.emplace_back(std::thread{sender});
199204

200205
// Wait until all items have been read
201206
std::unique_lock<std::mutex> lock{mtx_wait};

0 commit comments

Comments
 (0)