Commit fc4bcbaa authored by eroman's avatar eroman Committed by Commit Bot

Remove the file thread dependency from FileNetLogObserver.

Instead use an internally created sequenced task runer.

BUG=689520
CQ_INCLUDE_TRYBOTS=master.tryserver.chromium.android:android_cronet_tester

Review-Url: https://codereview.chromium.org/2966283002
Cr-Commit-Position: refs/heads/master@{#485105}
parent 43ce1648
...@@ -1027,7 +1027,7 @@ void CronetURLRequestContextAdapter::StartNetLogOnNetworkThread( ...@@ -1027,7 +1027,7 @@ void CronetURLRequestContextAdapter::StartNetLogOnNetworkThread(
if (net_log_file_observer_) if (net_log_file_observer_)
return; return;
net_log_file_observer_ = net::FileNetLogObserver::CreateUnbounded( net_log_file_observer_ = net::FileNetLogObserver::CreateUnbounded(
GetFileThread()->task_runner(), file_path, /*constants=*/nullptr); file_path, /*constants=*/nullptr);
CreateNetLogEntriesForActiveObjects({context_.get()}, CreateNetLogEntriesForActiveObjects({context_.get()},
net_log_file_observer_.get()); net_log_file_observer_.get());
net::NetLogCaptureMode capture_mode = net::NetLogCaptureMode capture_mode =
...@@ -1052,8 +1052,7 @@ void CronetURLRequestContextAdapter::StartNetLogToBoundedFileOnNetworkThread( ...@@ -1052,8 +1052,7 @@ void CronetURLRequestContextAdapter::StartNetLogToBoundedFileOnNetworkThread(
DCHECK(base::PathIsWritable(file_path)); DCHECK(base::PathIsWritable(file_path));
net_log_file_observer_ = net::FileNetLogObserver::CreateBounded( net_log_file_observer_ = net::FileNetLogObserver::CreateBounded(
GetFileThread()->task_runner(), file_path, size, kNumNetLogEventFiles, file_path, size, kNumNetLogEventFiles, /*constants=*/nullptr);
/*constants=*/nullptr);
CreateNetLogEntriesForActiveObjects({context_.get()}, CreateNetLogEntriesForActiveObjects({context_.get()},
net_log_file_observer_.get()); net_log_file_observer_.get());
......
...@@ -184,9 +184,9 @@ void NetExportFileWriter::StartNetLog( ...@@ -184,9 +184,9 @@ void NetExportFileWriter::StartNetLog(
std::unique_ptr<base::Value> constants( std::unique_ptr<base::Value> constants(
ChromeNetLog::GetConstants(command_line_string, channel_string)); ChromeNetLog::GetConstants(command_line_string, channel_string));
// Instantiate a FileNetLogObserver in unbounded mode.
file_net_log_observer_ = net::FileNetLogObserver::CreateUnbounded( file_net_log_observer_ =
file_task_runner_, log_path_, std::move(constants)); net::FileNetLogObserver::CreateUnbounded(log_path_, std::move(constants));
net_task_runner_->PostTaskAndReply( net_task_runner_->PostTaskAndReply(
FROM_HERE, FROM_HERE,
......
...@@ -16,8 +16,7 @@ ...@@ -16,8 +16,7 @@
#include "base/files/scoped_temp_dir.h" #include "base/files/scoped_temp_dir.h"
#include "base/json/json_reader.h" #include "base/json/json_reader.h"
#include "base/json/json_string_value_serializer.h" #include "base/json/json_string_value_serializer.h"
#include "base/message_loop/message_loop.h" #include "base/test/scoped_task_environment.h"
#include "base/run_loop.h"
#include "base/threading/thread.h" #include "base/threading/thread.h"
#include "base/threading/thread_task_runner_handle.h" #include "base/threading/thread_task_runner_handle.h"
#include "base/values.h" #include "base/values.h"
...@@ -437,8 +436,7 @@ class NetExportFileWriterTest : public ::testing::Test { ...@@ -437,8 +436,7 @@ class NetExportFileWriterTest : public ::testing::Test {
TestStateObserver test_state_observer_; TestStateObserver test_state_observer_;
private: private:
// Allows tasks to be posted to the main thread. base::test::ScopedTaskEnvironment scoped_task_environment_;
base::MessageLoop message_loop_;
}; };
TEST_F(NetExportFileWriterTest, InitFail) { TEST_F(NetExportFileWriterTest, InitFail) {
......
This diff is collapsed.
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
namespace base { namespace base {
class Value; class Value;
class FilePath; class FilePath;
class SingleThreadTaskRunner; class SequencedTaskRunner;
} // namespace base } // namespace base
namespace net { namespace net {
...@@ -49,10 +49,7 @@ class NET_EXPORT FileNetLogObserver : public NetLog::ThreadSafeObserver { ...@@ -49,10 +49,7 @@ class NET_EXPORT FileNetLogObserver : public NetLog::ThreadSafeObserver {
public: public:
// Creates a FileNetLogObserver in bounded mode. // Creates a FileNetLogObserver in bounded mode.
// //
// |file_task_runner| indicates the task runner that should be used to post // |directory| is the directory where the log files will be written to.
// tasks from the main thread to the file thread.
//
// |directory| is the directory where the log files will be.
// //
// |max_total_size| is the approximate limit on the cumulative size of all // |max_total_size| is the approximate limit on the cumulative size of all
// netlog files. // netlog files.
...@@ -64,7 +61,6 @@ class NET_EXPORT FileNetLogObserver : public NetLog::ThreadSafeObserver { ...@@ -64,7 +61,6 @@ class NET_EXPORT FileNetLogObserver : public NetLog::ThreadSafeObserver {
// the log. It should generally be a modified version of GetNetConstants(). // the log. It should generally be a modified version of GetNetConstants().
// If not present, the output of GetNetConstants() will be used. // If not present, the output of GetNetConstants() will be used.
static std::unique_ptr<FileNetLogObserver> CreateBounded( static std::unique_ptr<FileNetLogObserver> CreateBounded(
scoped_refptr<base::SingleThreadTaskRunner> file_task_runner,
const base::FilePath& directory, const base::FilePath& directory,
size_t max_total_size, size_t max_total_size,
size_t total_num_files, size_t total_num_files,
...@@ -72,16 +68,12 @@ class NET_EXPORT FileNetLogObserver : public NetLog::ThreadSafeObserver { ...@@ -72,16 +68,12 @@ class NET_EXPORT FileNetLogObserver : public NetLog::ThreadSafeObserver {
// Creates a FileNetLogObserver in unbounded mode. // Creates a FileNetLogObserver in unbounded mode.
// //
// |file_task_runner| indicates the task runner that should be used to post // |log_path| is where the log file will be written to.
// tasks from the main thread to the file thread.
//
// |log_path| is where the log file will be.
// //
// |constants| is an optional legend for decoding constant values used in // |constants| is an optional legend for decoding constant values used in
// the log. It should generally be a modified version of GetNetConstants(). // the log. It should generally be a modified version of GetNetConstants().
// If not present, the output of GetNetConstants() will be used. // If not present, the output of GetNetConstants() will be used.
static std::unique_ptr<FileNetLogObserver> CreateUnbounded( static std::unique_ptr<FileNetLogObserver> CreateUnbounded(
scoped_refptr<base::SingleThreadTaskRunner> file_task_runner,
const base::FilePath& log_path, const base::FilePath& log_path,
std::unique_ptr<base::Value> constants); std::unique_ptr<base::Value> constants);
...@@ -113,29 +105,26 @@ class NET_EXPORT FileNetLogObserver : public NetLog::ThreadSafeObserver { ...@@ -113,29 +105,26 @@ class NET_EXPORT FileNetLogObserver : public NetLog::ThreadSafeObserver {
class BoundedFileWriter; class BoundedFileWriter;
class UnboundedFileWriter; class UnboundedFileWriter;
FileNetLogObserver( FileNetLogObserver(scoped_refptr<base::SequencedTaskRunner> file_task_runner,
scoped_refptr<base::SingleThreadTaskRunner> file_task_runner, std::unique_ptr<FileWriter> file_writer,
std::unique_ptr<FileWriter> file_writer, scoped_refptr<WriteQueue> write_queue,
scoped_refptr<WriteQueue> write_queue, std::unique_ptr<base::Value> constants);
std::unique_ptr<base::Value> constants);
scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_; scoped_refptr<base::SequencedTaskRunner> file_task_runner_;
// The |write_queue_| object is shared between the file thread and the main // The |write_queue_| object is shared between the file task runner and the
// thread, and should be alive for the entirety of the observer's lifetime. // main thread, and should be alive for the entirety of the observer's
// It should be destroyed once both the observer has been destroyed and all // lifetime. It should be destroyed once both the observer has been destroyed
// tasks posted to the file thread have completed. // and all tasks posted to the file task runner have completed.
scoped_refptr<WriteQueue> write_queue_; scoped_refptr<WriteQueue> write_queue_;
// This is the owning reference to a file thread object. The observer is // The FileNetLogObserver is shared between the main thread and
// responsible for destroying the file thread object by posting a task from // |file_task_runner_|.
// the main thread to the file thread to destroy the FileWriter when the
// observer is destroyed.
// //
// The use of base::Unretained with |file_writer_| to post tasks to the file // Conceptually FileNetLogObserver owns it, however on destruction its
// thread is safe because the FileWriter object will be alive until the // deletion is deferred until outstanding tasks on |file_task_runner_| have
// observer's destruction. // finished (since it is posted using base::Unretained()).
FileWriter* file_writer_; std::unique_ptr<FileWriter> file_writer_;
DISALLOW_COPY_AND_ASSIGN(FileNetLogObserver); DISALLOW_COPY_AND_ASSIGN(FileNetLogObserver);
}; };
......
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
#include "base/message_loop/message_loop.h" #include "base/message_loop/message_loop.h"
#include "base/strings/string_util.h" #include "base/strings/string_util.h"
#include "base/strings/stringprintf.h" #include "base/strings/stringprintf.h"
#include "base/task_scheduler/task_scheduler.h"
#include "base/threading/thread.h" #include "base/threading/thread.h"
#include "base/values.h" #include "base/values.h"
#include "net/base/test_completion_callback.h" #include "net/base/test_completion_callback.h"
...@@ -159,21 +160,16 @@ class FileNetLogObserverTest : public ::testing::TestWithParam<bool> { ...@@ -159,21 +160,16 @@ class FileNetLogObserverTest : public ::testing::TestWithParam<bool> {
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
bounded_log_dir_ = temp_dir_.GetPath(); bounded_log_dir_ = temp_dir_.GetPath();
unbounded_log_path_ = bounded_log_dir_.AppendASCII("net-log.json"); unbounded_log_path_ = bounded_log_dir_.AppendASCII("net-log.json");
file_thread_.reset(new base::Thread("NetLog File Thread"));
file_thread_->StartWithOptions(
base::Thread::Options(base::MessageLoop::TYPE_DEFAULT, 0));
ASSERT_TRUE(file_thread_->WaitUntilThreadStarted());
} }
void CreateAndStartObserving(std::unique_ptr<base::Value> constants) { void CreateAndStartObserving(std::unique_ptr<base::Value> constants) {
bool bounded = GetParam(); bool bounded = GetParam();
if (bounded) { if (bounded) {
logger_ = FileNetLogObserver::CreateBounded( logger_ = FileNetLogObserver::CreateBounded(
file_thread_->task_runner(), bounded_log_dir_, kLargeFileSize, bounded_log_dir_, kLargeFileSize, kTotalNumFiles,
kTotalNumFiles, std::move(constants)); std::move(constants));
} else { } else {
logger_ = FileNetLogObserver::CreateUnbounded(file_thread_->task_runner(), logger_ = FileNetLogObserver::CreateUnbounded(unbounded_log_path_,
unbounded_log_path_,
std::move(constants)); std::move(constants));
} }
...@@ -194,6 +190,11 @@ class FileNetLogObserverTest : public ::testing::TestWithParam<bool> { ...@@ -194,6 +190,11 @@ class FileNetLogObserverTest : public ::testing::TestWithParam<bool> {
} }
bool LogFilesExist() { bool LogFilesExist() {
// The log files are written by a sequenced task runner. Drain all the
// scheduled tasks to ensure that the file writing ones have run before
// checking if they exist.
base::TaskScheduler::GetInstance()->FlushForTesting();
bool bounded = GetParam(); bool bounded = GetParam();
if (bounded) { if (bounded) {
if (base::PathExists(bounded_log_dir_.AppendASCII("constants.json")) || if (base::PathExists(bounded_log_dir_.AppendASCII("constants.json")) ||
...@@ -212,7 +213,6 @@ class FileNetLogObserverTest : public ::testing::TestWithParam<bool> { ...@@ -212,7 +213,6 @@ class FileNetLogObserverTest : public ::testing::TestWithParam<bool> {
protected: protected:
NetLog net_log_; NetLog net_log_;
std::unique_ptr<base::Thread> file_thread_;
std::unique_ptr<FileNetLogObserver> logger_; std::unique_ptr<FileNetLogObserver> logger_;
private: private:
...@@ -227,18 +227,13 @@ class FileNetLogObserverBoundedTest : public ::testing::Test { ...@@ -227,18 +227,13 @@ class FileNetLogObserverBoundedTest : public ::testing::Test {
void SetUp() override { void SetUp() override {
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
bounded_log_dir_ = temp_dir_.GetPath(); bounded_log_dir_ = temp_dir_.GetPath();
file_thread_.reset(new base::Thread("NetLog File Thread"));
file_thread_->StartWithOptions(
base::Thread::Options(base::MessageLoop::TYPE_DEFAULT, 0));
ASSERT_TRUE(file_thread_->WaitUntilThreadStarted());
} }
void CreateAndStartObserving(std::unique_ptr<base::Value> constants, void CreateAndStartObserving(std::unique_ptr<base::Value> constants,
int total_file_size, int total_file_size,
int num_files) { int num_files) {
logger_ = FileNetLogObserver::CreateBounded( logger_ = FileNetLogObserver::CreateBounded(
file_thread_->task_runner(), bounded_log_dir_, total_file_size, bounded_log_dir_, total_file_size, num_files, std::move(constants));
num_files, std::move(constants));
logger_->StartObserving(&net_log_, NetLogCaptureMode::Default()); logger_->StartObserving(&net_log_, NetLogCaptureMode::Default());
} }
...@@ -263,7 +258,6 @@ class FileNetLogObserverBoundedTest : public ::testing::Test { ...@@ -263,7 +258,6 @@ class FileNetLogObserverBoundedTest : public ::testing::Test {
protected: protected:
NetLog net_log_; NetLog net_log_;
std::unique_ptr<base::Thread> file_thread_;
std::unique_ptr<FileNetLogObserver> logger_; std::unique_ptr<FileNetLogObserver> logger_;
private: private:
...@@ -276,15 +270,20 @@ INSTANTIATE_TEST_CASE_P(, ...@@ -276,15 +270,20 @@ INSTANTIATE_TEST_CASE_P(,
FileNetLogObserverTest, FileNetLogObserverTest,
::testing::Values(true, false)); ::testing::Values(true, false));
// Tests deleting a FileNetLogObserver without first calling StopObserving().
TEST_P(FileNetLogObserverTest, ObserverDestroyedWithoutStopObserving) { TEST_P(FileNetLogObserverTest, ObserverDestroyedWithoutStopObserving) {
CreateAndStartObserving(nullptr); CreateAndStartObserving(nullptr);
// Send dummy event // Send dummy event
AddEntries(logger_.get(), 1, kDummyEventSize); AddEntries(logger_.get(), 1, kDummyEventSize);
// The log files should have been started.
ASSERT_TRUE(LogFilesExist());
logger_.reset(); logger_.reset();
file_thread_.reset();
// When the logger is re-set without having called StopObserving(), the
// partially written log files are deleted.
ASSERT_FALSE(LogFilesExist()); ASSERT_FALSE(LogFilesExist());
} }
...@@ -801,7 +800,7 @@ void AddEntriesViaNetLog(NetLog* net_log, int num_entries) { ...@@ -801,7 +800,7 @@ void AddEntriesViaNetLog(NetLog* net_log, int num_entries) {
TEST_P(FileNetLogObserverTest, AddEventsFromMultipleThreadsWithStopObserving) { TEST_P(FileNetLogObserverTest, AddEventsFromMultipleThreadsWithStopObserving) {
const size_t kNumThreads = 10; const size_t kNumThreads = 10;
std::vector<std::unique_ptr<base::Thread>> threads(kNumThreads); std::vector<std::unique_ptr<base::Thread>> threads(kNumThreads);
// Start all the threads. Waiting for them to start is to hopefuly improve // Start all the threads. Waiting for them to start is to hopefully improve
// the odds of hitting interesting races once events start being added. // the odds of hitting interesting races once events start being added.
for (size_t i = 0; i < threads.size(); ++i) { for (size_t i = 0; i < threads.size(); ++i) {
threads[i] = base::MakeUnique<base::Thread>( threads[i] = base::MakeUnique<base::Thread>(
...@@ -828,13 +827,15 @@ TEST_P(FileNetLogObserverTest, AddEventsFromMultipleThreadsWithStopObserving) { ...@@ -828,13 +827,15 @@ TEST_P(FileNetLogObserverTest, AddEventsFromMultipleThreadsWithStopObserving) {
// Join all the threads. // Join all the threads.
threads.clear(); threads.clear();
ASSERT_TRUE(LogFilesExist());
} }
TEST_P(FileNetLogObserverTest, TEST_P(FileNetLogObserverTest,
AddEventsFromMultipleThreadsWithoutStopObserving) { AddEventsFromMultipleThreadsWithoutStopObserving) {
const size_t kNumThreads = 10; const size_t kNumThreads = 10;
std::vector<std::unique_ptr<base::Thread>> threads(kNumThreads); std::vector<std::unique_ptr<base::Thread>> threads(kNumThreads);
// Start all the threads. Waiting for them to start is to hopefuly improve // Start all the threads. Waiting for them to start is to hopefully improve
// the odds of hitting interesting races once events start being added. // the odds of hitting interesting races once events start being added.
for (size_t i = 0; i < threads.size(); ++i) { for (size_t i = 0; i < threads.size(); ++i) {
threads[i] = base::MakeUnique<base::Thread>( threads[i] = base::MakeUnique<base::Thread>(
...@@ -859,6 +860,9 @@ TEST_P(FileNetLogObserverTest, ...@@ -859,6 +860,9 @@ TEST_P(FileNetLogObserverTest,
// Join all the threads. // Join all the threads.
threads.clear(); threads.clear();
// The log file doesn't exist since StopObserving() was not called.
ASSERT_FALSE(LogFilesExist());
} }
} // namespace } // namespace
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment