OLD | NEW |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-2015 Eyeo GmbH | 3 * Copyright (C) 2006-2015 Eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
(...skipping 16 matching lines...) Expand all Loading... |
27 const std::string failMessage = "initializer failed on purpose"; | 27 const std::string failMessage = "initializer failed on purpose"; |
28 void FailFunction() | 28 void FailFunction() |
29 { | 29 { |
30 throw std::runtime_error(failMessage); | 30 throw std::runtime_error(failMessage); |
31 } | 31 } |
32 | 32 |
33 /* | 33 /* |
34 * The destructor has to be able to block to wait for the initializer | 34 * The destructor has to be able to block to wait for the initializer |
35 * This test ensures that it doesn't block unnecessarily. | 35 * This test ensures that it doesn't block unnecessarily. |
36 */ | 36 */ |
37 TEST(DetachedInitializer, DestructorDoesNotBlockAfterNoActivity) | 37 TEST(DetachedInitializerStaticFunction, DestructorDoesNotBlockAfterNoActivity) |
38 { | 38 { |
39 DetachedInitializer<NullFunction> x; | 39 DetachedInitializerStaticFunction<NullFunction> x; |
40 } | 40 } |
41 | 41 |
42 TEST(DetachedInitializer, NullInstance) | 42 TEST(DetachedInitializerStaticFunction, NullInstance) |
43 { | 43 { |
44 ASSERT_NO_THROW( | 44 ASSERT_NO_THROW( |
45 { | 45 { |
46 DetachedInitializer<NullFunction> x; | 46 DetachedInitializerStaticFunction<NullFunction> x; |
47 x.SpawnInitializer(); | 47 x.SpawnInitializer(); |
48 x.EnsureInitialized(); | 48 x.EnsureInitialized(); |
49 } | 49 } |
50 ); | 50 ); |
51 } | 51 } |
52 | 52 |
53 /* | 53 /* |
54 * This test illustrates a defect in Google Test. | 54 * This test illustrates a defect in Google Test. |
55 * This test fails with a message that the test threw an exception. | 55 * This test fails with a message that the test threw an exception. |
56 * The exception message is the one thrown by FailFunction. | 56 * The exception message is the one thrown by FailFunction. |
57 * It does not fail, importantly, because SpawnInitializer() throws. | 57 * It does not fail, importantly, because SpawnInitializer() throws. |
58 * This can be verified in the debugger. | 58 * This can be verified in the debugger. |
59 * | 59 * |
60 * It's disabled in the present code base so it doesn't break the unit test. | 60 * It's disabled in the present code base so it doesn't break the unit test. |
61 * Check this test again when we upgrade past GTest 1.60 and the VS 2012 toolset
. | 61 * Check this test again when we upgrade past GTest 1.60 and the VS 2012 toolset
. |
62 */ | 62 */ |
63 TEST(DetachedInitializer, DISABLED_EnsureInitializedThrowingGtestDefectIllustrat
ion) | 63 TEST(DetachedInitializerStaticFunction, DISABLED_EnsureInitializedThrowingGtestD
efectIllustration) |
64 { | 64 { |
65 DetachedInitializer<FailFunction> x; | 65 DetachedInitializerStaticFunction<FailFunction> x; |
66 bool threw = false; | 66 bool threw = false; |
67 try | 67 try |
68 { | 68 { |
69 x.SpawnInitializer(); | 69 x.SpawnInitializer(); |
70 } | 70 } |
71 catch (...) | 71 catch (...) |
72 { | 72 { |
73 threw = true; | 73 threw = true; |
74 FAIL() << "SpawnInitializer() threw"; | 74 FAIL() << "SpawnInitializer() threw"; |
75 } | 75 } |
76 ASSERT_FALSE(threw); | 76 ASSERT_FALSE(threw); |
77 } | 77 } |
78 | 78 |
79 /* | 79 /* |
80 * This test fails because of a defect in Google Test. | 80 * This test fails because of a defect in Google Test. |
81 * It's disabled at present, but should be enabled when the previous test is wor
king. | 81 * It's disabled at present, but should be enabled when the previous test is wor
king. |
82 */ | 82 */ |
83 TEST(DetachedInitializer, DISABLED_EnsureInitializedThrowing) | 83 TEST(DetachedInitializerStaticFunction, DISABLED_EnsureInitializedThrowing) |
84 { | 84 { |
85 DetachedInitializer<FailFunction> x; | 85 DetachedInitializerStaticFunction<FailFunction> x; |
86 try | 86 try |
87 { | 87 { |
88 x.SpawnInitializer(); | 88 x.SpawnInitializer(); |
89 } | 89 } |
90 catch (...) | 90 catch (...) |
91 { | 91 { |
92 FAIL() << "SpawnInitializer() threw"; | 92 FAIL() << "SpawnInitializer() threw"; |
93 return; | 93 return; |
94 } | 94 } |
95 try | 95 try |
96 { | 96 { |
97 x.EnsureInitialized(); | 97 x.EnsureInitialized(); |
98 FAIL() << "EnsureInitialized() did not throw"; | 98 FAIL() << "EnsureInitialized() did not throw"; |
99 } | 99 } |
100 catch (std::runtime_error& e) | 100 catch (std::runtime_error& e) |
101 { | 101 { |
102 ASSERT_EQ(failMessage, e.what()); | 102 ASSERT_EQ(failMessage, e.what()); |
103 } | 103 } |
104 } | 104 } |
105 | 105 |
106 /* | 106 /* |
107 * Resetable version of DetachedInitializer | 107 * Resetable version of DetachedInitializerStaticFunction |
108 */ | 108 */ |
109 template<void(&F)()> | 109 template<void(&F)()> |
110 class DidReset | 110 class DisfReset |
111 : public DetachedInitializer<F> | 111 : public DetachedInitializerStaticFunction<F> |
112 { | 112 { |
113 public: | 113 public: |
114 void Reset() | 114 void Reset() |
115 { | 115 { |
116 isInitializationComplete.store(false, std::memory_order_relaxed); | 116 isInitializationComplete.store(false, std::memory_order_relaxed); |
117 hasInitializerStarted.store(false, std::memory_order_relaxed); | 117 hasInitializerStarted.store(false, std::memory_order_relaxed); |
118 initializerException = nullptr; | 118 initializerException = nullptr; |
119 // Lock and unlock the mutex to clear any previous state | 119 // Lock and unlock the mutex to clear any previous state |
120 // It will also detect a locked mutex left over from a previous test. | 120 // It will also detect a locked mutex left over from a previous test. |
121 mutex.lock(); | 121 mutex.lock(); |
122 mutex.unlock(); | 122 mutex.unlock(); |
123 } | 123 } |
124 }; | 124 }; |
125 | 125 |
126 /* | 126 /* |
127 * Uses designated implementation pattern of DetachedInitializer | 127 * Uses designated implementation pattern of DetachedInitializerStaticFunction |
128 */ | 128 */ |
129 struct DeferredIncrementingInitializer | 129 struct DeferredIncrementingInitializer |
130 { | 130 { |
131 static int initializedCount; | 131 static int initializedCount; |
132 | 132 |
133 static void Incrementer() | 133 static void Incrementer() |
134 { | 134 { |
135 ++initializedCount; | 135 ++initializedCount; |
136 } | 136 } |
137 | 137 |
138 typedef DidReset<Incrementer> InitializerType; | 138 typedef DisfReset<Incrementer> InitializerType; |
139 static InitializerType di; | 139 static InitializerType di; |
140 | 140 |
141 public: | 141 public: |
142 DeferredIncrementingInitializer() | 142 DeferredIncrementingInitializer() |
143 { | 143 { |
144 di.SpawnInitializer(); | 144 di.SpawnInitializer(); |
145 } | 145 } |
146 | 146 |
147 static void ResetClass() | 147 static void ResetClass() |
148 { | 148 { |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
189 TEST(DetachedInitializer, Many) | 189 TEST(DetachedInitializer, Many) |
190 { | 190 { |
191 DeferredIncrementingInitializer::ResetClass(); | 191 DeferredIncrementingInitializer::ResetClass(); |
192 const int many = 1000000; | 192 const int many = 1000000; |
193 for (int j = 0; j < many;++j) | 193 for (int j = 0; j < many;++j) |
194 { | 194 { |
195 DeferredIncrementingInitializer x; | 195 DeferredIncrementingInitializer x; |
196 } | 196 } |
197 ASSERT_EQ(1, DeferredIncrementingInitializer::GetInitializerCount()); | 197 ASSERT_EQ(1, DeferredIncrementingInitializer::GetInitializerCount()); |
198 } | 198 } |
OLD | NEW |