Skip to content

Commit

Permalink
Unittests: Add BlockingLoopTest
Browse files Browse the repository at this point in the history
  • Loading branch information
degasus committed May 28, 2015
1 parent d70b801 commit 40621dc
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
59 changes: 59 additions & 0 deletions Source/UnitTests/Common/BlockingLoopTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2014 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.

#include <atomic>
#include <thread>

#include <gtest/gtest.h>

#include "Common/BlockingLoop.h"


TEST(BlockingLoop, MultiThreaded)
{
Common::BlockingLoop loop;
std::atomic<int> signaled(0);
std::atomic<int> received(0);
for (int i = 0; i < 100; i++)
{
// Invalidate the current state.
received.store(signaled.load() + 1);

// Must not block as the loop is stopped.
loop.Wait();

std::thread loop_thread(
[&]() {
loop.Run(
[&]() {
received.store(signaled.load());
}
);
});

// Now Wait must block.
loop.Prepare();

// The payload must run at least once on startup.
loop.Wait();
EXPECT_EQ(signaled.load(), received.load());

for (int j = 0; j < 100; j++)
{
for (int k = 0; k < 100; k++)
{
signaled++;
loop.Set();
}

loop.Wait();
EXPECT_EQ(signaled.load(), received.load());
}

loop.Stop();

loop_thread.join();
}
}

1 change: 1 addition & 0 deletions Source/UnitTests/Common/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
add_dolphin_test(BitFieldTest BitFieldTest.cpp)
add_dolphin_test(BitSetTest BitSetTest.cpp)
add_dolphin_test(BlockingLoopTest BlockingLoopTest.cpp)
add_dolphin_test(CommonFuncsTest CommonFuncsTest.cpp)
add_dolphin_test(EventTest EventTest.cpp)
add_dolphin_test(FifoQueueTest FifoQueueTest.cpp)
Expand Down

0 comments on commit 40621dc

Please sign in to comment.