|
| 1 | +#include <atomic> |
| 2 | +#include <catch2/catch_all.hpp> |
| 3 | +#include <chrono> |
| 4 | +#include <lsl_cpp.h> |
| 5 | +#include <thread> |
| 6 | + |
| 7 | +// clazy:excludeall=non-pod-global-static |
| 8 | + |
| 9 | +namespace { |
| 10 | + |
| 11 | +bool wait_until_no_consumers(lsl::stream_outlet &outlet, double timeout_sec) { |
| 12 | + auto deadline = |
| 13 | + std::chrono::steady_clock::now() + std::chrono::duration<double>(timeout_sec); |
| 14 | + while (outlet.have_consumers() && std::chrono::steady_clock::now() < deadline) |
| 15 | + std::this_thread::sleep_for(std::chrono::milliseconds(10)); |
| 16 | + return !outlet.have_consumers(); |
| 17 | +} |
| 18 | + |
| 19 | +TEST_CASE("have_consumers becomes false after idle inlet disconnects", "[outlet][basic]") { |
| 20 | + lsl::stream_info info("have_consumers_idle", "Markers", 1, lsl::IRREGULAR_RATE, lsl::cf_int32, |
| 21 | + "have_consumers_idle"); |
| 22 | + lsl::stream_outlet outlet(info); |
| 23 | + auto found = lsl::resolve_stream("name", info.name(), 1, 2.0); |
| 24 | + REQUIRE(found.size() == 1); |
| 25 | + |
| 26 | + { |
| 27 | + lsl::stream_inlet inlet(found[0]); |
| 28 | + inlet.open_stream(2); |
| 29 | + REQUIRE(outlet.wait_for_consumers(2)); |
| 30 | + REQUIRE(outlet.have_consumers()); |
| 31 | + // Intentionally do not push samples; disconnect must still be detected (#267). |
| 32 | + } |
| 33 | + |
| 34 | + CHECK(wait_until_no_consumers(outlet, 2.0)); |
| 35 | +} |
| 36 | + |
| 37 | +TEST_CASE("have_consumers becomes false after disconnect during push", "[outlet][basic]") { |
| 38 | + lsl::stream_info info("have_consumers_busy", "Markers", 1, lsl::IRREGULAR_RATE, lsl::cf_int32, |
| 39 | + "have_consumers_busy"); |
| 40 | + lsl::stream_outlet outlet(info); |
| 41 | + auto found = lsl::resolve_stream("name", info.name(), 1, 2.0); |
| 42 | + REQUIRE(found.size() == 1); |
| 43 | + |
| 44 | + std::atomic<bool> keep_pushing{true}; |
| 45 | + std::thread pusher([&outlet, &keep_pushing]() { |
| 46 | + int32_t value = 0; |
| 47 | + while (keep_pushing.load(std::memory_order_relaxed)) { |
| 48 | + outlet.push_sample(&value); |
| 49 | + ++value; |
| 50 | + } |
| 51 | + }); |
| 52 | + // Stop and join even if open_stream() throws or a REQUIRE aborts the test. |
| 53 | + struct stop_and_join { |
| 54 | + std::atomic<bool> &keep_pushing; |
| 55 | + std::thread &pusher; |
| 56 | + ~stop_and_join() { |
| 57 | + keep_pushing.store(false, std::memory_order_relaxed); |
| 58 | + pusher.join(); |
| 59 | + } |
| 60 | + } pusher_cleanup{keep_pushing, pusher}; |
| 61 | + |
| 62 | + { |
| 63 | + lsl::stream_inlet inlet(found[0]); |
| 64 | + inlet.open_stream(2); |
| 65 | + REQUIRE(outlet.wait_for_consumers(2)); |
| 66 | + REQUIRE(outlet.have_consumers()); |
| 67 | + int32_t received = 0; |
| 68 | + CHECK(inlet.pull_sample(&received, 1, 1.0) != 0.0); |
| 69 | + // Destroy the inlet while the outlet thread is still producing. |
| 70 | + std::this_thread::sleep_for(std::chrono::milliseconds(20)); |
| 71 | + } |
| 72 | + |
| 73 | + CHECK(wait_until_no_consumers(outlet, 2.0)); |
| 74 | +} |
| 75 | + |
| 76 | +TEST_CASE("have_consumers tracks explicit close and reopen", "[outlet][basic]") { |
| 77 | + lsl::stream_info info("have_consumers_reopen", "Markers", 1, lsl::IRREGULAR_RATE, |
| 78 | + lsl::cf_int32, "have_consumers_reopen"); |
| 79 | + lsl::stream_outlet outlet(info); |
| 80 | + auto found = lsl::resolve_stream("name", info.name(), 1, 2.0); |
| 81 | + REQUIRE(found.size() == 1); |
| 82 | + |
| 83 | + lsl::stream_inlet inlet(found[0]); |
| 84 | + for (int cycle = 0; cycle < 3; ++cycle) { |
| 85 | + CAPTURE(cycle); |
| 86 | + inlet.open_stream(2); |
| 87 | + REQUIRE(outlet.wait_for_consumers(2)); |
| 88 | + // close_stream() instead of destroying the inlet; the outlet must notice either way. |
| 89 | + inlet.close_stream(); |
| 90 | + REQUIRE(wait_until_no_consumers(outlet, 2.0)); |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +TEST_CASE("have_consumers stays true when one of two inlets disconnects", "[outlet][basic]") { |
| 95 | + lsl::stream_info info("have_consumers_two", "Markers", 1, lsl::IRREGULAR_RATE, lsl::cf_int32, |
| 96 | + "have_consumers_two"); |
| 97 | + lsl::stream_outlet outlet(info); |
| 98 | + auto found = lsl::resolve_stream("name", info.name(), 1, 2.0); |
| 99 | + REQUIRE(found.size() == 1); |
| 100 | + |
| 101 | + lsl::stream_inlet keeper(found[0]); |
| 102 | + keeper.open_stream(2); |
| 103 | + REQUIRE(outlet.wait_for_consumers(2)); |
| 104 | + |
| 105 | + { |
| 106 | + lsl::stream_inlet leaver(found[0]); |
| 107 | + leaver.open_stream(2); |
| 108 | + REQUIRE(outlet.have_consumers()); |
| 109 | + } |
| 110 | + std::this_thread::sleep_for(std::chrono::milliseconds(500)); |
| 111 | + |
| 112 | + // Unregistering one consumer must leave the other registered and still fed. |
| 113 | + CHECK(outlet.have_consumers()); |
| 114 | + int32_t sent = 42, received = 0; |
| 115 | + outlet.push_sample(&sent); |
| 116 | + CHECK(keeper.pull_sample(&received, 1, 2.0) != 0.0); |
| 117 | + CHECK(received == sent); |
| 118 | + |
| 119 | + keeper.close_stream(); |
| 120 | + CHECK(wait_until_no_consumers(outlet, 2.0)); |
| 121 | +} |
| 122 | + |
| 123 | +} // namespace |
0 commit comments