Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include <opc/common/thread.h>
00012
00013 #include <gmock/gmock.h>
00014 #include <gtest/gtest.h>
00015
00016 using namespace testing;
00017
00018 namespace
00019 {
00020
00021 class TestThreadObserver : public Common::ThreadObserver
00022 {
00023 public:
00024 TestThreadObserver()
00025 : OnSuccessCallCount(0)
00026 , OnErrorCallCount(0)
00027 {
00028 }
00029
00030 virtual void OnSuccess() override
00031 {
00032 ++OnSuccessCallCount;
00033 }
00034
00035 virtual void OnError(const std::exception& exc) override
00036 {
00037 ++OnErrorCallCount;
00038 }
00039
00040 public:
00041 volatile unsigned OnSuccessCallCount;
00042 volatile unsigned OnErrorCallCount;
00043 };
00044
00045 void DoNothing()
00046 {
00047 }
00048
00049 void ThrowsException()
00050 {
00051 throw std::logic_error("oppps!");
00052 }
00053
00054 }
00055
00056 TEST(Thread, CallsOnSuccess)
00057 {
00058 TestThreadObserver observer;
00059
00060 Common::Thread thread(std::function<void()>(DoNothing), &observer);
00061 thread.Join();
00062 EXPECT_EQ(observer.OnSuccessCallCount, 1);
00063 EXPECT_EQ(observer.OnErrorCallCount, 0);
00064 }
00065
00066 TEST(Thread, CallsOnError)
00067 {
00068 TestThreadObserver observer;
00069
00070 Common::Thread thread(std::function<void()>(ThrowsException), &observer);
00071 thread.Join();
00072 EXPECT_EQ(observer.OnSuccessCallCount, 0);
00073 EXPECT_EQ(observer.OnErrorCallCount, 1);
00074 }
00075