2012-05-22 15:57:34 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
2012-05-23 15:49:48 +00:00
|
|
|
|
2013-01-02 08:45:03 +00:00
|
|
|
#include "webrtc/system_wrappers/interface/thread_wrapper.h"
|
2012-05-22 15:57:34 +00:00
|
|
|
|
2013-05-27 15:07:45 +00:00
|
|
|
#include "testing/gtest/include/gtest/gtest.h"
|
2015-02-26 14:34:55 +00:00
|
|
|
#include "webrtc/base/scoped_ptr.h"
|
2014-07-18 10:12:50 +00:00
|
|
|
#include "webrtc/system_wrappers/interface/sleep.h"
|
2012-05-22 15:57:34 +00:00
|
|
|
|
|
|
|
|
namespace webrtc {
|
|
|
|
|
|
|
|
|
|
// Function that does nothing, and reports success.
|
2013-01-02 08:45:03 +00:00
|
|
|
bool NullRunFunction(void* obj) {
|
2014-07-18 10:12:50 +00:00
|
|
|
SleepMs(0); // Hand over timeslice, prevents busy looping.
|
2012-05-22 15:57:34 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-15 05:33:25 +00:00
|
|
|
TEST(ThreadTest, StartStop) {
|
2015-03-19 14:44:18 +00:00
|
|
|
rtc::scoped_ptr<ThreadWrapper> thread = ThreadWrapper::CreateThread(
|
2015-03-22 14:33:54 +00:00
|
|
|
&NullRunFunction, NULL);
|
2015-03-13 00:06:21 +00:00
|
|
|
ASSERT_TRUE(thread->Start());
|
2012-05-22 15:57:34 +00:00
|
|
|
EXPECT_TRUE(thread->Stop());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Function that sets a boolean.
|
|
|
|
|
bool SetFlagRunFunction(void* obj) {
|
2013-01-02 08:45:03 +00:00
|
|
|
bool* obj_as_bool = static_cast<bool*>(obj);
|
2012-05-22 15:57:34 +00:00
|
|
|
*obj_as_bool = true;
|
2014-07-18 10:12:50 +00:00
|
|
|
SleepMs(0); // Hand over timeslice, prevents busy looping.
|
2012-05-22 15:57:34 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-15 05:33:25 +00:00
|
|
|
TEST(ThreadTest, RunFunctionIsCalled) {
|
2012-05-22 15:57:34 +00:00
|
|
|
bool flag = false;
|
2015-03-19 14:44:18 +00:00
|
|
|
rtc::scoped_ptr<ThreadWrapper> thread = ThreadWrapper::CreateThread(
|
2015-03-22 14:33:54 +00:00
|
|
|
&SetFlagRunFunction, &flag);
|
2015-03-13 00:06:21 +00:00
|
|
|
ASSERT_TRUE(thread->Start());
|
2013-01-02 08:45:03 +00:00
|
|
|
|
2012-05-22 15:57:34 +00:00
|
|
|
// At this point, the flag may be either true or false.
|
|
|
|
|
EXPECT_TRUE(thread->Stop());
|
2013-01-02 08:45:03 +00:00
|
|
|
|
2012-05-22 15:57:34 +00:00
|
|
|
// We expect the thread to have run at least once.
|
|
|
|
|
EXPECT_TRUE(flag);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace webrtc
|