OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Test of classes in the tracked_objects.h classes. | 5 // Test of classes in the tracked_objects.h classes. |
6 | 6 |
7 #include "base/tracked_objects.h" | 7 #include "base/tracked_objects.h" |
8 | 8 |
9 #include "base/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 587 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
598 "\"file_name\":\"AnotherFileName\"," | 598 "\"file_name\":\"AnotherFileName\"," |
599 "\"function_name\":\"DifferentLives\"," | 599 "\"function_name\":\"DifferentLives\"," |
600 "\"line_number\":999" | 600 "\"line_number\":999" |
601 "}" | 601 "}" |
602 "}" | 602 "}" |
603 "]" | 603 "]" |
604 "}"; | 604 "}"; |
605 EXPECT_EQ(one_line_result, json); | 605 EXPECT_EQ(one_line_result, json); |
606 } | 606 } |
607 | 607 |
| 608 TEST_F(TrackedObjectsTest, TrackedTimerMilliseconds) { |
| 609 // First make sure we basicallly transfer simple milliseconds values as |
| 610 // expected. Most critically, things should not become null. |
| 611 int32 kSomeMilliseconds = 243; // Some example times. |
| 612 int64 kReallyBigMilliseconds = (1LL << 35) + kSomeMilliseconds; |
| 613 |
| 614 TrackedTime some = TrackedTime() + |
| 615 Duration::FromMilliseconds(kSomeMilliseconds); |
| 616 EXPECT_EQ(kSomeMilliseconds, (some - TrackedTime()).InMilliseconds()); |
| 617 EXPECT_FALSE(some.is_null()); |
| 618 |
| 619 // Now create a big time, to check that it is wrapped modulo 2^32. |
| 620 base::TimeTicks big = base::TimeTicks() + |
| 621 base::TimeDelta::FromMilliseconds(kReallyBigMilliseconds); |
| 622 EXPECT_EQ(kReallyBigMilliseconds, (big - base::TimeTicks()).InMilliseconds()); |
| 623 |
| 624 TrackedTime wrapped_big(big); |
| 625 #if defined(USE_FAST_TIME_CLASS_FOR_DURATION_CALCULATIONS) |
| 626 // Expect wrapping at 32 bits. |
| 627 EXPECT_EQ(kSomeMilliseconds, (wrapped_big - TrackedTime()).InMilliseconds()); |
| 628 #else // !USE_FAST_TIME_CLASS_FOR_DURATION_CALCULATIONS) |
| 629 // Expect no wrapping at 32 bits. |
| 630 EXPECT_EQ(kReallyBigMilliseconds, |
| 631 (wrapped_big - TrackedTime()).InMilliseconds()); |
| 632 #endif // USE_FAST_TIME_CLASS_FOR_DURATION_CALCULATIONS) |
| 633 } |
| 634 |
| 635 TEST_F(TrackedObjectsTest, TrackedTimerDuration) { |
| 636 int kFirstMilliseconds = 793; |
| 637 int kSecondMilliseconds = 14889; |
| 638 |
| 639 Duration first = Duration::FromMilliseconds(kFirstMilliseconds); |
| 640 Duration second = Duration::FromMilliseconds(kSecondMilliseconds); |
| 641 |
| 642 EXPECT_EQ(kFirstMilliseconds, first.InMilliseconds()); |
| 643 EXPECT_EQ(kSecondMilliseconds, second.InMilliseconds()); |
| 644 |
| 645 Duration sum = first + second; |
| 646 EXPECT_EQ(kFirstMilliseconds + kSecondMilliseconds, sum.InMilliseconds()); |
| 647 } |
| 648 |
| 649 TEST_F(TrackedObjectsTest, TrackedTimerVsTimeTicks) { |
| 650 // Make sure that our 32 bit timer is aligned with the TimeTicks() timer. |
| 651 |
| 652 // First get a 64 bit timer (which should not be null). |
| 653 base::TimeTicks ticks_before = base::TimeTicks::Now(); |
| 654 EXPECT_FALSE(ticks_before.is_null()); |
| 655 |
| 656 // Then get a 32 bit timer that can be be null when it wraps. |
| 657 TrackedTime now = TrackedTime::Now(); |
| 658 |
| 659 // Then get a bracketing time. |
| 660 base::TimeTicks ticks_after = base::TimeTicks::Now(); |
| 661 EXPECT_FALSE(ticks_after.is_null()); |
| 662 |
| 663 // Now make sure that we bracketed our tracked time nicely. |
| 664 Duration before = now - TrackedTime(ticks_before); |
| 665 EXPECT_LE(0, before.InMilliseconds()); |
| 666 Duration after = now - TrackedTime(ticks_after); |
| 667 EXPECT_GE(0, after.InMilliseconds()); |
| 668 } |
| 669 |
| 670 TEST_F(TrackedObjectsTest, TrackedTimerDisabled) { |
| 671 // Check to be sure disabling the collection of data induces a null time |
| 672 // (which we know will return much faster). |
| 673 if (!ThreadData::InitializeAndSetTrackingStatus(false)) |
| 674 return; |
| 675 // Since we disabled tracking, we should get a null response. |
| 676 TrackedTime track_now = ThreadData::Now(); |
| 677 EXPECT_TRUE(track_now.is_null()); |
| 678 } |
| 679 |
| 680 TEST_F(TrackedObjectsTest, TrackedTimerEnabled) { |
| 681 if (!ThreadData::InitializeAndSetTrackingStatus(true)) |
| 682 return; |
| 683 // Make sure that when we enable tracking, we get a real timer result. |
| 684 |
| 685 // First get a 64 bit timer (which should not be null). |
| 686 base::TimeTicks ticks_before = base::TimeTicks::Now(); |
| 687 EXPECT_FALSE(ticks_before.is_null()); |
| 688 |
| 689 // Then get a 32 bit timer that can be null when it wraps. |
| 690 // Crtical difference from the TrackedTimerVsTimeTicks test, is that we use |
| 691 // ThreadData::Now(). It can sometimes return the null time. |
| 692 TrackedTime now = ThreadData::Now(); |
| 693 |
| 694 // Then get a bracketing time. |
| 695 base::TimeTicks ticks_after = base::TimeTicks::Now(); |
| 696 EXPECT_FALSE(ticks_after.is_null()); |
| 697 |
| 698 // Now make sure that we bracketed our tracked time nicely. |
| 699 Duration before = now - TrackedTime(ticks_before); |
| 700 EXPECT_LE(0, before.InMilliseconds()); |
| 701 Duration after = now - TrackedTime(ticks_after); |
| 702 EXPECT_GE(0, after.InMilliseconds()); |
| 703 } |
| 704 |
| 705 |
608 } // namespace tracked_objects | 706 } // namespace tracked_objects |
OLD | NEW |