blob: eb28b53ebea54d4598c34ad49a3cd0a31f0468ac [file] [log] [blame]
[email protected]d9b14782010-04-15 08:08:071// Copyright (c) 2010 The Chromium Authors. All rights reserved.
[email protected]baeb22f52009-12-03 21:11:342// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// Tests of CancellationFlag class.
6
7#include "base/cancellation_flag.h"
8
9#include "base/logging.h"
[email protected]d9b14782010-04-15 08:08:0710#include "base/message_loop.h"
[email protected]baeb22f52009-12-03 21:11:3411#include "base/spin_wait.h"
12#include "base/time.h"
13#include "base/thread.h"
14#include "testing/gtest/include/gtest/gtest.h"
15#include "testing/platform_test.h"
16
17using base::CancellationFlag;
18using base::TimeDelta;
19using base::Thread;
20
21namespace {
22
23//------------------------------------------------------------------------------
24// Define our test class.
25//------------------------------------------------------------------------------
26
27class CancelTask : public Task {
28 public:
29 explicit CancelTask(CancellationFlag* flag) : flag_(flag) {}
30 virtual void Run() {
31 ASSERT_DEBUG_DEATH(flag_->Set(), "");
32 }
33 private:
34 CancellationFlag* flag_;
35};
36
37TEST(CancellationFlagTest, SimpleSingleThreadedTest) {
38 CancellationFlag flag;
39 ASSERT_FALSE(flag.IsSet());
40 flag.Set();
41 ASSERT_TRUE(flag.IsSet());
42}
43
44TEST(CancellationFlagTest, DoubleSetTest) {
45 CancellationFlag flag;
46 ASSERT_FALSE(flag.IsSet());
47 flag.Set();
48 ASSERT_TRUE(flag.IsSet());
49 flag.Set();
50 ASSERT_TRUE(flag.IsSet());
51}
52
[email protected]31a80f22009-12-10 20:33:0253TEST(CancellationFlagTest, SetOnDifferentThreadDeathTest) {
[email protected]baeb22f52009-12-03 21:11:3454 // Checks that Set() can't be called from any other thread.
[email protected]31a80f22009-12-10 20:33:0255 // CancellationFlag should die on a DCHECK if Set() is called from
56 // other thread.
[email protected]baeb22f52009-12-03 21:11:3457 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
58 Thread t("CancellationFlagTest.SetOnDifferentThreadDeathTest");
59 ASSERT_TRUE(t.Start());
60 ASSERT_TRUE(t.message_loop());
61 ASSERT_TRUE(t.IsRunning());
62
63 CancellationFlag flag;
64 t.message_loop()->PostTask(FROM_HERE, new CancelTask(&flag));
65}
66
67} // namespace