Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2016 The Chromium Authors |
fdoray | eed5fa7 | 2016-07-26 22:28:45 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "base/sequence_token.h" |
| 6 | |
| 7 | #include "testing/gtest/include/gtest/gtest.h" |
| 8 | |
François Doray | 524d2a2 | 2024-01-04 09:54:16 | [diff] [blame] | 9 | namespace base::internal { |
fdoray | eed5fa7 | 2016-07-26 22:28:45 | [diff] [blame] | 10 | |
| 11 | TEST(SequenceTokenTest, IsValid) { |
| 12 | EXPECT_FALSE(SequenceToken().IsValid()); |
| 13 | EXPECT_TRUE(SequenceToken::Create().IsValid()); |
| 14 | } |
| 15 | |
| 16 | TEST(SequenceTokenTest, OperatorEquals) { |
Justin Cohen | c2b52c5a | 2018-04-20 17:09:52 | [diff] [blame] | 17 | const SequenceToken invalid_a; |
| 18 | const SequenceToken invalid_b; |
fdoray | eed5fa7 | 2016-07-26 22:28:45 | [diff] [blame] | 19 | const SequenceToken valid_a = SequenceToken::Create(); |
| 20 | const SequenceToken valid_b = SequenceToken::Create(); |
| 21 | |
| 22 | EXPECT_FALSE(invalid_a == invalid_a); |
| 23 | EXPECT_FALSE(invalid_a == invalid_b); |
| 24 | EXPECT_FALSE(invalid_a == valid_a); |
| 25 | EXPECT_FALSE(invalid_a == valid_b); |
| 26 | |
| 27 | EXPECT_FALSE(valid_a == invalid_a); |
| 28 | EXPECT_FALSE(valid_a == invalid_b); |
| 29 | EXPECT_EQ(valid_a, valid_a); |
| 30 | EXPECT_FALSE(valid_a == valid_b); |
| 31 | } |
| 32 | |
| 33 | TEST(SequenceTokenTest, OperatorNotEquals) { |
Justin Cohen | c2b52c5a | 2018-04-20 17:09:52 | [diff] [blame] | 34 | const SequenceToken invalid_a; |
| 35 | const SequenceToken invalid_b; |
fdoray | eed5fa7 | 2016-07-26 22:28:45 | [diff] [blame] | 36 | const SequenceToken valid_a = SequenceToken::Create(); |
| 37 | const SequenceToken valid_b = SequenceToken::Create(); |
| 38 | |
| 39 | EXPECT_NE(invalid_a, invalid_a); |
| 40 | EXPECT_NE(invalid_a, invalid_b); |
| 41 | EXPECT_NE(invalid_a, valid_a); |
| 42 | EXPECT_NE(invalid_a, valid_b); |
| 43 | |
| 44 | EXPECT_NE(valid_a, invalid_a); |
| 45 | EXPECT_NE(valid_a, invalid_b); |
| 46 | EXPECT_FALSE(valid_a != valid_a); |
| 47 | EXPECT_NE(valid_a, valid_b); |
| 48 | } |
| 49 | |
| 50 | TEST(SequenceTokenTest, GetForCurrentThread) { |
| 51 | const SequenceToken token = SequenceToken::Create(); |
| 52 | |
François Doray | 524d2a2 | 2024-01-04 09:54:16 | [diff] [blame] | 53 | EXPECT_TRUE(SequenceToken::GetForCurrentThread().IsValid()); |
fdoray | eed5fa7 | 2016-07-26 22:28:45 | [diff] [blame] | 54 | |
| 55 | { |
François Doray | 524d2a2 | 2024-01-04 09:54:16 | [diff] [blame] | 56 | TaskScope task_scope(token, /* is_thread_bound=*/false); |
fdoray | eed5fa7 | 2016-07-26 22:28:45 | [diff] [blame] | 57 | EXPECT_TRUE(SequenceToken::GetForCurrentThread().IsValid()); |
| 58 | EXPECT_EQ(token, SequenceToken::GetForCurrentThread()); |
| 59 | } |
| 60 | |
François Doray | 524d2a2 | 2024-01-04 09:54:16 | [diff] [blame] | 61 | EXPECT_TRUE(SequenceToken::GetForCurrentThread().IsValid()); |
fdoray | eed5fa7 | 2016-07-26 22:28:45 | [diff] [blame] | 62 | } |
| 63 | |
gab | b755507 | 2016-10-06 21:16:24 | [diff] [blame] | 64 | TEST(SequenceTokenTest, ToInternalValue) { |
| 65 | const SequenceToken token1 = SequenceToken::Create(); |
| 66 | const SequenceToken token2 = SequenceToken::Create(); |
| 67 | |
| 68 | // Confirm that internal values are unique. |
| 69 | EXPECT_NE(token1.ToInternalValue(), token2.ToInternalValue()); |
| 70 | } |
| 71 | |
fdoray | b339954b | 2016-08-09 21:49:26 | [diff] [blame] | 72 | // Expect a default-constructed TaskToken to be invalid and not equal to |
| 73 | // another invalid TaskToken. |
| 74 | TEST(TaskTokenTest, InvalidDefaultConstructed) { |
| 75 | EXPECT_FALSE(TaskToken().IsValid()); |
| 76 | EXPECT_NE(TaskToken(), TaskToken()); |
| 77 | } |
| 78 | |
| 79 | // Expect a TaskToken returned by TaskToken::GetForCurrentThread() outside the |
François Doray | 524d2a2 | 2024-01-04 09:54:16 | [diff] [blame] | 80 | // scope of a TaskScope to be invalid. |
fdoray | b339954b | 2016-08-09 21:49:26 | [diff] [blame] | 81 | TEST(TaskTokenTest, InvalidOutsideScope) { |
| 82 | EXPECT_FALSE(TaskToken::GetForCurrentThread().IsValid()); |
| 83 | } |
| 84 | |
| 85 | // Expect an invalid TaskToken not to be equal with a valid TaskToken. |
| 86 | TEST(TaskTokenTest, ValidNotEqualsInvalid) { |
François Doray | 524d2a2 | 2024-01-04 09:54:16 | [diff] [blame] | 87 | TaskScope task_scope(SequenceToken::Create(), |
| 88 | /* is_thread_bound=*/false); |
fdoray | b339954b | 2016-08-09 21:49:26 | [diff] [blame] | 89 | TaskToken valid = TaskToken::GetForCurrentThread(); |
| 90 | TaskToken invalid; |
| 91 | EXPECT_NE(valid, invalid); |
| 92 | } |
| 93 | |
| 94 | // Expect TaskTokens returned by TaskToken::GetForCurrentThread() in the scope |
François Doray | 524d2a2 | 2024-01-04 09:54:16 | [diff] [blame] | 95 | // of the same TaskScope instance to be |
fdoray | b339954b | 2016-08-09 21:49:26 | [diff] [blame] | 96 | // valid and equal with each other. |
| 97 | TEST(TaskTokenTest, EqualInSameScope) { |
François Doray | 524d2a2 | 2024-01-04 09:54:16 | [diff] [blame] | 98 | TaskScope task_scope(SequenceToken::Create(), |
| 99 | /* is_thread_bound=*/false); |
fdoray | b339954b | 2016-08-09 21:49:26 | [diff] [blame] | 100 | |
| 101 | const TaskToken token_a = TaskToken::GetForCurrentThread(); |
| 102 | const TaskToken token_b = TaskToken::GetForCurrentThread(); |
| 103 | |
| 104 | EXPECT_TRUE(token_a.IsValid()); |
| 105 | EXPECT_TRUE(token_b.IsValid()); |
| 106 | EXPECT_EQ(token_a, token_b); |
| 107 | } |
| 108 | |
| 109 | // Expect TaskTokens returned by TaskToken::GetForCurrentThread() in the scope |
François Doray | 524d2a2 | 2024-01-04 09:54:16 | [diff] [blame] | 110 | // of different TaskScope instances to be |
fdoray | b339954b | 2016-08-09 21:49:26 | [diff] [blame] | 111 | // valid but not equal to each other. |
| 112 | TEST(TaskTokenTest, NotEqualInDifferentScopes) { |
| 113 | TaskToken token_a; |
| 114 | TaskToken token_b; |
| 115 | |
| 116 | { |
François Doray | 524d2a2 | 2024-01-04 09:54:16 | [diff] [blame] | 117 | TaskScope task_scope(SequenceToken::Create(), |
| 118 | /* is_thread_bound=*/false); |
fdoray | b339954b | 2016-08-09 21:49:26 | [diff] [blame] | 119 | token_a = TaskToken::GetForCurrentThread(); |
| 120 | } |
| 121 | { |
François Doray | 524d2a2 | 2024-01-04 09:54:16 | [diff] [blame] | 122 | TaskScope task_scope(SequenceToken::Create(), |
| 123 | /* is_thread_bound=*/false); |
fdoray | b339954b | 2016-08-09 21:49:26 | [diff] [blame] | 124 | token_b = TaskToken::GetForCurrentThread(); |
| 125 | } |
| 126 | |
| 127 | EXPECT_TRUE(token_a.IsValid()); |
| 128 | EXPECT_TRUE(token_b.IsValid()); |
| 129 | EXPECT_NE(token_a, token_b); |
| 130 | } |
| 131 | |
François Doray | 524d2a2 | 2024-01-04 09:54:16 | [diff] [blame] | 132 | TEST(TaskScopeTest, ThreadBound) { |
| 133 | // Code running outside of a `TaskScope` is thread-bound. |
| 134 | EXPECT_TRUE(CurrentTaskIsThreadBound()); |
| 135 | |
| 136 | { |
| 137 | TaskScope scope(SequenceToken::Create(), /* is_thread_bound=*/false); |
| 138 | EXPECT_FALSE(CurrentTaskIsThreadBound()); |
| 139 | { |
| 140 | TaskScope inner_scope(SequenceToken::Create(), /* is_thread_bound=*/true); |
| 141 | EXPECT_TRUE(CurrentTaskIsThreadBound()); |
| 142 | } |
| 143 | EXPECT_FALSE(CurrentTaskIsThreadBound()); |
| 144 | } |
| 145 | |
| 146 | EXPECT_TRUE(CurrentTaskIsThreadBound()); |
| 147 | |
| 148 | { |
| 149 | TaskScope scope(SequenceToken::Create(), /* is_thread_bound=*/true); |
| 150 | EXPECT_TRUE(CurrentTaskIsThreadBound()); |
| 151 | { |
| 152 | TaskScope inner_scope(SequenceToken::Create(), |
| 153 | /* is_thread_bound=*/false); |
| 154 | EXPECT_FALSE(CurrentTaskIsThreadBound()); |
| 155 | } |
| 156 | EXPECT_TRUE(CurrentTaskIsThreadBound()); |
| 157 | } |
| 158 | |
| 159 | EXPECT_TRUE(CurrentTaskIsThreadBound()); |
| 160 | } |
| 161 | |
| 162 | } // namespace base::internal |