OLD | NEW |
| (Empty) |
1 // Copyright 2015 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 "blimp/net/blimp_message_thread_pipe.h" | |
6 | |
7 #include "base/location.h" | |
8 #include "base/memory/ptr_util.h" | |
9 #include "base/memory/ref_counted.h" | |
10 #include "base/message_loop/message_loop.h" | |
11 #include "base/threading/thread.h" | |
12 #include "blimp/net/null_blimp_message_processor.h" | |
13 #include "blimp/net/test_common.h" | |
14 #include "net/base/net_errors.h" | |
15 #include "net/base/test_completion_callback.h" | |
16 #include "testing/gmock/include/gmock/gmock.h" | |
17 #include "testing/gtest/include/gtest/gtest.h" | |
18 | |
19 using testing::_; | |
20 using testing::SaveArg; | |
21 | |
22 namespace blimp { | |
23 | |
24 class BlimpMessageThreadPipeTest : public testing::Test { | |
25 public: | |
26 BlimpMessageThreadPipeTest() : thread_("PipeThread") {} | |
27 | |
28 ~BlimpMessageThreadPipeTest() override {} | |
29 | |
30 void SetUp() override { | |
31 // Start the target processor thread and initialize the pipe & proxy. | |
32 // Note that none of this will "touch" the target processor, so it's | |
33 // safe to do here, before EXPECT_CALL() expectations are set up. | |
34 ASSERT_TRUE(thread_.Start()); | |
35 pipe_ = base::MakeUnique<BlimpMessageThreadPipe>(thread_.task_runner()); | |
36 proxy_ = pipe_->CreateProxy(); | |
37 | |
38 thread_.task_runner()->PostTask( | |
39 FROM_HERE, base::Bind(&BlimpMessageThreadPipe::set_target_processor, | |
40 base::Unretained(pipe_.get()), &null_processor_)); | |
41 } | |
42 | |
43 void TearDown() override { | |
44 // If |pipe_| is still active, tear it down safely on |thread_|. | |
45 if (pipe_) | |
46 DeletePipeOnThread(); | |
47 | |
48 // Synchronize with |thread_| to ensure that any pending work is done. | |
49 SynchronizeWithThread(); | |
50 } | |
51 | |
52 MOCK_METHOD1(MockCompletionCallback, void(int)); | |
53 | |
54 void DeletePipeOnThread() { | |
55 thread_.task_runner()->DeleteSoon(FROM_HERE, pipe_.release()); | |
56 } | |
57 | |
58 void SynchronizeWithThread() { | |
59 net::TestCompletionCallback cb; | |
60 thread_.task_runner()->PostTaskAndReply(FROM_HERE, | |
61 base::Bind(&base::DoNothing), | |
62 base::Bind(cb.callback(), net::OK)); | |
63 ASSERT_EQ(net::OK, cb.WaitForResult()); | |
64 } | |
65 | |
66 protected: | |
67 base::MessageLoop message_loop_; | |
68 | |
69 NullBlimpMessageProcessor null_processor_; | |
70 | |
71 std::unique_ptr<BlimpMessageThreadPipe> pipe_; | |
72 std::unique_ptr<BlimpMessageProcessor> proxy_; | |
73 | |
74 base::Thread thread_; | |
75 }; | |
76 | |
77 TEST_F(BlimpMessageThreadPipeTest, ProcessMessage) { | |
78 EXPECT_CALL(*this, MockCompletionCallback(_)).Times(1); | |
79 | |
80 // Pass a message to the proxy for processing. | |
81 proxy_->ProcessMessage( | |
82 base::WrapUnique(new BlimpMessage), | |
83 base::Bind(&BlimpMessageThreadPipeTest::MockCompletionCallback, | |
84 base::Unretained(this))); | |
85 } | |
86 | |
87 TEST_F(BlimpMessageThreadPipeTest, DeleteProxyBeforeCompletion) { | |
88 EXPECT_CALL(*this, MockCompletionCallback(_)).Times(0); | |
89 | |
90 // Pass a message to the proxy, but then immediately delete the proxy. | |
91 proxy_->ProcessMessage( | |
92 base::WrapUnique(new BlimpMessage), | |
93 base::Bind(&BlimpMessageThreadPipeTest::MockCompletionCallback, | |
94 base::Unretained(this))); | |
95 proxy_ = nullptr; | |
96 } | |
97 | |
98 TEST_F(BlimpMessageThreadPipeTest, DeletePipeBeforeProcessMessage) { | |
99 EXPECT_CALL(*this, MockCompletionCallback(_)).Times(1); | |
100 | |
101 // Tear down the pipe (on |thread_|) between two ProcessMessage calls. | |
102 proxy_->ProcessMessage( | |
103 base::WrapUnique(new BlimpMessage), | |
104 base::Bind(&BlimpMessageThreadPipeTest::MockCompletionCallback, | |
105 base::Unretained(this))); | |
106 DeletePipeOnThread(); | |
107 proxy_->ProcessMessage( | |
108 base::WrapUnique(new BlimpMessage), | |
109 base::Bind(&BlimpMessageThreadPipeTest::MockCompletionCallback, | |
110 base::Unretained(this))); | |
111 } | |
112 | |
113 TEST_F(BlimpMessageThreadPipeTest, NullCompletionCallback) { | |
114 // Don't expect the mock to be called, but do expect not to crash. | |
115 EXPECT_CALL(*this, MockCompletionCallback(_)).Times(0); | |
116 | |
117 proxy_->ProcessMessage(base::WrapUnique(new BlimpMessage), | |
118 net::CompletionCallback()); | |
119 } | |
120 | |
121 } // namespace blimp | |
OLD | NEW |