Commit e606bdfa authored by Bailey Berro's avatar Bailey Berro Committed by Commit Bot

Create SmbTaskQueue

This change creates the SmbTaskQueue class for queueing FileSystem
operations that will be sent over D-Bus to the Smb Daemon.

BUG=chromium:757625

Change-Id: Id48b8b4abbc5841f42ae1b52dca2e142f2ba6706
Reviewed-on: https://chromium-review.googlesource.com/953134
Commit-Queue: Bailey Berro <baileyberro@chromium.org>
Reviewed-by: default avatarZentaro Kavanagh <zentaro@chromium.org>
Cr-Commit-Position: refs/heads/master@{#542899}
parent 03b14220
......@@ -1552,6 +1552,8 @@ source_set("chromeos") {
"smb_client/smb_service.h",
"smb_client/smb_service_factory.cc",
"smb_client/smb_service_factory.h",
"smb_client/smb_task_queue.cc",
"smb_client/smb_task_queue.h",
"smb_client/temp_file_manager.cc",
"smb_client/temp_file_manager.h",
"status/network_menu.cc",
......@@ -2042,6 +2044,7 @@ source_set("unit_tests") {
"smb_client/discovery/in_memory_host_locator_unittest.cc",
"smb_client/discovery/network_scanner_unittest.cc",
"smb_client/smb_service_unittest.cc",
"smb_client/smb_task_queue_unittest.cc",
"smb_client/temp_file_manager_unittest.cc",
"system/automatic_reboot_manager_unittest.cc",
"system/device_disabling_manager_unittest.cc",
......
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/chromeos/smb_client/smb_task_queue.h"
#include "base/bind.h"
#include "base/callback.h"
namespace chromeos {
namespace smb_client {
SmbTaskQueue::SmbTaskQueue(size_t max_pending) : max_pending_(max_pending) {}
SmbTaskQueue::~SmbTaskQueue() {}
void SmbTaskQueue::AddTask(SmbTask task) {
operations_.push(std::move(task));
RunTaskIfNeccessary();
}
void SmbTaskQueue::TaskFinished() {
DCHECK_GT(num_pending_, 0u);
--num_pending_;
RunTaskIfNeccessary();
}
void SmbTaskQueue::RunTaskIfNeccessary() {
if (IsCapacityToRunTask() && IsTaskToRun()) {
RunNextTask();
// Sanity check that either the maximum number of tasks are running or
// nothing is in the queue. If there is anything left in the queue to run,
// then the maximum number of tasks should already be running.
DCHECK(!IsCapacityToRunTask() || !IsTaskToRun());
}
}
SmbTask SmbTaskQueue::GetNextTask() {
DCHECK(IsTaskToRun());
SmbTask next_task = std::move(operations_.front());
operations_.pop();
return next_task;
}
void SmbTaskQueue::RunNextTask() {
DCHECK(IsTaskToRun());
++num_pending_;
GetNextTask().Run();
}
bool SmbTaskQueue::IsTaskToRun() const {
return !operations_.empty();
}
bool SmbTaskQueue::IsCapacityToRunTask() const {
return num_pending_ < max_pending_;
}
} // namespace smb_client
} // namespace chromeos
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_CHROMEOS_SMB_CLIENT_SMB_TASK_QUEUE_H_
#define CHROME_BROWSER_CHROMEOS_SMB_CLIENT_SMB_TASK_QUEUE_H_
#include "base/callback.h"
#include "base/containers/queue.h"
#include "chromeos/dbus/smb_provider_client.h"
namespace chromeos {
namespace smb_client {
// An SmbTask is a call to SmbProviderClient with a bound SmbFileSystem callback
// that runs when SmbProviderClient receives a D-Bus message response.
using SmbTask = base::OnceClosure;
// SmbTaskQueue handles the queuing of SmbTasks. Tasks are 'pending' while
// SmbProviderClient awaits a D-Bus Message Response. Tasks are added to
// the queue via SmbTaskQueue::AddTask. Upon the SmbFileSystem callback in the
// task running, the caller must call SmbTaskQueue::TaskFinished to allow the
// next task to run.
//
// Example:
//
// void CreateDirectory(FilePath directory_path, bool recursive,
// StatusCallback callback) {
// auto reply = base::BindOnce(&SmbFileSystem::HandleStatusCallback,
// AsWeakPtr(), callback);
//
// SmbTask task = base::BindOnce(&SmbProviderClient::CreateDirectory,
// base::Unretained(GetSmbProviderClient()),
// GetMountId(),
// directory_path,
// recursive,
// std::move(reply));
// tq_.AddTask(std::move(task));
// }
//
// void HandleStatusCallback(StatusCallback callback, ErrorType error) {
// tq_.TaskFinished();
// callback.Run(error);
// }
class SmbTaskQueue {
public:
// Adds |task| to the task queue. If fewer that max_pending_ tasks are
// outstanding, |task| will run immediately. Otherwise, it will be added to
// operations_ and run in FIFO order.
void AddTask(SmbTask task);
// Must be called by the owner of this class to indicate that a response to a
// task was received.
void TaskFinished();
explicit SmbTaskQueue(size_t max_pending);
~SmbTaskQueue();
private:
// This runs the next task in operations_ if there is capacity to run an
// additional task, and a task remaing to run.
void RunTaskIfNeccessary();
// Helper method that returns the next task to run.
SmbTask GetNextTask();
// Helper method that runs the next task.
void RunNextTask();
// Helper method that returns whether there are tasks in operations_ to run.
bool IsTaskToRun() const;
// Helper method that returns whether there are fewer than max_pending tasks
// outstanding.
bool IsCapacityToRunTask() const;
const size_t max_pending_;
size_t num_pending_ = 0;
base::queue<SmbTask> operations_;
DISALLOW_COPY_AND_ASSIGN(SmbTaskQueue);
};
} // namespace smb_client
} // namespace chromeos
#endif // CHROME_BROWSER_CHROMEOS_SMB_CLIENT_SMB_TASK_QUEUE_H_
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/chromeos/smb_client/smb_task_queue.h"
#include <map>
#include <string>
#include <vector>
#include "base/bind.h"
#include "base/callback.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace chromeos {
namespace smb_client {
namespace {
constexpr size_t kTaskQueueCapacity = 3;
}
// SmbTaskQueue is used to test SmbTaskQueue. Tasks are added to the task queue
// with specified |task_id|'s. When a task is run by the task_queue_, it is
// added to the pending_ map and can be completed by invoking the
// CompleteTask method.
class SmbTaskQueueTest : public testing::Test {
public:
SmbTaskQueueTest() : task_queue_(kTaskQueueCapacity) {}
~SmbTaskQueueTest() override = default;
protected:
// Creates and adds the task with |task_id| to task_queue_.
void CreateAndAddTask(uint32_t task_id) {
base::OnceClosure reply =
base::BindOnce(&SmbTaskQueueTest::OnReply, base::Unretained(this));
SmbTask task =
base::BindOnce(&SmbTaskQueueTest::Start, base::Unretained(this),
task_id, std::move(reply));
task_queue_.AddTask(std::move(task));
}
// Checks whether the task |task_id| is pending.
bool IsPending(uint32_t task_id) const { return pending_.count(task_id); }
// Completes the pending task |task_id|, running its reply.
void CompleteTask(uint32_t task_id) {
DCHECK(IsPending(task_id));
SmbTask to_run = std::move(pending_[task_id]);
pending_.erase(task_id);
std::move(to_run).Run();
}
// Returns the number of pending tasks.
size_t PendingCount() const { return pending_.size(); }
private:
void OnReply() { task_queue_.TaskFinished(); }
void Start(uint32_t task_id, base::OnceClosure reply) {
pending_[task_id] = std::move(reply);
}
SmbTaskQueue task_queue_;
std::map<uint32_t, base::OnceClosure> pending_;
DISALLOW_COPY_AND_ASSIGN(SmbTaskQueueTest);
};
// SmbTaskQueue immediately runs a task when less than max_pending are running.
TEST_F(SmbTaskQueueTest, TaskQueueRunsASingleTask) {
const uint32_t task_id = 1;
CreateAndAddTask(task_id);
EXPECT_TRUE(IsPending(task_id));
CompleteTask(task_id);
EXPECT_FALSE(IsPending(task_id));
}
// SmbTaskQueue runs atleast max_pending_ tasks concurrently.
TEST_F(SmbTaskQueueTest, TaskQueueRunsMultipleTasks) {
const uint32_t task_id_1 = 1;
const uint32_t task_id_2 = 2;
const uint32_t task_id_3 = 3;
CreateAndAddTask(task_id_1);
CreateAndAddTask(task_id_2);
CreateAndAddTask(task_id_3);
EXPECT_EQ(kTaskQueueCapacity, PendingCount());
EXPECT_TRUE(IsPending(task_id_1));
EXPECT_TRUE(IsPending(task_id_2));
EXPECT_TRUE(IsPending(task_id_3));
CompleteTask(task_id_1);
CompleteTask(task_id_2);
CompleteTask(task_id_3);
EXPECT_FALSE(IsPending(task_id_1));
EXPECT_FALSE(IsPending(task_id_2));
EXPECT_FALSE(IsPending(task_id_3));
}
// SmbTaskQueue runs at most max_pending_ tasks concurrently.
TEST_F(SmbTaskQueueTest, TaskQueueDoesNotRunAdditionalTestsWhenFull) {
const uint32_t task_id_1 = 1;
const uint32_t task_id_2 = 2;
const uint32_t task_id_3 = 3;
const uint32_t task_id_4 = 4;
CreateAndAddTask(task_id_1);
CreateAndAddTask(task_id_2);
CreateAndAddTask(task_id_3);
CreateAndAddTask(task_id_4);
// The first three tasks should run.
EXPECT_EQ(kTaskQueueCapacity, PendingCount());
EXPECT_TRUE(IsPending(task_id_1));
EXPECT_TRUE(IsPending(task_id_2));
EXPECT_TRUE(IsPending(task_id_3));
// The fourth task should wait until another task finishes to run.
EXPECT_FALSE(IsPending(task_id_4));
CompleteTask(task_id_1);
EXPECT_FALSE(IsPending(task_id_1));
// After completing a task, the fourth task should be able to run.
EXPECT_TRUE(IsPending(task_id_4));
}
} // namespace smb_client
} // namespace chromeos
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