blob: 25095e6599ba2b28b382b233e1e0f9b3bad340dd [file] [log] [blame]
hta@webrtc.orge1919f42012-05-22 15:57:34 +00001/*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
hta@webrtc.org40300132012-05-23 15:49:48 +000010
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000011#include "webrtc/system_wrappers/interface/thread_wrapper.h"
hta@webrtc.orge1919f42012-05-22 15:57:34 +000012
pbos@webrtc.orgacaf3a12013-05-27 15:07:45 +000013#include "testing/gtest/include/gtest/gtest.h"
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000014#include "webrtc/system_wrappers/interface/scoped_ptr.h"
hta@webrtc.orge1919f42012-05-22 15:57:34 +000015
16namespace webrtc {
17
hta@webrtc.orge1919f42012-05-22 15:57:34 +000018// Function that does nothing, and reports success.
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000019bool NullRunFunction(void* obj) {
hta@webrtc.orge1919f42012-05-22 15:57:34 +000020 return true;
21}
22
andrew@webrtc.org23ec30b2012-11-15 05:33:25 +000023TEST(ThreadTest, StartStop) {
henrike@webrtc.orga3e6bec2013-01-18 16:39:21 +000024 ThreadWrapper* thread = ThreadWrapper::CreateThread(&NullRunFunction, NULL);
hta@webrtc.orge1919f42012-05-22 15:57:34 +000025 unsigned int id = 42;
26 ASSERT_TRUE(thread->Start(id));
27 EXPECT_TRUE(thread->Stop());
hta@webrtc.org6ed617b2012-05-22 16:55:16 +000028 delete thread;
hta@webrtc.orge1919f42012-05-22 15:57:34 +000029}
30
31// Function that sets a boolean.
32bool SetFlagRunFunction(void* obj) {
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000033 bool* obj_as_bool = static_cast<bool*>(obj);
hta@webrtc.orge1919f42012-05-22 15:57:34 +000034 *obj_as_bool = true;
35 return true;
36}
37
andrew@webrtc.org23ec30b2012-11-15 05:33:25 +000038TEST(ThreadTest, RunFunctionIsCalled) {
hta@webrtc.orge1919f42012-05-22 15:57:34 +000039 bool flag = false;
40 ThreadWrapper* thread = ThreadWrapper::CreateThread(&SetFlagRunFunction,
41 &flag);
42 unsigned int id = 42;
43 ASSERT_TRUE(thread->Start(id));
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000044
hta@webrtc.orge1919f42012-05-22 15:57:34 +000045 // At this point, the flag may be either true or false.
46 EXPECT_TRUE(thread->Stop());
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000047
hta@webrtc.orge1919f42012-05-22 15:57:34 +000048 // We expect the thread to have run at least once.
49 EXPECT_TRUE(flag);
hta@webrtc.org6ed617b2012-05-22 16:55:16 +000050 delete thread;
hta@webrtc.orge1919f42012-05-22 15:57:34 +000051}
52
hta@webrtc.orge1919f42012-05-22 15:57:34 +000053} // namespace webrtc