You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
2.2 KiB
42 lines
2.2 KiB
/* Проверяем настоящую очередь: конкуренция, переполнение, целостность и повторный запуск. */ |
|
#include "../transport/audio_diagnostics.cpp" |
|
#include <cassert> |
|
#include <cstdio> |
|
#include <vector> |
|
|
|
int main() { |
|
debug_config_init(); debug_enable_console(0); |
|
constexpr int producers = 4, perProducer = 20000; |
|
std::vector<bool> seen(producers * perProducer); |
|
std::atomic<int> done{0}; |
|
for (uint64_t i = 0; i < kCapacity; ++i) queue[i].sequence.store(i); |
|
running = true; |
|
// Сначала полное кольцо: producer обязан вернуть управление, потеря учтена. |
|
for (uint64_t i = 0; i < kCapacity + 1; ++i) audio_diag_event(nullptr, AUDIO_DIAG_HOLE, AUDIO_DIAG_READ, i, 7); |
|
assert(lost.load() == 1); |
|
Event e; |
|
for (uint64_t i = 0; i < kCapacity; ++i) { |
|
assert(take(e)); assert(e.value == int64_t(i) && e.detail == 7); |
|
} |
|
assert(!take(e)); lost = 0; |
|
std::vector<std::thread> workers; |
|
for (int p = 0; p < producers; ++p) workers.emplace_back([p, &done]() { |
|
for (int i = 0; i < perProducer; ++i) |
|
audio_diag_event(nullptr, AUDIO_DIAG_HOLE, AUDIO_DIAG_READ, p * perProducer + i, p); |
|
++done; |
|
}); |
|
uint64_t received = 0; |
|
while (done.load() != producers || readPosition != writePosition.load()) { |
|
if (!take(e)) { std::this_thread::yield(); continue; } |
|
assert(e.value >= 0 && e.value < producers * perProducer); |
|
assert(e.kind == AUDIO_DIAG_HOLE && e.stage == AUDIO_DIAG_READ && e.value / perProducer == e.detail); |
|
assert(e.wall && e.cpu && e.tid && !seen[e.value]); |
|
seen[e.value] = true; ++received; |
|
} |
|
for (auto& worker : workers) worker.join(); |
|
assert(received + lost.load() == producers * perProducer); |
|
running = false; |
|
// Lifecycle не оставляет joinable-поток и очищает очередь между сеансами. |
|
for (int i = 0; i < 2; ++i) { audio_diag_start(); audio_diag_event(nullptr, AUDIO_DIAG_OPEN, -1, 1, 48000); audio_diag_stop(); } |
|
puts("test_audio_diagnostics: bounded MPSC, concurrent integrity and restart PASS"); |
|
}
|
|
|