1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 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 "chrome/browser/net/chrome_net_log.h" 6 7 #include "base/synchronization/waitable_event.h" 8 #include "base/threading/simple_thread.h" 9 #include "testing/gtest/include/gtest/gtest.h" 10 11 namespace { 12 13 const int kThreads = 10; 14 const int kEvents = 100; 15 16 class ChromeNetLogTestThread : public base::SimpleThread { 17 public: 18 ChromeNetLogTestThread() : base::SimpleThread("ChromeNetLogTest"), 19 can_start_loop_(false, false), 20 log_(NULL) { 21 } 22 23 void Init(ChromeNetLog* log) { 24 log_ = log; 25 } 26 27 virtual void Run() { 28 can_start_loop_.Wait(); 29 for (int i = 0; i < kEvents; ++i) { 30 net::NetLog::Source source(net::NetLog::SOURCE_SOCKET, log_->NextID()); 31 log_->AddEntry(net::NetLog::TYPE_SOCKET_ALIVE, base::TimeTicks(), 32 source, net::NetLog::PHASE_BEGIN, NULL); 33 } 34 log_->ClearAllPassivelyCapturedEvents(); 35 } 36 37 void ReallyStart() { 38 can_start_loop_.Signal(); 39 } 40 41 private: 42 // Only triggered once all threads have been created, to make it much less 43 // likely each thread completes before the next one starts. 44 base::WaitableEvent can_start_loop_; 45 46 ChromeNetLog* log_; 47 48 DISALLOW_COPY_AND_ASSIGN(ChromeNetLogTestThread); 49 }; 50 51 } // namespace 52 53 // Attempts to check thread safety, exercising checks in ChromeNetLog and 54 // PassiveLogCollector. 55 TEST(ChromeNetLogTest, NetLogThreads) { 56 ChromeNetLog log; 57 ChromeNetLogTestThread threads[kThreads]; 58 59 for (int i = 0; i < kThreads; ++i) { 60 threads[i].Init(&log); 61 threads[i].Start(); 62 } 63 64 for (int i = 0; i < kThreads; ++i) 65 threads[i].ReallyStart(); 66 67 for (int i = 0; i < kThreads; ++i) 68 threads[i].Join(); 69 70 ChromeNetLog::EntryList entries; 71 log.GetAllPassivelyCapturedEvents(&entries); 72 EXPECT_EQ(0u, entries.size()); 73 } 74