| OLD | NEW |
| 1 // Copyright 2009 Google Inc. All Rights Reserved. | 1 // Copyright 2009 Google Inc. All Rights Reserved. |
| 2 // Author: Nabeel Mian (nabeelmian@google.com) | 2 // Author: Nabeel Mian (nabeelmian@google.com) |
| 3 // Chris Demetriou (cgd@google.com) | 3 // Chris Demetriou (cgd@google.com) |
| 4 // | 4 // |
| 5 // This file contains the unit tests for profile-handler.h interface. | 5 // This file contains the unit tests for profile-handler.h interface. |
| 6 | 6 |
| 7 #include "config.h" | 7 #include "config.h" |
| 8 #include "profile-handler.h" | 8 #include "profile-handler.h" |
| 9 | 9 |
| 10 #include <assert.h> | 10 #include <assert.h> |
| 11 #include <pthread.h> |
| 11 #include <sys/time.h> | 12 #include <sys/time.h> |
| 12 #include <pthread.h> | 13 #include <time.h> |
| 13 #include "base/logging.h" | 14 #include "base/logging.h" |
| 14 #include "base/simple_mutex.h" | 15 #include "base/simple_mutex.h" |
| 15 | 16 |
| 16 // Some helpful macros for the test class | 17 // Some helpful macros for the test class |
| 17 #define TEST_F(cls, fn) void cls :: fn() | 18 #define TEST_F(cls, fn) void cls :: fn() |
| 18 | 19 |
| 19 namespace { | 20 namespace { |
| 20 | 21 |
| 21 // TODO(csilvers): error-checking on the pthreads routines | 22 // TODO(csilvers): error-checking on the pthreads routines |
| 22 class Thread { | 23 class Thread { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 39 private: | 40 private: |
| 40 static void* DoRun(void* cls) { | 41 static void* DoRun(void* cls) { |
| 41 ProfileHandlerRegisterThread(); | 42 ProfileHandlerRegisterThread(); |
| 42 reinterpret_cast<Thread*>(cls)->Run(); | 43 reinterpret_cast<Thread*>(cls)->Run(); |
| 43 return NULL; | 44 return NULL; |
| 44 } | 45 } |
| 45 pthread_t thread_; | 46 pthread_t thread_; |
| 46 bool joinable_; | 47 bool joinable_; |
| 47 }; | 48 }; |
| 48 | 49 |
| 49 // Sleep interval in usecs. To ensure a SIGPROF timer interrupt under heavy | 50 // Sleep interval in nano secs. ITIMER_PROF goes off only afer the specified CPU |
| 50 // load, this is set to a 20x of ProfileHandler timer interval (i.e 100Hz) | 51 // time is consumed. Under heavy load this process may no get scheduled in a |
| 51 // TODO(nabeelmian) Under very heavy loads, the worker thread may not accumulate | 52 // timely fashion. Therefore, give enough time (20x of ProfileHandle timer |
| 52 // enough cpu usage to get a profile tick. | 53 // interval 10ms (100Hz)) for this process to accumulate enought CPU time to get |
| 53 int kSleepInterval = 200000; | 54 // a profile tick. |
| 55 int kSleepInterval = 200000000; |
| 56 |
| 57 // Sleep interval in nano secs. To ensure that if the timer has expired it is |
| 58 // reset. |
| 59 int kTimerResetInterval = 5000000; |
| 54 | 60 |
| 55 // Whether each thread has separate timers. | 61 // Whether each thread has separate timers. |
| 56 static bool timer_separate_ = false; | 62 static bool timer_separate_ = false; |
| 63 static int timer_type_ = ITIMER_PROF; |
| 64 static int signal_number_ = SIGPROF; |
| 65 |
| 66 // Delays processing by the specified number of nano seconds. 'delay_ns' |
| 67 // must be less than the number of nano seconds in a second (1000000000). |
| 68 void Delay(int delay_ns) { |
| 69 static const int kNumNSecInSecond = 1000000000; |
| 70 EXPECT_LT(delay_ns, kNumNSecInSecond); |
| 71 struct timespec delay = { 0, delay_ns }; |
| 72 nanosleep(&delay, 0); |
| 73 } |
| 57 | 74 |
| 58 // Checks whether the profile timer is enabled for the current thread. | 75 // Checks whether the profile timer is enabled for the current thread. |
| 59 bool IsTimerEnabled() { | 76 bool IsTimerEnabled() { |
| 60 itimerval current_timer; | 77 itimerval current_timer; |
| 61 EXPECT_EQ(0, getitimer(ITIMER_PROF, ¤t_timer)); | 78 EXPECT_EQ(0, getitimer(timer_type_, ¤t_timer)); |
| 79 if ((current_timer.it_value.tv_sec == 0) && |
| 80 (current_timer.it_value.tv_usec != 0)) { |
| 81 // May be the timer has expired. Sleep for a bit and check again. |
| 82 Delay(kTimerResetInterval); |
| 83 EXPECT_EQ(0, getitimer(timer_type_, ¤t_timer)); |
| 84 } |
| 62 return (current_timer.it_value.tv_sec != 0 || | 85 return (current_timer.it_value.tv_sec != 0 || |
| 63 current_timer.it_value.tv_usec != 0); | 86 current_timer.it_value.tv_usec != 0); |
| 64 } | 87 } |
| 65 | 88 |
| 66 class VirtualTimerGetterThread : public Thread { | 89 class VirtualTimerGetterThread : public Thread { |
| 67 public: | 90 public: |
| 68 VirtualTimerGetterThread() { | 91 VirtualTimerGetterThread() { |
| 69 memset(&virtual_timer_, 0, sizeof virtual_timer_); | 92 memset(&virtual_timer_, 0, sizeof virtual_timer_); |
| 70 } | 93 } |
| 71 struct itimerval virtual_timer_; | 94 struct itimerval virtual_timer_; |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 int* counter = static_cast<int*>(tick_counter); | 176 int* counter = static_cast<int*>(tick_counter); |
| 154 ++(*counter); | 177 ++(*counter); |
| 155 } | 178 } |
| 156 | 179 |
| 157 // This class tests the profile-handler.h interface. | 180 // This class tests the profile-handler.h interface. |
| 158 class ProfileHandlerTest { | 181 class ProfileHandlerTest { |
| 159 protected: | 182 protected: |
| 160 | 183 |
| 161 // Determines whether threads have separate timers. | 184 // Determines whether threads have separate timers. |
| 162 static void SetUpTestCase() { | 185 static void SetUpTestCase() { |
| 186 timer_type_ = (getenv("CPUPROFILE_REALTIME") ? ITIMER_REAL : ITIMER_PROF); |
| 187 signal_number_ = (getenv("CPUPROFILE_REALTIME") ? SIGALRM : SIGPROF); |
| 188 |
| 163 timer_separate_ = threads_have_separate_timers(); | 189 timer_separate_ = threads_have_separate_timers(); |
| 190 Delay(kTimerResetInterval); |
| 164 } | 191 } |
| 165 | 192 |
| 166 // Sets up the profile timers and SIGPROF handler in a known state. It does | 193 // Sets up the profile timers and SIGPROF/SIGALRM handler in a known state. |
| 167 // the following: | 194 // It does the following: |
| 168 // 1. Unregisters all the callbacks, stops the timer (if shared) and | 195 // 1. Unregisters all the callbacks, stops the timer (if shared) and |
| 169 // clears out timer_sharing state in the ProfileHandler. This clears | 196 // clears out timer_sharing state in the ProfileHandler. This clears |
| 170 // out any state left behind by the previous test or during module | 197 // out any state left behind by the previous test or during module |
| 171 // initialization when the test program was started. | 198 // initialization when the test program was started. |
| 172 // 2. Spawns two threads which will be registered with the ProfileHandler. | 199 // 2. Spawns two threads which will be registered with the ProfileHandler. |
| 173 // At this time ProfileHandler knows if the timers are shared. | 200 // At this time ProfileHandler knows if the timers are shared. |
| 174 // 3. Starts a busy worker thread to accumulate CPU usage. | 201 // 3. Starts a busy worker thread to accumulate CPU usage. |
| 175 virtual void SetUp() { | 202 virtual void SetUp() { |
| 176 // Reset the state of ProfileHandler between each test. This unregisters | 203 // Reset the state of ProfileHandler between each test. This unregisters |
| 177 // all callbacks, stops timer (if shared) and clears timer sharing state. | 204 // all callbacks, stops timer (if shared) and clears timer sharing state. |
| 178 ProfileHandlerReset(); | 205 ProfileHandlerReset(); |
| 179 EXPECT_EQ(GetCallbackCount(), 0); | 206 EXPECT_EQ(0, GetCallbackCount()); |
| 180 VerifyDisabled(); | 207 VerifyDisabled(); |
| 181 // ProfileHandler requires at least two threads to be registerd to determine | 208 // ProfileHandler requires at least two threads to be registerd to determine |
| 182 // whether timers are shared. | 209 // whether timers are shared. |
| 183 RegisterThread(); | 210 RegisterThread(); |
| 184 RegisterThread(); | 211 RegisterThread(); |
| 185 // Now that two threads are started, verify that the signal handler is | 212 // Now that two threads are started, verify that the signal handler is |
| 186 // disabled and the timers are correctly enabled/disabled. | 213 // disabled and the timers are correctly enabled/disabled. |
| 187 VerifyDisabled(); | 214 VerifyDisabled(); |
| 188 // Start worker to accumulate cpu usage. | 215 // Start worker to accumulate cpu usage. |
| 189 StartWorker(); | 216 StartWorker(); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 206 | 233 |
| 207 // Starts a busy worker thread to accumulate cpu time. There should be only | 234 // Starts a busy worker thread to accumulate cpu time. There should be only |
| 208 // one busy worker running. This is required for the case where there are | 235 // one busy worker running. This is required for the case where there are |
| 209 // separate timers for each thread. | 236 // separate timers for each thread. |
| 210 void StartWorker() { | 237 void StartWorker() { |
| 211 busy_worker_ = new BusyThread(); | 238 busy_worker_ = new BusyThread(); |
| 212 busy_worker_->SetJoinable(true); | 239 busy_worker_->SetJoinable(true); |
| 213 busy_worker_->Start(); | 240 busy_worker_->Start(); |
| 214 // Wait for worker to start up and register with the ProfileHandler. | 241 // Wait for worker to start up and register with the ProfileHandler. |
| 215 // TODO(nabeelmian) This may not work under very heavy load. | 242 // TODO(nabeelmian) This may not work under very heavy load. |
| 216 usleep(kSleepInterval); | 243 Delay(kSleepInterval); |
| 217 } | 244 } |
| 218 | 245 |
| 219 // Stops the worker thread. | 246 // Stops the worker thread. |
| 220 void StopWorker() { | 247 void StopWorker() { |
| 221 busy_worker_->set_stop_work(true); | 248 busy_worker_->set_stop_work(true); |
| 222 busy_worker_->Join(); | 249 busy_worker_->Join(); |
| 223 delete busy_worker_; | 250 delete busy_worker_; |
| 224 } | 251 } |
| 225 | 252 |
| 226 // Checks whether SIGPROF signal handler is enabled. | 253 // Checks whether SIGPROF/SIGALRM signal handler is enabled. |
| 227 bool IsSignalEnabled() { | 254 bool IsSignalEnabled() { |
| 228 struct sigaction sa; | 255 struct sigaction sa; |
| 229 CHECK_EQ(sigaction(SIGPROF, NULL, &sa), 0); | 256 CHECK_EQ(sigaction(signal_number_, NULL, &sa), 0); |
| 230 return ((sa.sa_handler == SIG_IGN) || (sa.sa_handler == SIG_DFL)) ? | 257 return ((sa.sa_handler == SIG_IGN) || (sa.sa_handler == SIG_DFL)) ? |
| 231 false : true; | 258 false : true; |
| 232 } | 259 } |
| 233 | 260 |
| 234 // Gets the number of callbacks registered with the ProfileHandler. | 261 // Gets the number of callbacks registered with the ProfileHandler. |
| 235 uint32 GetCallbackCount() { | 262 uint32 GetCallbackCount() { |
| 236 ProfileHandlerState state; | 263 ProfileHandlerState state; |
| 237 ProfileHandlerGetState(&state); | 264 ProfileHandlerGetState(&state); |
| 238 return state.callback_count; | 265 return state.callback_count; |
| 239 } | 266 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 250 void VerifyRegistration(const int& tick_counter) { | 277 void VerifyRegistration(const int& tick_counter) { |
| 251 // Check the callback count. | 278 // Check the callback count. |
| 252 EXPECT_GT(GetCallbackCount(), 0); | 279 EXPECT_GT(GetCallbackCount(), 0); |
| 253 // Check that the profile timer is enabled. | 280 // Check that the profile timer is enabled. |
| 254 EXPECT_TRUE(IsTimerEnabled()); | 281 EXPECT_TRUE(IsTimerEnabled()); |
| 255 // Check that the signal handler is enabled. | 282 // Check that the signal handler is enabled. |
| 256 EXPECT_TRUE(IsSignalEnabled()); | 283 EXPECT_TRUE(IsSignalEnabled()); |
| 257 uint64 interrupts_before = GetInterruptCount(); | 284 uint64 interrupts_before = GetInterruptCount(); |
| 258 // Sleep for a bit and check that tick counter is making progress. | 285 // Sleep for a bit and check that tick counter is making progress. |
| 259 int old_tick_count = tick_counter; | 286 int old_tick_count = tick_counter; |
| 260 usleep(kSleepInterval); | 287 Delay(kSleepInterval); |
| 261 int new_tick_count = tick_counter; | 288 int new_tick_count = tick_counter; |
| 262 EXPECT_GT(new_tick_count, old_tick_count); | 289 EXPECT_GT(new_tick_count, old_tick_count); |
| 263 uint64 interrupts_after = GetInterruptCount(); | 290 uint64 interrupts_after = GetInterruptCount(); |
| 264 EXPECT_GT(interrupts_after, interrupts_before); | 291 EXPECT_GT(interrupts_after, interrupts_before); |
| 265 } | 292 } |
| 266 | 293 |
| 267 // Verifies that a callback is not receiving profile ticks. | 294 // Verifies that a callback is not receiving profile ticks. |
| 268 void VerifyUnregistration(const int& tick_counter) { | 295 void VerifyUnregistration(const int& tick_counter) { |
| 269 // Sleep for a bit and check that tick counter is not making progress. | 296 // Sleep for a bit and check that tick counter is not making progress. |
| 270 int old_tick_count = tick_counter; | 297 int old_tick_count = tick_counter; |
| 271 usleep(kSleepInterval); | 298 Delay(kSleepInterval); |
| 272 int new_tick_count = tick_counter; | 299 int new_tick_count = tick_counter; |
| 273 EXPECT_EQ(new_tick_count, old_tick_count); | 300 EXPECT_EQ(old_tick_count, new_tick_count); |
| 274 // If no callbacks, signal handler and shared timer should be disabled. | 301 // If no callbacks, signal handler and shared timer should be disabled. |
| 275 if (GetCallbackCount() == 0) { | 302 if (GetCallbackCount() == 0) { |
| 276 EXPECT_FALSE(IsSignalEnabled()); | 303 EXPECT_FALSE(IsSignalEnabled()); |
| 277 if (timer_separate_) { | 304 if (timer_separate_) { |
| 278 EXPECT_TRUE(IsTimerEnabled()); | 305 EXPECT_TRUE(IsTimerEnabled()); |
| 279 } else { | 306 } else { |
| 280 EXPECT_FALSE(IsTimerEnabled()); | 307 EXPECT_FALSE(IsTimerEnabled()); |
| 281 } | 308 } |
| 282 } | 309 } |
| 283 } | 310 } |
| 284 | 311 |
| 285 // Verifies that the SIGPROF interrupt handler is disabled and the timer, | 312 // Verifies that the SIGPROF/SIGALRM interrupt handler is disabled and the |
| 286 // if shared, is disabled. Expects the worker to be running. | 313 // timer, if shared, is disabled. Expects the worker to be running. |
| 287 void VerifyDisabled() { | 314 void VerifyDisabled() { |
| 288 // Check that the signal handler is disabled. | 315 // Check that the signal handler is disabled. |
| 289 EXPECT_FALSE(IsSignalEnabled()); | 316 EXPECT_FALSE(IsSignalEnabled()); |
| 290 // Check that the callback count is 0. | 317 // Check that the callback count is 0. |
| 291 EXPECT_EQ(GetCallbackCount(), 0); | 318 EXPECT_EQ(0, GetCallbackCount()); |
| 292 // Check that the timer is disabled if shared, enabled otherwise. | 319 // Check that the timer is disabled if shared, enabled otherwise. |
| 293 if (timer_separate_) { | 320 if (timer_separate_) { |
| 294 EXPECT_TRUE(IsTimerEnabled()); | 321 EXPECT_TRUE(IsTimerEnabled()); |
| 295 } else { | 322 } else { |
| 296 EXPECT_FALSE(IsTimerEnabled()); | 323 EXPECT_FALSE(IsTimerEnabled()); |
| 297 } | 324 } |
| 298 // Verify that the ProfileHandler is not accumulating profile ticks. | 325 // Verify that the ProfileHandler is not accumulating profile ticks. |
| 299 uint64 interrupts_before = GetInterruptCount(); | 326 uint64 interrupts_before = GetInterruptCount(); |
| 300 usleep(kSleepInterval); | 327 Delay(kSleepInterval); |
| 301 uint64 interrupts_after = GetInterruptCount(); | 328 uint64 interrupts_after = GetInterruptCount(); |
| 302 EXPECT_EQ(interrupts_after, interrupts_before); | 329 EXPECT_EQ(interrupts_before, interrupts_after); |
| 330 } |
| 331 |
| 332 // Registers a callback and waits for kTimerResetInterval for timers to get |
| 333 // reset. |
| 334 ProfileHandlerToken* RegisterCallback(void* callback_arg) { |
| 335 ProfileHandlerToken* token = ProfileHandlerRegisterCallback( |
| 336 TickCounter, callback_arg); |
| 337 Delay(kTimerResetInterval); |
| 338 return token; |
| 339 } |
| 340 |
| 341 // Unregisters a callback and waits for kTimerResetInterval for timers to get |
| 342 // reset. |
| 343 void UnregisterCallback(ProfileHandlerToken* token) { |
| 344 ProfileHandlerUnregisterCallback(token); |
| 345 Delay(kTimerResetInterval); |
| 303 } | 346 } |
| 304 | 347 |
| 305 // Busy worker thread to accumulate cpu usage. | 348 // Busy worker thread to accumulate cpu usage. |
| 306 BusyThread* busy_worker_; | 349 BusyThread* busy_worker_; |
| 307 | 350 |
| 308 private: | 351 private: |
| 309 // The tests to run | 352 // The tests to run |
| 310 void RegisterUnregisterCallback(); | 353 void RegisterUnregisterCallback(); |
| 311 void MultipleCallbacks(); | 354 void MultipleCallbacks(); |
| 312 void Reset(); | 355 void Reset(); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 329 RUN(RegisterCallbackBeforeThread); | 372 RUN(RegisterCallbackBeforeThread); |
| 330 printf("Done\n"); | 373 printf("Done\n"); |
| 331 return 0; | 374 return 0; |
| 332 } | 375 } |
| 333 }; | 376 }; |
| 334 | 377 |
| 335 // Verifies ProfileHandlerRegisterCallback and | 378 // Verifies ProfileHandlerRegisterCallback and |
| 336 // ProfileHandlerUnregisterCallback. | 379 // ProfileHandlerUnregisterCallback. |
| 337 TEST_F(ProfileHandlerTest, RegisterUnregisterCallback) { | 380 TEST_F(ProfileHandlerTest, RegisterUnregisterCallback) { |
| 338 int tick_count = 0; | 381 int tick_count = 0; |
| 339 ProfileHandlerToken* token = ProfileHandlerRegisterCallback( | 382 ProfileHandlerToken* token = RegisterCallback(&tick_count); |
| 340 TickCounter, &tick_count); | |
| 341 VerifyRegistration(tick_count); | 383 VerifyRegistration(tick_count); |
| 342 ProfileHandlerUnregisterCallback(token); | 384 UnregisterCallback(token); |
| 343 VerifyUnregistration(tick_count); | 385 VerifyUnregistration(tick_count); |
| 344 } | 386 } |
| 345 | 387 |
| 346 // Verifies that multiple callbacks can be registered. | 388 // Verifies that multiple callbacks can be registered. |
| 347 TEST_F(ProfileHandlerTest, MultipleCallbacks) { | 389 TEST_F(ProfileHandlerTest, MultipleCallbacks) { |
| 348 // Register first callback. | 390 // Register first callback. |
| 349 int first_tick_count; | 391 int first_tick_count; |
| 350 ProfileHandlerToken* token1 = ProfileHandlerRegisterCallback( | 392 ProfileHandlerToken* token1 = RegisterCallback(&first_tick_count); |
| 351 TickCounter, &first_tick_count); | |
| 352 // Check that callback was registered correctly. | 393 // Check that callback was registered correctly. |
| 353 VerifyRegistration(first_tick_count); | 394 VerifyRegistration(first_tick_count); |
| 354 EXPECT_EQ(GetCallbackCount(), 1); | 395 EXPECT_EQ(1, GetCallbackCount()); |
| 355 | 396 |
| 356 // Register second callback. | 397 // Register second callback. |
| 357 int second_tick_count; | 398 int second_tick_count; |
| 358 ProfileHandlerToken* token2 = ProfileHandlerRegisterCallback( | 399 ProfileHandlerToken* token2 = RegisterCallback(&second_tick_count); |
| 359 TickCounter, &second_tick_count); | |
| 360 // Check that callback was registered correctly. | 400 // Check that callback was registered correctly. |
| 361 VerifyRegistration(second_tick_count); | 401 VerifyRegistration(second_tick_count); |
| 362 EXPECT_EQ(GetCallbackCount(), 2); | 402 EXPECT_EQ(2, GetCallbackCount()); |
| 363 | 403 |
| 364 // Unregister first callback. | 404 // Unregister first callback. |
| 365 ProfileHandlerUnregisterCallback(token1); | 405 UnregisterCallback(token1); |
| 366 VerifyUnregistration(first_tick_count); | 406 VerifyUnregistration(first_tick_count); |
| 367 EXPECT_EQ(GetCallbackCount(), 1); | 407 EXPECT_EQ(1, GetCallbackCount()); |
| 368 // Verify that second callback is still registered. | 408 // Verify that second callback is still registered. |
| 369 VerifyRegistration(second_tick_count); | 409 VerifyRegistration(second_tick_count); |
| 370 | 410 |
| 371 // Unregister second callback. | 411 // Unregister second callback. |
| 372 ProfileHandlerUnregisterCallback(token2); | 412 UnregisterCallback(token2); |
| 373 VerifyUnregistration(second_tick_count); | 413 VerifyUnregistration(second_tick_count); |
| 374 EXPECT_EQ(GetCallbackCount(), 0); | 414 EXPECT_EQ(0, GetCallbackCount()); |
| 375 | 415 |
| 376 // Verify that the signal handler and timers are correctly disabled. | 416 // Verify that the signal handler and timers are correctly disabled. |
| 377 VerifyDisabled(); | 417 VerifyDisabled(); |
| 378 } | 418 } |
| 379 | 419 |
| 380 // Verifies ProfileHandlerReset | 420 // Verifies ProfileHandlerReset |
| 381 TEST_F(ProfileHandlerTest, Reset) { | 421 TEST_F(ProfileHandlerTest, Reset) { |
| 382 // Verify that the profile timer interrupt is disabled. | 422 // Verify that the profile timer interrupt is disabled. |
| 383 VerifyDisabled(); | 423 VerifyDisabled(); |
| 384 int first_tick_count; | 424 int first_tick_count; |
| 385 ProfileHandlerRegisterCallback(TickCounter, &first_tick_count); | 425 RegisterCallback(&first_tick_count); |
| 386 VerifyRegistration(first_tick_count); | 426 VerifyRegistration(first_tick_count); |
| 387 EXPECT_EQ(GetCallbackCount(), 1); | 427 EXPECT_EQ(1, GetCallbackCount()); |
| 388 | 428 |
| 389 // Register second callback. | 429 // Register second callback. |
| 390 int second_tick_count; | 430 int second_tick_count; |
| 391 ProfileHandlerRegisterCallback(TickCounter, &second_tick_count); | 431 RegisterCallback(&second_tick_count); |
| 392 VerifyRegistration(second_tick_count); | 432 VerifyRegistration(second_tick_count); |
| 393 EXPECT_EQ(GetCallbackCount(), 2); | 433 EXPECT_EQ(2, GetCallbackCount()); |
| 394 | 434 |
| 395 // Reset the profile handler and verify that callback were correctly | 435 // Reset the profile handler and verify that callback were correctly |
| 396 // unregistered and timer/signal are disabled. | 436 // unregistered and timer/signal are disabled. |
| 397 ProfileHandlerReset(); | 437 ProfileHandlerReset(); |
| 398 VerifyUnregistration(first_tick_count); | 438 VerifyUnregistration(first_tick_count); |
| 399 VerifyUnregistration(second_tick_count); | 439 VerifyUnregistration(second_tick_count); |
| 400 VerifyDisabled(); | 440 VerifyDisabled(); |
| 401 } | 441 } |
| 402 | 442 |
| 403 // Verifies that ProfileHandler correctly handles a case where a callback was | 443 // Verifies that ProfileHandler correctly handles a case where a callback was |
| 404 // registered before the second thread started. | 444 // registered before the second thread started. |
| 405 TEST_F(ProfileHandlerTest, RegisterCallbackBeforeThread) { | 445 TEST_F(ProfileHandlerTest, RegisterCallbackBeforeThread) { |
| 406 // Stop the worker. | 446 // Stop the worker. |
| 407 StopWorker(); | 447 StopWorker(); |
| 408 // Unregister all existing callbacks, stop the timer (if shared), disable | 448 // Unregister all existing callbacks, stop the timer (if shared), disable |
| 409 // the signal handler and reset the timer sharing state in the Profile | 449 // the signal handler and reset the timer sharing state in the Profile |
| 410 // Handler. | 450 // Handler. |
| 411 ProfileHandlerReset(); | 451 ProfileHandlerReset(); |
| 412 EXPECT_EQ(GetCallbackCount(), 0); | 452 EXPECT_EQ(0, GetCallbackCount()); |
| 413 VerifyDisabled(); | 453 VerifyDisabled(); |
| 414 | 454 |
| 415 // Start the worker. At this time ProfileHandler doesn't know if timers are | 455 // Start the worker. At this time ProfileHandler doesn't know if timers are |
| 416 // shared as only one thread has registered so far. | 456 // shared as only one thread has registered so far. |
| 417 StartWorker(); | 457 StartWorker(); |
| 418 // Register a callback and check that profile ticks are being delivered. | 458 // Register a callback and check that profile ticks are being delivered. |
| 419 int tick_count; | 459 int tick_count; |
| 420 ProfileHandlerRegisterCallback(TickCounter, &tick_count); | 460 RegisterCallback(&tick_count); |
| 421 EXPECT_EQ(GetCallbackCount(), 1); | 461 EXPECT_EQ(1, GetCallbackCount()); |
| 422 VerifyRegistration(tick_count); | 462 VerifyRegistration(tick_count); |
| 423 | 463 |
| 424 // Register a second thread and verify that timer and signal handler are | 464 // Register a second thread and verify that timer and signal handler are |
| 425 // correctly enabled. | 465 // correctly enabled. |
| 426 RegisterThread(); | 466 RegisterThread(); |
| 427 EXPECT_EQ(GetCallbackCount(), 1); | 467 EXPECT_EQ(1, GetCallbackCount()); |
| 428 EXPECT_TRUE(IsTimerEnabled()); | 468 EXPECT_TRUE(IsTimerEnabled()); |
| 429 EXPECT_TRUE(IsSignalEnabled()); | 469 EXPECT_TRUE(IsSignalEnabled()); |
| 430 } | 470 } |
| 431 | 471 |
| 432 } // namespace | 472 } // namespace |
| 433 | 473 |
| 434 int main(int argc, char** argv) { | 474 int main(int argc, char** argv) { |
| 435 return ProfileHandlerTest::RUN_ALL_TESTS(); | 475 return ProfileHandlerTest::RUN_ALL_TESTS(); |
| 436 } | 476 } |
| OLD | NEW |