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
|
|
|
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "rtc_base/platform_thread.h"
|
2012-05-22 15:57:34 +00:00
|
|
|
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "test/gtest.h"
|
2012-05-22 15:57:34 +00:00
|
|
|
|
2016-04-22 09:08:44 -07:00
|
|
|
namespace rtc {
|
|
|
|
|
namespace {
|
2012-05-22 15:57:34 +00:00
|
|
|
|
2017-02-22 11:22:05 -08:00
|
|
|
void NullRunFunction(void* obj) {}
|
|
|
|
|
|
2012-05-22 15:57:34 +00:00
|
|
|
// Function that sets a boolean.
|
2017-02-22 11:22:05 -08:00
|
|
|
void SetFlagRunFunction(void* obj) {
|
|
|
|
|
bool* obj_as_bool = static_cast<bool*>(obj);
|
|
|
|
|
*obj_as_bool = true;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-22 09:08:44 -07:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
TEST(PlatformThreadTest, StartStop) {
|
|
|
|
|
PlatformThread thread(&NullRunFunction, nullptr, "PlatformThreadTest");
|
|
|
|
|
EXPECT_TRUE(thread.name() == "PlatformThreadTest");
|
|
|
|
|
EXPECT_TRUE(thread.GetThreadRef() == 0);
|
|
|
|
|
thread.Start();
|
|
|
|
|
EXPECT_TRUE(thread.GetThreadRef() != 0);
|
|
|
|
|
thread.Stop();
|
|
|
|
|
EXPECT_TRUE(thread.GetThreadRef() == 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(PlatformThreadTest, StartStop2) {
|
|
|
|
|
PlatformThread thread1(&NullRunFunction, nullptr, "PlatformThreadTest1");
|
|
|
|
|
PlatformThread thread2(&NullRunFunction, nullptr, "PlatformThreadTest2");
|
|
|
|
|
EXPECT_TRUE(thread1.GetThreadRef() == thread2.GetThreadRef());
|
|
|
|
|
thread1.Start();
|
|
|
|
|
thread2.Start();
|
|
|
|
|
EXPECT_TRUE(thread1.GetThreadRef() != thread2.GetThreadRef());
|
|
|
|
|
thread2.Stop();
|
|
|
|
|
thread1.Stop();
|
|
|
|
|
}
|
2012-05-22 15:57:34 +00:00
|
|
|
|
2015-11-23 14:47:56 -08:00
|
|
|
TEST(PlatformThreadTest, RunFunctionIsCalled) {
|
2012-05-22 15:57:34 +00:00
|
|
|
bool flag = false;
|
2016-04-22 09:08:44 -07:00
|
|
|
PlatformThread thread(&SetFlagRunFunction, &flag, "RunFunctionIsCalled");
|
2015-11-26 17:45:47 +01:00
|
|
|
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.
|
2015-11-26 17:45:47 +01:00
|
|
|
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);
|
|
|
|
|
}
|
2017-02-22 11:22:05 -08:00
|
|
|
|
2016-04-22 09:08:44 -07:00
|
|
|
} // namespace rtc
|