TestEventSignal.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <native/tests/assetmanager/TestEventSignal.h>
  9. #include <AzCore/std/parallel/thread.h>
  10. #if !defined(Q_MOC_RUN)
  11. #include <AzCore/UnitTest/TestTypes.h>
  12. #endif
  13. namespace UnitTests
  14. {
  15. void TestEventPair::Signal()
  16. {
  17. bool expected = false;
  18. ASSERT_TRUE(m_signaled.compare_exchange_strong(expected, true));
  19. ASSERT_EQ(m_threadId, AZStd::thread_id{});
  20. m_threadId = AZStd::this_thread::get_id();
  21. m_event.release();
  22. }
  23. bool TestEventPair::WaitAndCheck()
  24. {
  25. // usually this completes under a millisecond or two, but a slow machine or busy machine
  26. // can cause hiccups of anywhere between a few milliseconds to a few seconds.
  27. // Since this test will exit the instant it gets its signal, prefer to set a very long timeout
  28. // beyond what is even remotely necessary, so that if the test hits it, we know with a high degree of confidence
  29. // that the message is not forthcoming, not that we just didn't wait long enough for it due to environmental
  30. // issues.
  31. constexpr int MaxWaitTimeMilliseconds = 30000;
  32. auto thisThreadId = AZStd::this_thread::get_id();
  33. bool acquireSuccess = m_event.try_acquire_for(AZStd::chrono::milliseconds(MaxWaitTimeMilliseconds));
  34. EXPECT_TRUE(acquireSuccess);
  35. EXPECT_NE(m_threadId, AZStd::thread_id{});
  36. EXPECT_TRUE(m_threadId != thisThreadId);
  37. return acquireSuccess && m_threadId != AZStd::thread_id{} && m_threadId != thisThreadId;
  38. }
  39. }