OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 #include "net/log/file_net_log_observer.h" | 5 #include "net/log/file_net_log_observer.h" |
6 | 6 |
7 #include <math.h> | 7 #include <math.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 #include <string> | 10 #include <string> |
(...skipping 774 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
785 ASSERT_TRUE(std::all_of(std::begin(events_written), std::end(events_written), | 785 ASSERT_TRUE(std::all_of(std::begin(events_written), std::end(events_written), |
786 [](bool j) { return j; })); | 786 [](bool j) { return j; })); |
787 | 787 |
788 // Check that there are events written to all files. | 788 // Check that there are events written to all files. |
789 for (int i = 0; i < kTotalNumFiles; i++) { | 789 for (int i = 0; i < kTotalNumFiles; i++) { |
790 ASSERT_GE(GetFileSize(GetEventFilePath(i)), | 790 ASSERT_GE(GetFileSize(GetEventFilePath(i)), |
791 static_cast<int64_t>(kEventSize)); | 791 static_cast<int64_t>(kEventSize)); |
792 } | 792 } |
793 } | 793 } |
794 | 794 |
| 795 void AddEntriesViaNetLog(NetLog* net_log, int num_entries) { |
| 796 for (int i = 0; i < num_entries; i++) { |
| 797 net_log->AddGlobalEntry(NetLogEventType::PAC_JAVASCRIPT_ERROR); |
| 798 } |
| 799 } |
| 800 |
| 801 TEST_P(FileNetLogObserverTest, AddEventsFromMultipleThreadsWithStopObserving) { |
| 802 const size_t kNumThreads = 10; |
| 803 std::vector<std::unique_ptr<base::Thread>> threads(kNumThreads); |
| 804 // Start all the threads. Waiting for them to start is to hopefuly improve |
| 805 // the odds of hitting interesting races once events start being added. |
| 806 for (size_t i = 0; i < threads.size(); ++i) { |
| 807 threads[i] = base::MakeUnique<base::Thread>( |
| 808 base::StringPrintf("WorkerThread%i", static_cast<int>(i))); |
| 809 threads[i]->Start(); |
| 810 threads[i]->WaitUntilThreadStarted(); |
| 811 } |
| 812 |
| 813 CreateAndStartObserving(nullptr); |
| 814 |
| 815 const size_t kNumEventsAddedPerThread = 200; |
| 816 |
| 817 // Add events in parallel from all the threads. |
| 818 for (size_t i = 0; i < kNumThreads; ++i) { |
| 819 threads[i]->task_runner()->PostTask( |
| 820 FROM_HERE, base::Bind(&AddEntriesViaNetLog, base::Unretained(&net_log_), |
| 821 kNumEventsAddedPerThread)); |
| 822 } |
| 823 |
| 824 // Stop observing. |
| 825 TestClosure closure; |
| 826 logger_->StopObserving(nullptr, closure.closure()); |
| 827 closure.WaitForResult(); |
| 828 |
| 829 // Join all the threads. |
| 830 threads.clear(); |
| 831 } |
| 832 |
| 833 TEST_P(FileNetLogObserverTest, |
| 834 AddEventsFromMultipleThreadsWithoutStopObserving) { |
| 835 const size_t kNumThreads = 10; |
| 836 std::vector<std::unique_ptr<base::Thread>> threads(kNumThreads); |
| 837 // Start all the threads. Waiting for them to start is to hopefuly improve |
| 838 // the odds of hitting interesting races once events start being added. |
| 839 for (size_t i = 0; i < threads.size(); ++i) { |
| 840 threads[i] = base::MakeUnique<base::Thread>( |
| 841 base::StringPrintf("WorkerThread%i", static_cast<int>(i))); |
| 842 threads[i]->Start(); |
| 843 threads[i]->WaitUntilThreadStarted(); |
| 844 } |
| 845 |
| 846 CreateAndStartObserving(nullptr); |
| 847 |
| 848 const size_t kNumEventsAddedPerThread = 200; |
| 849 |
| 850 // Add events in parallel from all the threads. |
| 851 for (size_t i = 0; i < kNumThreads; ++i) { |
| 852 threads[i]->task_runner()->PostTask( |
| 853 FROM_HERE, base::Bind(&AddEntriesViaNetLog, base::Unretained(&net_log_), |
| 854 kNumEventsAddedPerThread)); |
| 855 } |
| 856 |
| 857 // Destroy logger. |
| 858 logger_.reset(); |
| 859 |
| 860 // Join all the threads. |
| 861 threads.clear(); |
| 862 } |
| 863 |
795 } // namespace | 864 } // namespace |
796 | 865 |
797 } // namespace net | 866 } // namespace net |
OLD | NEW |