PROTON-1930: [cpp] Fix race condition in container_test.cpp Test was starting container, opening connection and then checking for ["start", "open"] sequence to be set by handlers.
Sometimes the sequence was instead ["open", "start"], which is legal since the events are generated in different handler contexts. Fixed by starting container, waiting for "start", opening connection, then waiting for "open" Project: http://git-wip-us.apache.org/repos/asf/qpid-proton/repo Commit: http://git-wip-us.apache.org/repos/asf/qpid-proton/commit/d3c11354 Tree: http://git-wip-us.apache.org/repos/asf/qpid-proton/tree/d3c11354 Diff: http://git-wip-us.apache.org/repos/asf/qpid-proton/diff/d3c11354 Branch: refs/heads/master Commit: d3c113548146b56cdd8b2a31c2d4dd7ba5dddbfd Parents: 27edd9a Author: Alan Conway <acon...@redhat.com> Authored: Mon Sep 10 12:35:52 2018 -0400 Committer: Alan Conway <acon...@redhat.com> Committed: Mon Sep 10 15:50:37 2018 -0400 ---------------------------------------------------------------------- cpp/src/container_test.cpp | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/d3c11354/cpp/src/container_test.cpp ---------------------------------------------------------------------- diff --git a/cpp/src/container_test.cpp b/cpp/src/container_test.cpp index 6f7caa4..0e74aaa 100644 --- a/cpp/src/container_test.cpp +++ b/cpp/src/container_test.cpp @@ -366,17 +366,18 @@ void test_container_mt_stop_empty() { c.auto_stop( false ); container_runner runner(c); auto t = std::thread(runner); - // Must ensure that thread is joined or detached + // Must ensure that thread is joined try { ASSERT_EQUAL("start", th.wait()); c.stop(); t.join(); ASSERT_EQUAL("", th.error().name()); - } catch (...) { - // We don't join as we don't know if we'll be stuck waiting - if (t.joinable()) { - t.detach(); - } + } catch (const std::exception &e) { + std::cerr << FAIL_MSG(e.what()) << std::endl; + // If join hangs, let the test die by timeout. We cannot + // detach and continue: deleting the container while t is + // still alive will put the process in an undefined state. + t.join(); throw; } } @@ -387,19 +388,20 @@ void test_container_mt_stop() { c.auto_stop(false); container_runner runner(c); auto t = std::thread(runner); - // Must ensure that thread is joined or detached + // Must ensure that thread is joined try { test_listen_handler lh; - c.listen("//:0", lh); // Also opens a connection ASSERT_EQUAL("start", th.wait()); + c.listen("//:0", lh); // Also opens a connection ASSERT_EQUAL("open", th.wait()); c.stop(); t.join(); - } catch (...) { - // We don't join as we don't know if we'll be stuck waiting - if (t.joinable()) { - t.detach(); - } + } catch (const std::exception& e) { + std::cerr << FAIL_MSG(e.what()) << std::endl; + // If join hangs, let the test die by timeout. We cannot + // detach and continue: deleting the container while t is + // still alive will put the process in an undefined state. + t.join(); throw; } } --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@qpid.apache.org For additional commands, e-mail: commits-h...@qpid.apache.org