1 /* *****************************************************************
3 * Copyright 2017 Microsoft
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 ******************************************************************/
23 * This file implement tests for the Event object.
27 #include "gtest/gtest.h"
31 class EventTester : public testing::Test
36 m_event = oc_event_new();
37 ASSERT_TRUE(nullptr != m_event);
39 virtual void TearDown()
41 oc_event_free(m_event);
43 void TestSignalBeforeWait(uint32_t timeout);
47 void EventTester::TestSignalBeforeWait(uint32_t timeout)
49 std::thread thread([this]()
51 oc_event_signal(m_event);
54 // Make sure the signal occurs before the wait.
57 if (UINT_MAX == timeout)
59 oc_event_wait(m_event);
63 EXPECT_EQ(OC_WAIT_SUCCESS, oc_event_wait_for(m_event, timeout));
67 TEST_F(EventTester, InfiniteTimeout_SignaledAfterWait)
69 std::thread thread([this]()
71 // This sleep allows the main thread to enter the wait state before the signal.
72 std::this_thread::sleep_for(std::chrono::milliseconds(10));
73 oc_event_signal(m_event);
75 oc_event_wait(m_event);
79 TEST_F(EventTester, InfiniteTimeout_SignaledBeforeWait)
81 TestSignalBeforeWait(UINT_MAX);
84 TEST_F(EventTester, ZeroTimeout_NotSignaled)
86 EXPECT_EQ(OC_WAIT_TIMEDOUT, oc_event_wait_for(m_event, 0));
89 TEST_F(EventTester, ZeroTimeout_Signaled)
91 TestSignalBeforeWait(0);
94 TEST_F(EventTester, TimedOut)
96 EXPECT_EQ(OC_WAIT_TIMEDOUT, oc_event_wait_for(m_event, 10));