aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEric Caruso <ejcaruso@chromium.org>2018-04-09 17:48:19 -0700
committerchrome-bot <chrome-bot@chromium.org>2018-04-11 20:28:19 -0700
commitc452c4cea981f4741cc28368caf94acf89768255 (patch)
treeabfee3f05c4ea49b03a10a9141d699d944b7f163
parent69df7cbdda2e7878f6e217670741bf54857baab1 (diff)
downloadplatform_external_libbrillo-c452c4cea981f4741cc28368caf94acf89768255.tar.gz
platform_external_libbrillo-c452c4cea981f4741cc28368caf94acf89768255.tar.bz2
platform_external_libbrillo-c452c4cea981f4741cc28368caf94acf89768255.zip
libbrillo: use bound arguments rather than capture-by-ref
In future libchrome we will have the ability to bind lambdas but it's considered unacceptable to bind lambdas that carry an environment. To prepare for this, explicitly state the environment as bound bare pointers to local variables. BUG=b:37434548 TEST=unit tests Change-Id: Id6cd318077de9b5ad253731a107d2cb418323de6 Reviewed-on: https://chromium-review.googlesource.com/1003892 Commit-Ready: Eric Caruso <ejcaruso@chromium.org> Tested-by: Eric Caruso <ejcaruso@chromium.org> Reviewed-by: Eric Caruso <ejcaruso@chromium.org>
-rw-r--r--brillo/dbus/dbus_signal_handler.h16
-rw-r--r--brillo/http/http_connection_fake.cc8
-rw-r--r--brillo/http/http_request_unittest.cc15
-rw-r--r--brillo/http/http_transport_curl_unittest.cc11
-rw-r--r--brillo/http/http_transport_fake.cc9
-rw-r--r--brillo/http/http_utils_unittest.cc21
-rw-r--r--brillo/message_loops/fake_message_loop_unittest.cc20
-rw-r--r--brillo/message_loops/glib_message_loop_unittest.cc10
-rw-r--r--brillo/message_loops/message_loop_unittest.cc113
-rw-r--r--brillo/message_loops/message_loop_utils.cc3
-rw-r--r--brillo/process_reaper_unittest.cc37
-rw-r--r--brillo/streams/fake_stream_unittest.cc51
-rw-r--r--brillo/streams/file_stream_unittest.cc62
-rw-r--r--brillo/streams/stream_unittest.cc99
14 files changed, 264 insertions, 211 deletions
diff --git a/brillo/dbus/dbus_signal_handler.h b/brillo/dbus/dbus_signal_handler.h
index 46d7212..5bf1d1c 100644
--- a/brillo/dbus/dbus_signal_handler.h
+++ b/brillo/dbus/dbus_signal_handler.h
@@ -5,6 +5,7 @@
#ifndef LIBBRILLO_BRILLO_DBUS_DBUS_SIGNAL_HANDLER_H_
#define LIBBRILLO_BRILLO_DBUS_DBUS_SIGNAL_HANDLER_H_
+#include <functional>
#include <string>
#include <brillo/bind_lambda.h>
@@ -49,17 +50,18 @@ void ConnectToSignal(
// from |signal| message buffer and redirects the call to
// |signal_callback_wrapper| which, in turn, would call the user-provided
// |signal_callback|.
- auto dbus_signal_callback = [signal_callback_wrapper](dbus::Signal* signal) {
+ auto dbus_signal_callback = [](std::function<void(const Args&...)> callback,
+ dbus::Signal* signal) {
dbus::MessageReader reader(signal);
- DBusParamReader<false, Args...>::Invoke(
- signal_callback_wrapper, &reader, nullptr);
+ DBusParamReader<false, Args...>::Invoke(callback, &reader, nullptr);
};
// Register our stub handler with D-Bus ObjectProxy.
- object_proxy->ConnectToSignal(interface_name,
- signal_name,
- base::Bind(dbus_signal_callback),
- on_connected_callback);
+ object_proxy->ConnectToSignal(
+ interface_name,
+ signal_name,
+ base::Bind(dbus_signal_callback, signal_callback_wrapper),
+ on_connected_callback);
}
} // namespace dbus_utils
diff --git a/brillo/http/http_connection_fake.cc b/brillo/http/http_connection_fake.cc
index 36506fe..0e14c7d 100644
--- a/brillo/http/http_connection_fake.cc
+++ b/brillo/http/http_connection_fake.cc
@@ -63,10 +63,14 @@ RequestID Connection::FinishRequestAsync(
// Make sure the produced Closure holds a reference to the instance of this
// connection.
auto connection = std::static_pointer_cast<Connection>(shared_from_this());
- auto callback = [connection, success_callback, error_callback] {
+ auto callback = [](std::shared_ptr<Connection> connection,
+ const SuccessCallback& success_callback,
+ const ErrorCallback& error_callback) {
connection->FinishRequestAsyncHelper(success_callback, error_callback);
};
- transport_->RunCallbackAsync(FROM_HERE, base::Bind(callback));
+ transport_->RunCallbackAsync(
+ FROM_HERE,
+ base::Bind(callback, connection, success_callback, error_callback));
return 1;
}
diff --git a/brillo/http/http_request_unittest.cc b/brillo/http/http_request_unittest.cc
index 962abbc..39ccc18 100644
--- a/brillo/http/http_request_unittest.cc
+++ b/brillo/http/http_request_unittest.cc
@@ -156,23 +156,24 @@ TEST_F(HttpRequestTest, GetResponse) {
return true;
};
- auto success_callback =
- [this, &resp_data](RequestID request_id, std::unique_ptr<Response> resp) {
+ auto success_callback = base::Bind(
+ [](MockConnection* connection, const std::string& resp_data,
+ RequestID request_id, std::unique_ptr<Response> resp) {
EXPECT_EQ(23, request_id);
- EXPECT_CALL(*connection_, GetResponseStatusCode())
+ EXPECT_CALL(*connection, GetResponseStatusCode())
.WillOnce(Return(status_code::Partial));
EXPECT_EQ(status_code::Partial, resp->GetStatusCode());
- EXPECT_CALL(*connection_, GetResponseStatusText())
+ EXPECT_CALL(*connection, GetResponseStatusText())
.WillOnce(Return("Partial completion"));
EXPECT_EQ("Partial completion", resp->GetStatusText());
- EXPECT_CALL(*connection_, GetResponseHeader(response_header::kContentType))
+ EXPECT_CALL(*connection, GetResponseHeader(response_header::kContentType))
.WillOnce(Return(mime::text::kHtml));
EXPECT_EQ(mime::text::kHtml, resp->GetContentType());
EXPECT_EQ(resp_data, resp->ExtractDataAsString());
- };
+ }, connection_.get(), resp_data);
auto finish_request_async =
[this, &read_data](const SuccessCallback& success_callback) {
@@ -195,7 +196,7 @@ TEST_F(HttpRequestTest, GetResponse) {
EXPECT_CALL(*connection_, FinishRequestAsync(_, _))
.WillOnce(DoAll(WithArg<0>(Invoke(finish_request_async)), Return(23)));
- EXPECT_EQ(23, request.GetResponse(base::Bind(success_callback), {}));
+ EXPECT_EQ(23, request.GetResponse(success_callback, {}));
}
} // namespace http
diff --git a/brillo/http/http_transport_curl_unittest.cc b/brillo/http/http_transport_curl_unittest.cc
index b72017a..c05c81a 100644
--- a/brillo/http/http_transport_curl_unittest.cc
+++ b/brillo/http/http_transport_curl_unittest.cc
@@ -235,12 +235,13 @@ TEST_F(HttpCurlTransportAsyncTest, StartAsyncTransfer) {
// Success/error callback needed to report the result of an async operation.
int success_call_count = 0;
- auto success_callback = [&success_call_count, &run_loop](
+ auto success_callback = base::Bind([](
+ int* success_call_count, const base::Closure& quit_closure,
RequestID /* request_id */, std::unique_ptr<http::Response> /* resp */) {
base::MessageLoop::current()->task_runner()->PostTask(
- FROM_HERE, run_loop.QuitClosure());
- success_call_count++;
- };
+ FROM_HERE, quit_closure);
+ (*success_call_count)++;
+ }, &success_call_count, run_loop.QuitClosure());
auto error_callback = [](RequestID /* request_id */,
const Error* /* error */) {
@@ -265,7 +266,7 @@ TEST_F(HttpCurlTransportAsyncTest, StartAsyncTransfer) {
.WillOnce(Return(CURLM_OK));
EXPECT_EQ(1, transport_->StartAsyncTransfer(connection.get(),
- base::Bind(success_callback),
+ success_callback,
base::Bind(error_callback)));
EXPECT_EQ(0, success_call_count);
diff --git a/brillo/http/http_transport_fake.cc b/brillo/http/http_transport_fake.cc
index b4cdce1..ad74173 100644
--- a/brillo/http/http_transport_fake.cc
+++ b/brillo/http/http_transport_fake.cc
@@ -121,11 +121,14 @@ void Transport::AddSimpleReplyHandler(const std::string& url,
int status_code,
const std::string& reply_text,
const std::string& mime_type) {
- auto handler = [status_code, reply_text, mime_type](
- const ServerRequest& /* request */, ServerResponse* response) {
+ auto handler = [](
+ int status_code, const std::string& reply_text,
+ const std::string& mime_type, const ServerRequest& /* request */,
+ ServerResponse* response) {
response->ReplyText(status_code, reply_text, mime_type);
};
- AddHandler(url, method, base::Bind(handler));
+ AddHandler(url, method,
+ base::Bind(handler, status_code, reply_text, mime_type));
}
Transport::HandlerCallback Transport::GetHandler(
diff --git a/brillo/http/http_utils_unittest.cc b/brillo/http/http_utils_unittest.cc
index aa54cb0..1497c3d 100644
--- a/brillo/http/http_utils_unittest.cc
+++ b/brillo/http/http_utils_unittest.cc
@@ -69,13 +69,14 @@ TEST(HttpUtils, SendRequestAsync_BinaryData) {
// Test binary data round-tripping.
std::vector<uint8_t> custom_data{0xFF, 0x00, 0x80, 0x40, 0xC0, 0x7F};
auto success_callback =
- [&custom_data](RequestID /* id */,
- std::unique_ptr<http::Response> response) {
+ base::Bind([](const std::vector<uint8_t>& custom_data,
+ RequestID /* id */,
+ std::unique_ptr<http::Response> response) {
EXPECT_TRUE(response->IsSuccessful());
EXPECT_EQ(brillo::mime::application::kOctet_stream,
response->GetContentType());
EXPECT_EQ(custom_data, response->ExtractData());
- };
+ }, custom_data);
auto error_callback = [](RequestID /* id */, const Error* /* error */) {
FAIL() << "This callback shouldn't have been called";
};
@@ -86,7 +87,7 @@ TEST(HttpUtils, SendRequestAsync_BinaryData) {
brillo::mime::application::kOctet_stream,
{},
transport,
- base::Bind(success_callback),
+ success_callback,
base::Bind(error_callback));
}
@@ -300,20 +301,22 @@ TEST(HttpUtils, PostBinary) {
TEST(HttpUtils, PostText) {
std::string fake_data = "Some data";
- auto PostHandler = [fake_data](const fake::ServerRequest& request,
- fake::ServerResponse* response) {
+ auto post_handler = base::Bind([](const std::string& data,
+ const fake::ServerRequest& request,
+ fake::ServerResponse* response) {
EXPECT_EQ(request_type::kPost, request.GetMethod());
- EXPECT_EQ(fake_data.size(),
+ EXPECT_EQ(data.size(),
std::stoul(request.GetHeader(request_header::kContentLength)));
EXPECT_EQ(brillo::mime::text::kPlain,
request.GetHeader(request_header::kContentType));
response->ReplyText(status_code::Ok,
request.GetDataAsString(),
brillo::mime::text::kPlain);
- };
+ }, fake_data);
std::shared_ptr<fake::Transport> transport(new fake::Transport);
- transport->AddHandler(kFakeUrl, request_type::kPost, base::Bind(PostHandler));
+ transport->AddHandler(
+ kFakeUrl, request_type::kPost, base::Bind(post_handler));
auto response = http::PostTextAndBlock(kFakeUrl,
fake_data,
diff --git a/brillo/message_loops/fake_message_loop_unittest.cc b/brillo/message_loops/fake_message_loop_unittest.cc
index 10b551e..f73b65a 100644
--- a/brillo/message_loops/fake_message_loop_unittest.cc
+++ b/brillo/message_loops/fake_message_loop_unittest.cc
@@ -45,13 +45,17 @@ TEST_F(FakeMessageLoopTest, CancelTaskInvalidValuesTest) {
TEST_F(FakeMessageLoopTest, PostDelayedTaskRunsInOrder) {
vector<int> order;
- loop_->PostDelayedTask(Bind([&order]() { order.push_back(1); }),
+ loop_->PostDelayedTask(Bind([](vector<int>* order) { order->push_back(1); },
+ &order),
TimeDelta::FromSeconds(1));
- loop_->PostDelayedTask(Bind([&order]() { order.push_back(4); }),
+ loop_->PostDelayedTask(Bind([](vector<int>* order) { order->push_back(4); },
+ &order),
TimeDelta::FromSeconds(4));
- loop_->PostDelayedTask(Bind([&order]() { order.push_back(3); }),
+ loop_->PostDelayedTask(Bind([](vector<int>* order) { order->push_back(3); },
+ &order),
TimeDelta::FromSeconds(3));
- loop_->PostDelayedTask(Bind([&order]() { order.push_back(2); }),
+ loop_->PostDelayedTask(Bind([](vector<int>* order) { order->push_back(2); },
+ &order),
TimeDelta::FromSeconds(2));
// Run until all the tasks are run.
loop_->Run();
@@ -89,14 +93,16 @@ TEST_F(FakeMessageLoopTest, WatchFileDescriptorWaits) {
int called = 0;
TaskId task_id = loop_->WatchFileDescriptor(
FROM_HERE, fd, MessageLoop::kWatchRead, false,
- Bind([&called] { called++; }));
+ Bind([](int* called) { (*called)++; }, &called));
EXPECT_NE(MessageLoop::kTaskIdNull, task_id);
EXPECT_NE(MessageLoop::kTaskIdNull,
- loop_->PostDelayedTask(Bind([this] { this->loop_->BreakLoop(); }),
+ loop_->PostDelayedTask(Bind(&FakeMessageLoop::BreakLoop,
+ base::Unretained(loop_.get())),
TimeDelta::FromSeconds(10)));
EXPECT_NE(MessageLoop::kTaskIdNull,
- loop_->PostDelayedTask(Bind([this] { this->loop_->BreakLoop(); }),
+ loop_->PostDelayedTask(Bind(&FakeMessageLoop::BreakLoop,
+ base::Unretained(loop_.get())),
TimeDelta::FromSeconds(20)));
loop_->Run();
EXPECT_EQ(0, called);
diff --git a/brillo/message_loops/glib_message_loop_unittest.cc b/brillo/message_loops/glib_message_loop_unittest.cc
index 4b72a11..e9206b0 100644
--- a/brillo/message_loops/glib_message_loop_unittest.cc
+++ b/brillo/message_loops/glib_message_loop_unittest.cc
@@ -42,7 +42,7 @@ TEST_F(GlibMessageLoopTest, WatchFileDescriptorTriggersWhenEmpty) {
int called = 0;
TaskId task_id = loop_->WatchFileDescriptor(
FROM_HERE, fd, MessageLoop::kWatchRead, true,
- Bind([&called] { called++; }));
+ Bind([](int* called) { (*called)++; }, &called));
EXPECT_NE(MessageLoop::kTaskIdNull, task_id);
EXPECT_NE(0, MessageLoopRunMaxIterations(loop_.get(), 10));
EXPECT_LT(2, called);
@@ -55,11 +55,11 @@ TEST_F(GlibMessageLoopTest, WatchFileDescriptorTriggersWhenInvalid) {
int called = 0;
TaskId task_id = loop_->WatchFileDescriptor(
FROM_HERE, fd, MessageLoop::kWatchRead, true,
- Bind([&called, fd] {
- if (!called)
+ Bind([] (int* called, int fd) {
+ if (!*called)
IGNORE_EINTR(close(fd));
- called++;
- }));
+ (*called)++;
+ }, &called, fd));
EXPECT_NE(MessageLoop::kTaskIdNull, task_id);
EXPECT_NE(0, MessageLoopRunMaxIterations(loop_.get(), 10));
EXPECT_LT(2, called);
diff --git a/brillo/message_loops/message_loop_unittest.cc b/brillo/message_loops/message_loop_unittest.cc
index 0265fbf..cf548ab 100644
--- a/brillo/message_loops/message_loop_unittest.cc
+++ b/brillo/message_loops/message_loop_unittest.cc
@@ -26,6 +26,23 @@
using base::Bind;
using base::TimeDelta;
+namespace {
+
+// Convenience functions for passing to base::Bind.
+void SetToTrue(bool* b) {
+ *b = true;
+}
+
+bool ReturnBool(bool *b) {
+ return *b;
+}
+
+void Increment(int* i) {
+ (*i)++;
+}
+
+} // namespace
+
namespace brillo {
using TaskId = MessageLoop::TaskId;
@@ -74,8 +91,7 @@ TYPED_TEST(MessageLoopTest, CancelTaskInvalidValuesTest) {
TYPED_TEST(MessageLoopTest, PostTaskTest) {
bool called = false;
- TaskId task_id = this->loop_->PostTask(FROM_HERE,
- Bind([&called]() { called = true; }));
+ TaskId task_id = this->loop_->PostTask(FROM_HERE, Bind(&SetToTrue, &called));
EXPECT_NE(MessageLoop::kTaskIdNull, task_id);
MessageLoopRunMaxIterations(this->loop_.get(), 100);
EXPECT_TRUE(called);
@@ -84,8 +100,7 @@ TYPED_TEST(MessageLoopTest, PostTaskTest) {
// Tests that we can cancel tasks right after we schedule them.
TYPED_TEST(MessageLoopTest, PostTaskCancelledTest) {
bool called = false;
- TaskId task_id = this->loop_->PostTask(FROM_HERE,
- Bind([&called]() { called = true; }));
+ TaskId task_id = this->loop_->PostTask(FROM_HERE, Bind(&SetToTrue, &called));
EXPECT_TRUE(this->loop_->CancelTask(task_id));
MessageLoopRunMaxIterations(this->loop_.get(), 100);
EXPECT_FALSE(called);
@@ -96,13 +111,11 @@ TYPED_TEST(MessageLoopTest, PostTaskCancelledTest) {
TYPED_TEST(MessageLoopTest, PostDelayedTaskRunsEventuallyTest) {
bool called = false;
TaskId task_id = this->loop_->PostDelayedTask(
- FROM_HERE,
- Bind([&called]() { called = true; }),
- TimeDelta::FromMilliseconds(50));
+ FROM_HERE, Bind(&SetToTrue, &called), TimeDelta::FromMilliseconds(50));
EXPECT_NE(MessageLoop::kTaskIdNull, task_id);
MessageLoopRunUntil(this->loop_.get(),
TimeDelta::FromSeconds(10),
- Bind([&called]() { return called; }));
+ Bind(&ReturnBool, &called));
// Check that the main loop finished before the 10 seconds timeout, so it
// finished due to the callback being called and not due to the timeout.
EXPECT_TRUE(called);
@@ -120,10 +133,10 @@ TYPED_TEST(MessageLoopTest, WatchForInvalidFD) {
bool called = false;
EXPECT_EQ(MessageLoop::kTaskIdNull, this->loop_->WatchFileDescriptor(
FROM_HERE, -1, MessageLoop::kWatchRead, true,
- Bind([&called] { called = true; })));
+ Bind(&SetToTrue, &called)));
EXPECT_EQ(MessageLoop::kTaskIdNull, this->loop_->WatchFileDescriptor(
FROM_HERE, -1, MessageLoop::kWatchWrite, true,
- Bind([&called] { called = true; })));
+ Bind(&SetToTrue, &called)));
EXPECT_EQ(0, MessageLoopRunMaxIterations(this->loop_.get(), 100));
EXPECT_FALSE(called);
}
@@ -133,7 +146,7 @@ TYPED_TEST(MessageLoopTest, CancelWatchedFileDescriptor) {
bool called = false;
TaskId task_id = this->loop_->WatchFileDescriptor(
FROM_HERE, pipe.reader, MessageLoop::kWatchRead, true,
- Bind([&called] { called = true; }));
+ Bind(&SetToTrue, &called));
EXPECT_NE(MessageLoop::kTaskIdNull, task_id);
// The reader end is blocked because we didn't write anything to the writer
// end.
@@ -152,7 +165,7 @@ TYPED_TEST(MessageLoopTest, WatchFileDescriptorTriggersWhenPipeClosed) {
pipe.writer = -1;
TaskId task_id = this->loop_->WatchFileDescriptor(
FROM_HERE, pipe.reader, MessageLoop::kWatchRead, true,
- Bind([&called] { called = true; }));
+ Bind(&SetToTrue, &called));
EXPECT_NE(MessageLoop::kTaskIdNull, task_id);
// The reader end is not blocked because we closed the writer end so a read on
// the reader end would return 0 bytes read.
@@ -170,7 +183,7 @@ TYPED_TEST(MessageLoopTest, WatchFileDescriptorPersistently) {
int called = 0;
TaskId task_id = this->loop_->WatchFileDescriptor(
FROM_HERE, pipe.reader, MessageLoop::kWatchRead, true,
- Bind([&called] { called++; }));
+ Bind(&Increment, &called));
EXPECT_NE(MessageLoop::kTaskIdNull, task_id);
// We let the main loop run for 20 iterations to give it enough iterations to
// verify that our callback was called more than one. We only check that our
@@ -187,7 +200,7 @@ TYPED_TEST(MessageLoopTest, WatchFileDescriptorNonPersistent) {
int called = 0;
TaskId task_id = this->loop_->WatchFileDescriptor(
FROM_HERE, pipe.reader, MessageLoop::kWatchRead, false,
- Bind([&called] { called++; }));
+ Bind(&Increment, &called));
EXPECT_NE(MessageLoop::kTaskIdNull, task_id);
// We let the main loop run for 20 iterations but we just expect it to run
// at least once. The callback should be called exactly once since we
@@ -206,17 +219,17 @@ TYPED_TEST(MessageLoopTest, WatchFileDescriptorForReadAndWriteSimultaneously) {
TaskId read_task_id = this->loop_->WatchFileDescriptor(
FROM_HERE, socks.left, MessageLoop::kWatchRead, true,
- Bind([this, &read_task_id] {
- EXPECT_TRUE(this->loop_->CancelTask(read_task_id))
- << "task_id" << read_task_id;
- }));
+ Bind([] (MessageLoop* loop, TaskId* read_task_id) {
+ EXPECT_TRUE(loop->CancelTask(*read_task_id))
+ << "task_id" << *read_task_id;
+ }, this->loop_.get(), &read_task_id));
EXPECT_NE(MessageLoop::kTaskIdNull, read_task_id);
TaskId write_task_id = this->loop_->WatchFileDescriptor(
FROM_HERE, socks.left, MessageLoop::kWatchWrite, true,
- Bind([this, &write_task_id] {
- EXPECT_TRUE(this->loop_->CancelTask(write_task_id));
- }));
+ Bind([] (MessageLoop* loop, TaskId* write_task_id) {
+ EXPECT_TRUE(loop->CancelTask(*write_task_id));
+ }, this->loop_.get(), &write_task_id));
EXPECT_NE(MessageLoop::kTaskIdNull, write_task_id);
EXPECT_LT(0, MessageLoopRunMaxIterations(this->loop_.get(), 20));
@@ -228,13 +241,12 @@ TYPED_TEST(MessageLoopTest, WatchFileDescriptorForReadAndWriteSimultaneously) {
// Test that we can cancel the task we are running, and should just fail.
TYPED_TEST(MessageLoopTest, DeleteTaskFromSelf) {
bool cancel_result = true; // We would expect this to be false.
- MessageLoop* loop_ptr = this->loop_.get();
TaskId task_id;
task_id = this->loop_->PostTask(
FROM_HERE,
- Bind([&cancel_result, loop_ptr, &task_id]() {
- cancel_result = loop_ptr->CancelTask(task_id);
- }));
+ Bind([](bool* cancel_result, MessageLoop* loop, TaskId* task_id) {
+ *cancel_result = loop->CancelTask(*task_id);
+ }, &cancel_result, this->loop_.get(), &task_id));
EXPECT_EQ(1, MessageLoopRunMaxIterations(this->loop_.get(), 100));
EXPECT_FALSE(cancel_result);
}
@@ -245,10 +257,10 @@ TYPED_TEST(MessageLoopTest, DeleteNonPersistenIOTaskFromSelf) {
ScopedPipe pipe;
TaskId task_id = this->loop_->WatchFileDescriptor(
FROM_HERE, pipe.writer, MessageLoop::kWatchWrite, false /* persistent */,
- Bind([this, &task_id] {
- EXPECT_FALSE(this->loop_->CancelTask(task_id));
- task_id = MessageLoop::kTaskIdNull;
- }));
+ Bind([](MessageLoop* loop, TaskId* task_id) {
+ EXPECT_FALSE(loop->CancelTask(*task_id));
+ *task_id = MessageLoop::kTaskIdNull;
+ }, this->loop_.get(), &task_id));
EXPECT_NE(MessageLoop::kTaskIdNull, task_id);
EXPECT_EQ(1, MessageLoopRunMaxIterations(this->loop_.get(), 100));
EXPECT_EQ(MessageLoop::kTaskIdNull, task_id);
@@ -260,10 +272,10 @@ TYPED_TEST(MessageLoopTest, DeletePersistenIOTaskFromSelf) {
ScopedPipe pipe;
TaskId task_id = this->loop_->WatchFileDescriptor(
FROM_HERE, pipe.writer, MessageLoop::kWatchWrite, true /* persistent */,
- Bind([this, &task_id] {
- EXPECT_TRUE(this->loop_->CancelTask(task_id));
- task_id = MessageLoop::kTaskIdNull;
- }));
+ Bind([](MessageLoop* loop, TaskId* task_id) {
+ EXPECT_TRUE(loop->CancelTask(*task_id));
+ *task_id = MessageLoop::kTaskIdNull;
+ }, this->loop_.get(), &task_id));
EXPECT_NE(MessageLoop::kTaskIdNull, task_id);
EXPECT_EQ(1, MessageLoopRunMaxIterations(this->loop_.get(), 100));
EXPECT_EQ(MessageLoop::kTaskIdNull, task_id);
@@ -282,14 +294,14 @@ TYPED_TEST(MessageLoopTest, DeleteAllPersistenIOTaskFromSelf) {
task_ids[i] = this->loop_->WatchFileDescriptor(
FROM_HERE, pipes[i].writer, MessageLoop::kWatchWrite,
true /* persistent */,
- Bind([this, &task_ids] {
+ Bind([] (MessageLoop* loop, TaskId* task_ids) {
for (int j = 0; j < kNumTasks; ++j) {
// Once we cancel all the tasks, none should run, so this code runs
// only once from one callback.
- EXPECT_TRUE(this->loop_->CancelTask(task_ids[j]));
+ EXPECT_TRUE(loop->CancelTask(task_ids[j]));
task_ids[j] = MessageLoop::kTaskIdNull;
}
- }));
+ }, this->loop_.get(), task_ids));
}
MessageLoopRunMaxIterations(this->loop_.get(), 100);
for (int i = 0; i < kNumTasks; ++i) {
@@ -310,13 +322,16 @@ TYPED_TEST(MessageLoopTest, AllTasksAreEqual) {
base::Closure timeout_callback;
MessageLoop::TaskId timeout_task;
timeout_callback = base::Bind(
- [this, &timeout_called, &total_calls, &timeout_callback, &timeout_task] {
- timeout_called++;
- total_calls++;
- timeout_task = this->loop_->PostTask(FROM_HERE, Bind(timeout_callback));
- if (total_calls > 100)
- this->loop_->BreakLoop();
- });
+ [](MessageLoop* loop, int* timeout_called, int* total_calls,
+ base::Closure* timeout_callback, MessageLoop::TaskId* timeout_task) {
+ (*timeout_called)++;
+ (*total_calls)++;
+ *timeout_task = loop->PostTask(FROM_HERE, Bind(*timeout_callback));
+ if (*total_calls > 100)
+ loop->BreakLoop();
+ },
+ this->loop_.get(), &timeout_called, &total_calls, &timeout_callback,
+ &timeout_task);
timeout_task = this->loop_->PostTask(FROM_HERE, timeout_callback);
// Second, schedule several file descriptor watchers.
@@ -325,14 +340,16 @@ TYPED_TEST(MessageLoopTest, AllTasksAreEqual) {
MessageLoop::TaskId tasks[kNumTasks];
int reads[kNumTasks] = {};
- auto fd_callback = [this, &pipes, &reads, &total_calls](int i) {
+ base::Callback<void(int)> fd_callback = base::Bind(
+ [](MessageLoop* loop, ScopedPipe* pipes, int* reads,
+ int* total_calls, int i) {
reads[i]++;
- total_calls++;
+ (*total_calls)++;
char c;
EXPECT_EQ(1, HANDLE_EINTR(read(pipes[i].reader, &c, 1)));
- if (total_calls > 100)
- this->loop_->BreakLoop();
- };
+ if (*total_calls > 100)
+ loop->BreakLoop();
+ }, this->loop_.get(), pipes, reads, &total_calls);
for (int i = 0; i < kNumTasks; ++i) {
tasks[i] = this->loop_->WatchFileDescriptor(
diff --git a/brillo/message_loops/message_loop_utils.cc b/brillo/message_loops/message_loop_utils.cc
index 7931447..d58711b 100644
--- a/brillo/message_loops/message_loop_utils.cc
+++ b/brillo/message_loops/message_loop_utils.cc
@@ -16,7 +16,8 @@ void MessageLoopRunUntil(
bool timeout_called = false;
MessageLoop::TaskId task_id = loop->PostDelayedTask(
FROM_HERE,
- base::Bind([&timeout_called]() { timeout_called = true; }),
+ base::Bind([](bool* timeout_called) { *timeout_called = true; },
+ &timeout_called),
timeout);
while (!timeout_called && (terminate.is_null() || !terminate.Run()))
loop->RunOnce(true);
diff --git a/brillo/process_reaper_unittest.cc b/brillo/process_reaper_unittest.cc
index 11db54d..98498f7 100644
--- a/brillo/process_reaper_unittest.cc
+++ b/brillo/process_reaper_unittest.cc
@@ -75,56 +75,57 @@ TEST_F(ProcessReaperTest, UnregisterAndReregister) {
TEST_F(ProcessReaperTest, ReapExitedChild) {
pid_t pid = ForkChildAndExit(123);
EXPECT_TRUE(process_reaper_.WatchForChild(FROM_HERE, pid, base::Bind(
- [this](const siginfo_t& info) {
+ [](MessageLoop* loop, const siginfo_t& info) {
EXPECT_EQ(CLD_EXITED, info.si_code);
EXPECT_EQ(123, info.si_status);
- this->brillo_loop_.BreakLoop();
- })));
+ loop->BreakLoop();
+ }, &brillo_loop_)));
brillo_loop_.Run();
}
// Test that simultaneous child processes fire their respective callbacks when
// exiting.
-TEST_F(ProcessReaperTest, ReapedChildsMatchCallbacks) {
- int running_childs = 10;
- for (int i = 0; i < running_childs; ++i) {
+TEST_F(ProcessReaperTest, ReapedChildrenMatchCallbacks) {
+ int running_children = 10;
+ for (int i = 0; i < running_children; ++i) {
// Different processes will have different exit values.
int exit_value = 1 + i;
pid_t pid = ForkChildAndExit(exit_value);
EXPECT_TRUE(process_reaper_.WatchForChild(FROM_HERE, pid, base::Bind(
- [this, exit_value, &running_childs](const siginfo_t& info) {
+ [](MessageLoop* loop, int exit_value, int* running_children,
+ const siginfo_t& info) {
EXPECT_EQ(CLD_EXITED, info.si_code);
EXPECT_EQ(exit_value, info.si_status);
- running_childs--;
- if (running_childs == 0)
- this->brillo_loop_.BreakLoop();
- })));
+ (*running_children)--;
+ if (*running_children == 0)
+ loop->BreakLoop();
+ }, &brillo_loop_, exit_value, &running_children)));
}
// This sleep is optional. It helps to have more processes exit before we
// start watching for them in the message loop.
usleep(10 * 1000);
brillo_loop_.Run();
- EXPECT_EQ(0, running_childs);
+ EXPECT_EQ(0, running_children);
}
TEST_F(ProcessReaperTest, ReapKilledChild) {
pid_t pid = ForkChildAndKill(SIGKILL);
EXPECT_TRUE(process_reaper_.WatchForChild(FROM_HERE, pid, base::Bind(
- [this](const siginfo_t& info) {
+ [](MessageLoop* loop, const siginfo_t& info) {
EXPECT_EQ(CLD_KILLED, info.si_code);
EXPECT_EQ(SIGKILL, info.si_status);
- this->brillo_loop_.BreakLoop();
- })));
+ loop->BreakLoop();
+ }, &brillo_loop_)));
brillo_loop_.Run();
}
TEST_F(ProcessReaperTest, ReapKilledAndForgottenChild) {
pid_t pid = ForkChildAndExit(0);
EXPECT_TRUE(process_reaper_.WatchForChild(FROM_HERE, pid, base::Bind(
- [this](const siginfo_t& /* info */) {
+ [](MessageLoop* loop, const siginfo_t& /* info */) {
ADD_FAILURE() << "Child process was still tracked.";
- this->brillo_loop_.BreakLoop();
- })));
+ loop->BreakLoop();
+ }, &brillo_loop_)));
EXPECT_TRUE(process_reaper_.ForgetChild(pid));
// A second call should return failure.
diff --git a/brillo/streams/fake_stream_unittest.cc b/brillo/streams/fake_stream_unittest.cc
index 5a66704..b198b17 100644
--- a/brillo/streams/fake_stream_unittest.cc
+++ b/brillo/streams/fake_stream_unittest.cc
@@ -236,19 +236,19 @@ TEST_F(FakeStreamTest, WaitForDataRead) {
EXPECT_CALL(mock_loop_, PostDelayedTask(_, _, zero_delay)).Times(2);
int call_count = 0;
- auto callback = [&call_count](Stream::AccessMode mode) {
- call_count++;
+ auto callback = base::Bind([](int* call_count, Stream::AccessMode mode) {
+ (*call_count)++;
EXPECT_EQ(Stream::AccessMode::READ, mode);
- };
+ }, &call_count);
EXPECT_TRUE(stream_->WaitForData(Stream::AccessMode::READ,
- base::Bind(callback), nullptr));
+ callback, nullptr));
mock_loop_.Run();
EXPECT_EQ(1, call_count);
stream_->AddReadPacketString({}, "foobar");
EXPECT_TRUE(stream_->WaitForData(Stream::AccessMode::READ,
- base::Bind(callback), nullptr));
+ callback, nullptr));
mock_loop_.Run();
EXPECT_EQ(2, call_count);
@@ -258,7 +258,7 @@ TEST_F(FakeStreamTest, WaitForDataRead) {
stream_->AddReadPacketString(one_sec_delay, "baz");
EXPECT_CALL(mock_loop_, PostDelayedTask(_, _, one_sec_delay)).Times(1);
EXPECT_TRUE(stream_->WaitForData(Stream::AccessMode::READ,
- base::Bind(callback), nullptr));
+ callback, nullptr));
mock_loop_.Run();
EXPECT_EQ(3, call_count);
}
@@ -283,12 +283,14 @@ TEST_F(FakeStreamTest, ReadAsync) {
int success_count = 0;
int error_count = 0;
- auto on_success = [&success_count] { success_count++; };
- auto on_failure = [&error_count](const Error* /* error */) { error_count++; };
+ auto on_success = [](int* success_count) { (*success_count)++; };
+ auto on_failure = [](int* error_count, const Error* /* error */) {
+ (*error_count)++;
+ };
EXPECT_TRUE(stream_->ReadAllAsync(buffer.data(), buffer.size(),
- base::Bind(on_success),
- base::Bind(on_failure),
+ base::Bind(on_success, &success_count),
+ base::Bind(on_failure, &error_count),
nullptr));
mock_loop_.Run();
EXPECT_EQ(1, success_count);
@@ -391,19 +393,19 @@ TEST_F(FakeStreamTest, WaitForDataWrite) {
EXPECT_CALL(mock_loop_, PostDelayedTask(_, _, zero_delay)).Times(2);
int call_count = 0;
- auto callback = [&call_count](Stream::AccessMode mode) {
- call_count++;
+ auto callback = base::Bind([](int* call_count, Stream::AccessMode mode) {
+ (*call_count)++;
EXPECT_EQ(Stream::AccessMode::WRITE, mode);
- };
+ }, &call_count);
EXPECT_TRUE(stream_->WaitForData(Stream::AccessMode::WRITE,
- base::Bind(callback), nullptr));
+ callback, nullptr));
mock_loop_.Run();
EXPECT_EQ(1, call_count);
stream_->ExpectWritePacketString({}, "foobar");
EXPECT_TRUE(stream_->WaitForData(Stream::AccessMode::WRITE,
- base::Bind(callback), nullptr));
+ callback, nullptr));
mock_loop_.Run();
EXPECT_EQ(2, call_count);
@@ -413,7 +415,7 @@ TEST_F(FakeStreamTest, WaitForDataWrite) {
stream_->ExpectWritePacketString(one_sec_delay, "baz");
EXPECT_CALL(mock_loop_, PostDelayedTask(_, _, one_sec_delay)).Times(1);
EXPECT_TRUE(stream_->WaitForData(Stream::AccessMode::WRITE,
- base::Bind(callback), nullptr));
+ callback, nullptr));
mock_loop_.Run();
EXPECT_EQ(3, call_count);
}
@@ -436,12 +438,14 @@ TEST_F(FakeStreamTest, WriteAsync) {
int success_count = 0;
int error_count = 0;
- auto on_success = [&success_count] { success_count++; };
- auto on_failure = [&error_count](const Error* /* error */) { error_count++; };
+ auto on_success = [](int* success_count) { (*success_count)++; };
+ auto on_failure = [](int* error_count, const Error* /* error */) {
+ (*error_count)++;
+ };
EXPECT_TRUE(stream_->WriteAllAsync(output_data.data(), output_data.size(),
- base::Bind(on_success),
- base::Bind(on_failure),
+ base::Bind(on_success, &success_count),
+ base::Bind(on_failure, &error_count),
nullptr));
mock_loop_.Run();
EXPECT_EQ(1, success_count);
@@ -455,11 +459,12 @@ TEST_F(FakeStreamTest, WaitForDataReadWrite) {
auto two_sec_delay = base::TimeDelta::FromSeconds(2);
int call_count = 0;
- auto callback = [&call_count](Stream::AccessMode mode,
+ auto callback = base::Bind([](int* call_count,
+ Stream::AccessMode mode,
Stream::AccessMode expected_mode) {
- call_count++;
+ (*call_count)++;
EXPECT_EQ(static_cast<int>(expected_mode), static_cast<int>(mode));
- };
+ }, &call_count);
stream_->AddReadPacketString(one_sec_delay, "foo");
stream_->ExpectWritePacketString(two_sec_delay, "bar");
diff --git a/brillo/streams/file_stream_unittest.cc b/brillo/streams/file_stream_unittest.cc
index ebbe551..5f79dc9 100644
--- a/brillo/streams/file_stream_unittest.cc
+++ b/brillo/streams/file_stream_unittest.cc
@@ -116,6 +116,15 @@ void TestCreateFile(Stream* stream) {
EXPECT_EQ(-1, ReadByte(stream));
}
+// Helper functions for base::Bind.
+void SetSizeT(size_t* target, size_t source) {
+ *target = source;
+}
+
+void SetToTrue(bool* target, const Error* /* error */) {
+ *target = true;
+}
+
} // anonymous namespace
// A mock file descriptor wrapper to test low-level file API used by FileStream.
@@ -371,8 +380,6 @@ TEST_F(FileStreamTest, Seek_Fail) {
TEST_F(FileStreamTest, ReadAsync) {
size_t read_size = 0;
bool failed = false;
- auto success_callback = [&read_size](size_t size) { read_size = size; };
- auto error_callback = [&failed](const Error* /* error */) { failed = true; };
FileStream::FileDescriptorInterface::DataCallback data_callback;
EXPECT_CALL(fd_mock(), Read(test_read_buffer_, 100))
@@ -380,8 +387,8 @@ TEST_F(FileStreamTest, ReadAsync) {
EXPECT_CALL(fd_mock(), WaitForData(Stream::AccessMode::READ, _, _))
.WillOnce(DoAll(SaveArg<1>(&data_callback), Return(true)));
EXPECT_TRUE(stream_->ReadAsync(test_read_buffer_, 100,
- base::Bind(success_callback),
- base::Bind(error_callback),
+ base::Bind(&SetSizeT, &read_size),
+ base::Bind(&SetToTrue, &failed),
nullptr));
EXPECT_EQ(0u, read_size);
EXPECT_FALSE(failed);
@@ -510,8 +517,6 @@ TEST_F(FileStreamTest, ReadAllBlocking_Fail) {
TEST_F(FileStreamTest, WriteAsync) {
size_t write_size = 0;
bool failed = false;
- auto success_callback = [&write_size](size_t size) { write_size = size; };
- auto error_callback = [&failed](const Error* /* error */) { failed = true; };
FileStream::FileDescriptorInterface::DataCallback data_callback;
EXPECT_CALL(fd_mock(), Write(test_write_buffer_, 100))
@@ -519,8 +524,8 @@ TEST_F(FileStreamTest, WriteAsync) {
EXPECT_CALL(fd_mock(), WaitForData(Stream::AccessMode::WRITE, _, _))
.WillOnce(DoAll(SaveArg<1>(&data_callback), Return(true)));
EXPECT_TRUE(stream_->WriteAsync(test_write_buffer_, 100,
- base::Bind(success_callback),
- base::Bind(error_callback),
+ base::Bind(&SetSizeT, &write_size),
+ base::Bind(&SetToTrue, &failed),
nullptr));
EXPECT_EQ(0u, write_size);
EXPECT_FALSE(failed);
@@ -1033,14 +1038,10 @@ TEST_F(FileStreamTest, FromFileDescriptor_ReadAsync) {
BaseMessageLoop brillo_loop{&base_loop};
brillo_loop.SetAsCurrent();
- auto success_callback = [&succeeded, &buffer](size_t size) {
+ auto success_callback = [](bool* succeeded, char* buffer, size_t size) {
std::string data{buffer, buffer + size};
ASSERT_EQ("abracadabra", data);
- succeeded = true;
- };
-
- auto error_callback = [&failed](const Error* /* error */) {
- failed = true;
+ *succeeded = true;
};
auto write_data_callback = [](int write_fd) {
@@ -1058,13 +1059,16 @@ TEST_F(FileStreamTest, FromFileDescriptor_ReadAsync) {
base::Bind(write_data_callback, fds[1]),
base::TimeDelta::FromMilliseconds(10));
- EXPECT_TRUE(stream->ReadAsync(buffer, 100, base::Bind(success_callback),
- base::Bind(error_callback), nullptr));
+ EXPECT_TRUE(stream->ReadAsync(
+ buffer, 100, base::Bind(success_callback, &succeeded, buffer),
+ base::Bind(&SetToTrue, &failed), nullptr));
- auto end_condition = [&failed, &succeeded] { return failed || succeeded; };
+ auto end_condition = [](bool* failed, bool* succeeded) {
+ return *failed || *succeeded;
+ };
MessageLoopRunUntil(&brillo_loop,
base::TimeDelta::FromSeconds(1),
- base::Bind(end_condition));
+ base::Bind(end_condition, &failed, &succeeded));
EXPECT_TRUE(succeeded);
EXPECT_FALSE(failed);
@@ -1084,27 +1088,27 @@ TEST_F(FileStreamTest, FromFileDescriptor_WriteAsync) {
ASSERT_EQ(0, pipe(fds));
- auto success_callback = [&succeeded, &data](int read_fd, size_t /* size */) {
+ auto success_callback = [](bool* succeeded, const std::string& data,
+ int read_fd, size_t /* size */) {
char buffer[100];
EXPECT_TRUE(base::ReadFromFD(read_fd, buffer, data.size()));
EXPECT_EQ(data, (std::string{buffer, buffer + data.size()}));
- succeeded = true;
- };
-
- auto error_callback = [&failed](const Error* /* error */) {
- failed = true;
+ *succeeded = true;
};
StreamPtr stream = FileStream::FromFileDescriptor(fds[1], true, nullptr);
- EXPECT_TRUE(stream->WriteAsync(data.data(), data.size(),
- base::Bind(success_callback, fds[0]),
- base::Bind(error_callback), nullptr));
+ EXPECT_TRUE(stream->WriteAsync(
+ data.data(), data.size(),
+ base::Bind(success_callback, &succeeded, data, fds[0]),
+ base::Bind(&SetToTrue, &failed), nullptr));
- auto end_condition = [&failed, &succeeded] { return failed || succeeded; };
+ auto end_condition = [](bool* failed, bool* succeeded) {
+ return *failed || *succeeded;
+ };
MessageLoopRunUntil(&brillo_loop,
base::TimeDelta::FromSeconds(1),
- base::Bind(end_condition));
+ base::Bind(end_condition, &failed, &succeeded));
EXPECT_TRUE(succeeded);
EXPECT_FALSE(failed);
diff --git a/brillo/streams/stream_unittest.cc b/brillo/streams/stream_unittest.cc
index 927b1d8..c341cde 100644
--- a/brillo/streams/stream_unittest.cc
+++ b/brillo/streams/stream_unittest.cc
@@ -21,6 +21,15 @@ using testing::SaveArg;
using testing::SetArgPointee;
using testing::_;
+namespace {
+
+// Helper function for base::Bind.
+void SetToTrue(bool* target, const brillo::Error* /* error */) {
+ *target = true;
+}
+
+} // namespace
+
namespace brillo {
using AccessMode = Stream::AccessMode;
@@ -100,11 +109,12 @@ TEST(Stream, ReadAsync) {
size_t read_size = 0;
bool succeeded = false;
bool failed = false;
- auto success_callback = [&read_size, &succeeded](size_t size) {
- read_size = size;
- succeeded = true;
- };
- auto error_callback = [&failed](const Error* /* error */) { failed = true; };
+ auto success_callback = base::Bind(
+ [](size_t* read_size, bool* succeeded, size_t size) {
+ *read_size = size;
+ *succeeded = true;
+ }, &read_size, &succeeded);
+ auto error_callback = base::Bind(&SetToTrue, &failed);
MockStreamImpl stream_mock;
base::Callback<void(AccessMode)> data_callback;
@@ -117,9 +127,8 @@ TEST(Stream, ReadAsync) {
DoAll(SetArgPointee<2>(0), SetArgPointee<3>(false), Return(true)));
EXPECT_CALL(stream_mock, WaitForData(AccessMode::READ, _, _))
.WillOnce(DoAll(SaveArg<1>(&data_callback), Return(true)));
- EXPECT_TRUE(stream_mock.ReadAsync(buf, sizeof(buf),
- base::Bind(success_callback),
- base::Bind(error_callback), nullptr));
+ EXPECT_TRUE(stream_mock.ReadAsync(buf, sizeof(buf), success_callback,
+ error_callback, nullptr));
EXPECT_EQ(0u, read_size);
EXPECT_FALSE(succeeded);
EXPECT_FALSE(failed);
@@ -127,9 +136,8 @@ TEST(Stream, ReadAsync) {
// Since the previous call is waiting for the data to be available, we can't
// schedule another read.
ErrorPtr error;
- EXPECT_FALSE(stream_mock.ReadAsync(buf, sizeof(buf),
- base::Bind(success_callback),
- base::Bind(error_callback), &error));
+ EXPECT_FALSE(stream_mock.ReadAsync(buf, sizeof(buf), success_callback,
+ error_callback, &error));
EXPECT_EQ(errors::stream::kDomain, error->GetDomain());
EXPECT_EQ(errors::stream::kOperationNotSupported, error->GetCode());
EXPECT_EQ("Another asynchronous operation is still pending",
@@ -149,8 +157,10 @@ TEST(Stream, ReadAsync) {
TEST(Stream, ReadAsync_DontWaitForData) {
bool succeeded = false;
bool failed = false;
- auto success_callback = [&succeeded](size_t /* size */) { succeeded = true; };
- auto error_callback = [&failed](const Error* /* error */) { failed = true; };
+ auto success_callback = base::Bind([](bool* succeeded, size_t /* size */) {
+ *succeeded = true;
+ }, &succeeded);
+ auto error_callback = base::Bind(&SetToTrue, &failed);
MockStreamImpl stream_mock;
char buf[10];
@@ -161,9 +171,8 @@ TEST(Stream, ReadAsync_DontWaitForData) {
.WillOnce(
DoAll(SetArgPointee<2>(5), SetArgPointee<3>(false), Return(true)));
EXPECT_CALL(stream_mock, WaitForData(_, _, _)).Times(0);
- EXPECT_TRUE(stream_mock.ReadAsync(buf, sizeof(buf),
- base::Bind(success_callback),
- base::Bind(error_callback), nullptr));
+ EXPECT_TRUE(stream_mock.ReadAsync(buf, sizeof(buf), success_callback,
+ error_callback, nullptr));
// Even if ReadNonBlocking() returned some data without waiting, the
// |success_callback| should not run yet.
EXPECT_TRUE(fake_loop_.PendingTasks());
@@ -173,9 +182,8 @@ TEST(Stream, ReadAsync_DontWaitForData) {
// Since the previous callback is still waiting in the main loop, we can't
// schedule another read yet.
ErrorPtr error;
- EXPECT_FALSE(stream_mock.ReadAsync(buf, sizeof(buf),
- base::Bind(success_callback),
- base::Bind(error_callback), &error));
+ EXPECT_FALSE(stream_mock.ReadAsync(buf, sizeof(buf), success_callback,
+ error_callback, &error));
EXPECT_EQ(errors::stream::kDomain, error->GetDomain());
EXPECT_EQ(errors::stream::kOperationNotSupported, error->GetCode());
EXPECT_EQ("Another asynchronous operation is still pending",
@@ -189,8 +197,9 @@ TEST(Stream, ReadAsync_DontWaitForData) {
TEST(Stream, ReadAllAsync) {
bool succeeded = false;
bool failed = false;
- auto success_callback = [&succeeded]() { succeeded = true; };
- auto error_callback = [&failed](const Error* /* error */) { failed = true; };
+ auto success_callback = base::Bind([](bool* succeeded) { *succeeded = true; },
+ &succeeded);
+ auto error_callback = base::Bind(&SetToTrue, &failed);
MockStreamImpl stream_mock;
base::Callback<void(AccessMode)> data_callback;
@@ -203,10 +212,8 @@ TEST(Stream, ReadAllAsync) {
DoAll(SetArgPointee<2>(0), SetArgPointee<3>(false), Return(true)));
EXPECT_CALL(stream_mock, WaitForData(AccessMode::READ, _, _))
.WillOnce(DoAll(SaveArg<1>(&data_callback), Return(true)));
- EXPECT_TRUE(stream_mock.ReadAllAsync(buf, sizeof(buf),
- base::Bind(success_callback),
- base::Bind(error_callback),
- nullptr));
+ EXPECT_TRUE(stream_mock.ReadAllAsync(buf, sizeof(buf), success_callback,
+ error_callback, nullptr));
EXPECT_FALSE(succeeded);
EXPECT_FALSE(failed);
testing::Mock::VerifyAndClearExpectations(&stream_mock);
@@ -239,12 +246,13 @@ TEST(Stream, ReadAllAsync) {
TEST(Stream, ReadAllAsync_EOS) {
bool succeeded = false;
bool failed = false;
- auto success_callback = [&succeeded]() { succeeded = true; };
- auto error_callback = [&failed](const Error* error) {
+ auto success_callback = base::Bind([](bool* succeeded) { *succeeded = true; },
+ &succeeded);
+ auto error_callback = base::Bind([](bool* failed, const Error* error) {
ASSERT_EQ(errors::stream::kDomain, error->GetDomain());
ASSERT_EQ(errors::stream::kPartialData, error->GetCode());
- failed = true;
- };
+ *failed = true;
+ }, &failed);
MockStreamImpl stream_mock;
base::Callback<void(AccessMode)> data_callback;
@@ -255,10 +263,8 @@ TEST(Stream, ReadAllAsync_EOS) {
DoAll(SetArgPointee<2>(0), SetArgPointee<3>(false), Return(true)));
EXPECT_CALL(stream_mock, WaitForData(AccessMode::READ, _, _))
.WillOnce(DoAll(SaveArg<1>(&data_callback), Return(true)));
- EXPECT_TRUE(stream_mock.ReadAllAsync(buf, sizeof(buf),
- base::Bind(success_callback),
- base::Bind(error_callback),
- nullptr));
+ EXPECT_TRUE(stream_mock.ReadAllAsync(buf, sizeof(buf), success_callback,
+ error_callback, nullptr));
// ReadAsyncAll() should finish and fail once ReadNonBlocking() returns an
// end-of-stream condition.
@@ -351,8 +357,10 @@ TEST(Stream, ReadAllBlocking) {
TEST(Stream, WriteAsync) {
size_t write_size = 0;
bool failed = false;
- auto success_callback = [&write_size](size_t size) { write_size = size; };
- auto error_callback = [&failed](const Error* /* error */) { failed = true; };
+ auto success_callback = base::Bind([](size_t* write_size, size_t size) {
+ *write_size = size;
+ }, &write_size);
+ auto error_callback = base::Bind(&SetToTrue, &failed);
MockStreamImpl stream_mock;
InSequence s;
@@ -365,16 +373,14 @@ TEST(Stream, WriteAsync) {
.WillOnce(DoAll(SetArgPointee<2>(0), Return(true)));
EXPECT_CALL(stream_mock, WaitForData(AccessMode::WRITE, _, _))
.WillOnce(DoAll(SaveArg<1>(&data_callback), Return(true)));
- EXPECT_TRUE(stream_mock.WriteAsync(buf, sizeof(buf),
- base::Bind(success_callback),
- base::Bind(error_callback), nullptr));
+ EXPECT_TRUE(stream_mock.WriteAsync(buf, sizeof(buf), success_callback,
+ error_callback, nullptr));
EXPECT_EQ(0u, write_size);
EXPECT_FALSE(failed);
ErrorPtr error;
- EXPECT_FALSE(stream_mock.WriteAsync(buf, sizeof(buf),
- base::Bind(success_callback),
- base::Bind(error_callback), &error));
+ EXPECT_FALSE(stream_mock.WriteAsync(buf, sizeof(buf), success_callback,
+ error_callback, &error));
EXPECT_EQ(errors::stream::kDomain, error->GetDomain());
EXPECT_EQ(errors::stream::kOperationNotSupported, error->GetCode());
EXPECT_EQ("Another asynchronous operation is still pending",
@@ -390,8 +396,9 @@ TEST(Stream, WriteAsync) {
TEST(Stream, WriteAllAsync) {
bool succeeded = false;
bool failed = false;
- auto success_callback = [&succeeded]() { succeeded = true; };
- auto error_callback = [&failed](const Error* /* error */) { failed = true; };
+ auto success_callback = base::Bind([](bool* succeeded) { *succeeded = true; },
+ &succeeded);
+ auto error_callback = base::Bind(&SetToTrue, &failed);
MockStreamImpl stream_mock;
base::Callback<void(AccessMode)> data_callback;
@@ -401,10 +408,8 @@ TEST(Stream, WriteAllAsync) {
.WillOnce(DoAll(SetArgPointee<2>(0), Return(true)));
EXPECT_CALL(stream_mock, WaitForData(AccessMode::WRITE, _, _))
.WillOnce(DoAll(SaveArg<1>(&data_callback), Return(true)));
- EXPECT_TRUE(stream_mock.WriteAllAsync(buf, sizeof(buf),
- base::Bind(success_callback),
- base::Bind(error_callback),
- nullptr));
+ EXPECT_TRUE(stream_mock.WriteAllAsync(buf, sizeof(buf), success_callback,
+ error_callback, nullptr));
testing::Mock::VerifyAndClearExpectations(&stream_mock);
EXPECT_FALSE(succeeded);
EXPECT_FALSE(failed);