summaryrefslogtreecommitdiffstats
path: root/blimp/net/blimp_message_thread_pipe_unittest.cc
blob: dd267da7b4ec21b8da7e340d801fcb14b0c107be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright 2015 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 "blimp/net/blimp_message_thread_pipe.h"

#include "base/location.h"
#include "base/memory/ref_counted.h"
#include "base/message_loop/message_loop.h"
#include "base/threading/thread.h"
#include "blimp/net/null_blimp_message_processor.h"
#include "blimp/net/test_common.h"
#include "net/base/net_errors.h"
#include "net/base/test_completion_callback.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using testing::_;
using testing::SaveArg;

namespace blimp {

class BlimpMessageThreadPipeTest : public testing::Test {
 public:
  BlimpMessageThreadPipeTest() : thread_("PipeThread") {}

  ~BlimpMessageThreadPipeTest() override {}

  void SetUp() override {
    // Start the target processor thread and initialize the pipe & proxy.
    // Note that none of this will "touch" the target processor, so it's
    // safe to do here, before EXPECT_CALL() expectations are set up.
    ASSERT_TRUE(thread_.Start());
    pipe_ = make_scoped_ptr(new BlimpMessageThreadPipe(thread_.task_runner()));
    proxy_ = pipe_->CreateProxy();

    thread_.task_runner()->PostTask(
        FROM_HERE, base::Bind(&BlimpMessageThreadPipe::set_target_processor,
                              base::Unretained(pipe_.get()), &null_processor_));
  }

  void TearDown() override {
    // If |pipe_| is still active, tear it down safely on |thread_|.
    if (pipe_)
      DeletePipeOnThread();

    // Synchronize with |thread_| to ensure that any pending work is done.
    SynchronizeWithThread();
  }

  MOCK_METHOD1(MockCompletionCallback, void(int));

  void DeletePipeOnThread() {
    thread_.task_runner()->DeleteSoon(FROM_HERE, pipe_.release());
  }

  void SynchronizeWithThread() {
    net::TestCompletionCallback cb;
    thread_.task_runner()->PostTaskAndReply(FROM_HERE,
                                            base::Bind(&base::DoNothing),
                                            base::Bind(cb.callback(), net::OK));
    ASSERT_EQ(net::OK, cb.WaitForResult());
  }

 protected:
  base::MessageLoop message_loop_;

  NullBlimpMessageProcessor null_processor_;

  scoped_ptr<BlimpMessageThreadPipe> pipe_;
  scoped_ptr<BlimpMessageProcessor> proxy_;

  base::Thread thread_;
};

TEST_F(BlimpMessageThreadPipeTest, ProcessMessage) {
  EXPECT_CALL(*this, MockCompletionCallback(_)).Times(1);

  // Pass a message to the proxy for processing.
  proxy_->ProcessMessage(
      make_scoped_ptr(new BlimpMessage),
      base::Bind(&BlimpMessageThreadPipeTest::MockCompletionCallback,
                 base::Unretained(this)));
}

TEST_F(BlimpMessageThreadPipeTest, DeleteProxyBeforeCompletion) {
  EXPECT_CALL(*this, MockCompletionCallback(_)).Times(0);

  // Pass a message to the proxy, but then immediately delete the proxy.
  proxy_->ProcessMessage(
      make_scoped_ptr(new BlimpMessage),
      base::Bind(&BlimpMessageThreadPipeTest::MockCompletionCallback,
                 base::Unretained(this)));
  proxy_ = nullptr;
}

TEST_F(BlimpMessageThreadPipeTest, DeletePipeBeforeProcessMessage) {
  EXPECT_CALL(*this, MockCompletionCallback(_)).Times(1);

  // Tear down the pipe (on |thread_|) between two ProcessMessage calls.
  proxy_->ProcessMessage(
      make_scoped_ptr(new BlimpMessage),
      base::Bind(&BlimpMessageThreadPipeTest::MockCompletionCallback,
                 base::Unretained(this)));
  DeletePipeOnThread();
  proxy_->ProcessMessage(
      make_scoped_ptr(new BlimpMessage),
      base::Bind(&BlimpMessageThreadPipeTest::MockCompletionCallback,
                 base::Unretained(this)));
}

TEST_F(BlimpMessageThreadPipeTest, NullCompletionCallback) {
  // Don't expect the mock to be called, but do expect not to crash.
  EXPECT_CALL(*this, MockCompletionCallback(_)).Times(0);

  proxy_->ProcessMessage(make_scoped_ptr(new BlimpMessage),
                         net::CompletionCallback());
}

}  // namespace blimp