Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1359)

Unified Diff: mojo/edk/system/raw_channel_unittest.cc

Issue 859333004: Allow mojo::system::RawChannel::Delegate methods to destroy the RawChannel. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: mojo/edk/system/raw_channel_unittest.cc
diff --git a/mojo/edk/system/raw_channel_unittest.cc b/mojo/edk/system/raw_channel_unittest.cc
index a8cbcba7d7b5d3d4cb93c110bd716d55c272d321..a95ce3022d02c66ac8562947ae877e4215216102 100644
--- a/mojo/edk/system/raw_channel_unittest.cc
+++ b/mojo/edk/system/raw_channel_unittest.cc
@@ -555,12 +555,14 @@ TEST_F(RawChannelTest, WriteMessageAfterShutdown) {
EXPECT_FALSE(rc->WriteMessage(MakeTestMessage(1)));
}
-// RawChannelTest.ShutdownOnReadMessage ----------------------------------------
+// RawChannelTest.{Shutdown, ShutdownAndDestroy}OnReadMessage ------------------
class ShutdownOnReadMessageRawChannelDelegate : public RawChannel::Delegate {
public:
- explicit ShutdownOnReadMessageRawChannelDelegate(RawChannel* raw_channel)
+ explicit ShutdownOnReadMessageRawChannelDelegate(RawChannel* raw_channel,
+ bool should_destroy)
: raw_channel_(raw_channel),
+ should_destroy_(should_destroy),
done_event_(false, false),
did_shutdown_(false) {}
~ShutdownOnReadMessageRawChannelDelegate() override {}
@@ -574,6 +576,8 @@ class ShutdownOnReadMessageRawChannelDelegate : public RawChannel::Delegate {
EXPECT_TRUE(
CheckMessageData(message_view.bytes(), message_view.num_bytes()));
raw_channel_->Shutdown();
+ if (should_destroy_)
+ delete raw_channel_;
did_shutdown_ = true;
done_event_.Signal();
}
@@ -589,6 +593,7 @@ class ShutdownOnReadMessageRawChannelDelegate : public RawChannel::Delegate {
private:
RawChannel* const raw_channel_;
+ const bool should_destroy_;
base::WaitableEvent done_event_;
bool did_shutdown_;
@@ -601,7 +606,7 @@ TEST_F(RawChannelTest, ShutdownOnReadMessage) {
EXPECT_TRUE(WriteTestMessageToHandle(handles[1].get(), 10));
scoped_ptr<RawChannel> rc(RawChannel::Create(handles[0].Pass()));
- ShutdownOnReadMessageRawChannelDelegate delegate(rc.get());
+ ShutdownOnReadMessageRawChannelDelegate delegate(rc.get(), false);
io_thread()->PostTaskAndWait(
FROM_HERE,
base::Bind(&InitOnIOThread, rc.get(), base::Unretained(&delegate)));
@@ -610,13 +615,29 @@ TEST_F(RawChannelTest, ShutdownOnReadMessage) {
delegate.Wait();
}
-// RawChannelTest.ShutdownOnError{Read, Write} ---------------------------------
+TEST_F(RawChannelTest, ShutdownAndDestroyOnReadMessage) {
yzshen1 2015/01/21 23:03:42 An alternative is to use value parameterized tests
viettrungluu 2015/01/21 23:34:32 I don't think it's worth the extra complication (t
+ // Write a message into the other end.
+ EXPECT_TRUE(WriteTestMessageToHandle(handles[1].get(), 10));
+
+ // The delegate will destroy |rc|.
+ RawChannel* rc = RawChannel::Create(handles[0].Pass()).release();
+ ShutdownOnReadMessageRawChannelDelegate delegate(rc, true);
+ io_thread()->PostTaskAndWait(
+ FROM_HERE, base::Bind(&InitOnIOThread, rc, base::Unretained(&delegate)));
+
+ // Wait for the delegate, which will shut the |RawChannel| down.
+ delegate.Wait();
+}
+
+// RawChannelTest.{Shutdown, ShutdownAndDestroy}OnError{Read, Write} -----------
class ShutdownOnErrorRawChannelDelegate : public RawChannel::Delegate {
public:
ShutdownOnErrorRawChannelDelegate(RawChannel* raw_channel,
+ bool should_destroy,
Error shutdown_on_error_type)
: raw_channel_(raw_channel),
+ should_destroy_(should_destroy),
shutdown_on_error_type_(shutdown_on_error_type),
done_event_(false, false),
did_shutdown_(false) {}
@@ -633,6 +654,8 @@ class ShutdownOnErrorRawChannelDelegate : public RawChannel::Delegate {
if (error != shutdown_on_error_type_)
return;
raw_channel_->Shutdown();
+ if (should_destroy_)
+ delete raw_channel_;
did_shutdown_ = true;
done_event_.Signal();
}
@@ -645,6 +668,7 @@ class ShutdownOnErrorRawChannelDelegate : public RawChannel::Delegate {
private:
RawChannel* const raw_channel_;
+ const bool should_destroy_;
const Error shutdown_on_error_type_;
base::WaitableEvent done_event_;
bool did_shutdown_;
@@ -655,7 +679,7 @@ class ShutdownOnErrorRawChannelDelegate : public RawChannel::Delegate {
TEST_F(RawChannelTest, ShutdownOnErrorRead) {
scoped_ptr<RawChannel> rc(RawChannel::Create(handles[0].Pass()));
ShutdownOnErrorRawChannelDelegate delegate(
- rc.get(), RawChannel::Delegate::ERROR_READ_SHUTDOWN);
+ rc.get(), false, RawChannel::Delegate::ERROR_READ_SHUTDOWN);
io_thread()->PostTaskAndWait(
FROM_HERE,
base::Bind(&InitOnIOThread, rc.get(), base::Unretained(&delegate)));
@@ -667,9 +691,23 @@ TEST_F(RawChannelTest, ShutdownOnErrorRead) {
delegate.Wait();
}
+TEST_F(RawChannelTest, ShutdownAndDestroyOnErrorRead) {
+ RawChannel* rc = RawChannel::Create(handles[0].Pass()).release();
+ ShutdownOnErrorRawChannelDelegate delegate(
+ rc, true, RawChannel::Delegate::ERROR_READ_SHUTDOWN);
+ io_thread()->PostTaskAndWait(
+ FROM_HERE, base::Bind(&InitOnIOThread, rc, base::Unretained(&delegate)));
+
+ // Close the handle of the other end, which should stuff fail.
+ handles[1].reset();
+
+ // Wait for the delegate, which will shut the |RawChannel| down.
+ delegate.Wait();
+}
+
TEST_F(RawChannelTest, ShutdownOnErrorWrite) {
scoped_ptr<RawChannel> rc(RawChannel::Create(handles[0].Pass()));
- ShutdownOnErrorRawChannelDelegate delegate(rc.get(),
+ ShutdownOnErrorRawChannelDelegate delegate(rc.get(), false,
RawChannel::Delegate::ERROR_WRITE);
io_thread()->PostTaskAndWait(
FROM_HERE,
@@ -684,6 +722,22 @@ TEST_F(RawChannelTest, ShutdownOnErrorWrite) {
delegate.Wait();
}
+TEST_F(RawChannelTest, ShutdownAndDestroyOnErrorWrite) {
+ RawChannel* rc = RawChannel::Create(handles[0].Pass()).release();
+ ShutdownOnErrorRawChannelDelegate delegate(rc, true,
+ RawChannel::Delegate::ERROR_WRITE);
+ io_thread()->PostTaskAndWait(
+ FROM_HERE, base::Bind(&InitOnIOThread, rc, base::Unretained(&delegate)));
+
+ // Close the handle of the other end, which should stuff fail.
+ handles[1].reset();
+
+ EXPECT_FALSE(rc->WriteMessage(MakeTestMessage(1)));
+
+ // Wait for the delegate, which will shut the |RawChannel| down.
+ delegate.Wait();
+}
+
// RawChannelTest.ReadWritePlatformHandles -------------------------------------
class ReadPlatformHandlesCheckerRawChannelDelegate

Powered by Google App Engine
This is Rietveld 408576698