summaryrefslogtreecommitdiffstats
path: root/mojo/message_pump/message_pump_mojo_unittest.cc
blob: 805b14120b67835885dcf386b16c4aafe19fc584 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Copyright 2013 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 "mojo/message_pump/message_pump_mojo.h"

#include "base/macros.h"
#include "base/message_loop/message_loop_test.h"
#include "base/run_loop.h"
#include "mojo/message_pump/message_pump_mojo_handler.h"
#include "mojo/public/cpp/system/core.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace mojo {
namespace common {
namespace test {

scoped_ptr<base::MessagePump> CreateMojoMessagePump() {
  return scoped_ptr<base::MessagePump>(new MessagePumpMojo());
}

RUN_MESSAGE_LOOP_TESTS(Mojo, &CreateMojoMessagePump);

class CountingMojoHandler : public MessagePumpMojoHandler {
 public:
  CountingMojoHandler() : success_count_(0), error_count_(0) {}

  void OnHandleReady(const Handle& handle) override {
    ReadMessageRaw(static_cast<const MessagePipeHandle&>(handle),
                   NULL,
                   NULL,
                   NULL,
                   NULL,
                   MOJO_READ_MESSAGE_FLAG_NONE);
    ++success_count_;
    if (success_count_ == success_callback_count_ &&
        !success_callback_.is_null()) {
      success_callback_.Run();
      success_callback_.Reset();
    }
  }

  void set_success_callback(const base::Closure& callback,
                            int success_count) {
    success_callback_ = callback;
    success_callback_count_ = success_count;
  }

  void OnHandleError(const Handle& handle, MojoResult result) override {
    ++error_count_;
    if (!error_callback_.is_null()) {
      error_callback_.Run();
      error_callback_.Reset();
    }
  }

  void set_error_callback(const base::Closure& callback) {
    error_callback_ = callback;
  }

  int success_count() { return success_count_; }
  int error_count() { return error_count_; }

 private:
  int success_count_;
  int error_count_;

  base::Closure error_callback_;
  int success_callback_count_;

  base::Closure success_callback_;

  DISALLOW_COPY_AND_ASSIGN(CountingMojoHandler);
};

class CountingObserver : public MessagePumpMojo::Observer {
 public:
  void WillSignalHandler() override { will_signal_handler_count++; }
  void DidSignalHandler() override { did_signal_handler_count++; }

  int will_signal_handler_count = 0;
  int did_signal_handler_count = 0;
};

TEST(MessagePumpMojo, RunUntilIdle) {
  base::MessageLoop message_loop(MessagePumpMojo::Create());
  CountingMojoHandler handler;
  base::RunLoop run_loop;
  handler.set_success_callback(run_loop.QuitClosure(), 2);
  MessagePipe handles;
  MessagePumpMojo::current()->AddHandler(&handler,
                                         handles.handle0.get(),
                                         MOJO_HANDLE_SIGNAL_READABLE,
                                         base::TimeTicks());
  WriteMessageRaw(
      handles.handle1.get(), NULL, 0, NULL, 0, MOJO_WRITE_MESSAGE_FLAG_NONE);
  WriteMessageRaw(
      handles.handle1.get(), NULL, 0, NULL, 0, MOJO_WRITE_MESSAGE_FLAG_NONE);
  MojoHandleSignalsState hss;
  ASSERT_EQ(MOJO_RESULT_OK,
            MojoWait(handles.handle0.get().value(), MOJO_HANDLE_SIGNAL_READABLE,
                      MOJO_DEADLINE_INDEFINITE, &hss));
  run_loop.Run();
  EXPECT_EQ(2, handler.success_count());
}

TEST(MessagePumpMojo, Observer) {
  base::MessageLoop message_loop(MessagePumpMojo::Create());

  CountingObserver observer;
  MessagePumpMojo::current()->AddObserver(&observer);

  CountingMojoHandler handler;
  base::RunLoop run_loop;
  handler.set_success_callback(run_loop.QuitClosure(), 1);
  MessagePipe handles;
  MessagePumpMojo::current()->AddHandler(&handler,
                                         handles.handle0.get(),
                                         MOJO_HANDLE_SIGNAL_READABLE,
                                         base::TimeTicks());
  WriteMessageRaw(
      handles.handle1.get(), NULL, 0, NULL, 0, MOJO_WRITE_MESSAGE_FLAG_NONE);

  MojoHandleSignalsState hss;
  ASSERT_EQ(MOJO_RESULT_OK,
            MojoWait(handles.handle0.get().value(), MOJO_HANDLE_SIGNAL_READABLE,
                      MOJO_DEADLINE_INDEFINITE, &hss));
  run_loop.Run();
  EXPECT_EQ(1, handler.success_count());
  EXPECT_EQ(1, observer.will_signal_handler_count);
  EXPECT_EQ(1, observer.did_signal_handler_count);
  MessagePumpMojo::current()->RemoveObserver(&observer);

  base::RunLoop run_loop2;
  handler.set_success_callback(run_loop2.QuitClosure(), 2);
  WriteMessageRaw(
      handles.handle1.get(), NULL, 0, NULL, 0, MOJO_WRITE_MESSAGE_FLAG_NONE);
  ASSERT_EQ(MOJO_RESULT_OK,
            MojoWait(handles.handle0.get().value(), MOJO_HANDLE_SIGNAL_READABLE,
                      MOJO_DEADLINE_INDEFINITE, &hss));
  run_loop2.Run();
  EXPECT_EQ(2, handler.success_count());
  EXPECT_EQ(1, observer.will_signal_handler_count);
  EXPECT_EQ(1, observer.did_signal_handler_count);
}

TEST(MessagePumpMojo, UnregisterAfterDeadline) {
  base::MessageLoop message_loop(MessagePumpMojo::Create());
  CountingMojoHandler handler;
  base::RunLoop run_loop;
  handler.set_error_callback(run_loop.QuitClosure());
  MessagePipe handles;
  MessagePumpMojo::current()->AddHandler(
      &handler,
      handles.handle0.get(),
      MOJO_HANDLE_SIGNAL_READABLE,
      base::TimeTicks::Now() - base::TimeDelta::FromSeconds(1));
  run_loop.Run();
  EXPECT_EQ(1, handler.error_count());
}

TEST(MessagePumpMojo, AddClosedHandle) {
  base::MessageLoop message_loop(MessagePumpMojo::Create());
  CountingMojoHandler handler;
  MessagePipe handles;
  Handle closed_handle = handles.handle0.get();
  handles.handle0.reset();
  MessagePumpMojo::current()->AddHandler(
      &handler, closed_handle, MOJO_HANDLE_SIGNAL_READABLE, base::TimeTicks());
  base::RunLoop run_loop;
  run_loop.RunUntilIdle();
  MessagePumpMojo::current()->RemoveHandler(closed_handle);
  EXPECT_EQ(0, handler.error_count());
  EXPECT_EQ(0, handler.success_count());
}

TEST(MessagePumpMojo, CloseAfterAdding) {
  base::MessageLoop message_loop(MessagePumpMojo::Create());
  CountingMojoHandler handler;
  MessagePipe handles;
  MessagePumpMojo::current()->AddHandler(&handler, handles.handle0.get(),
                                         MOJO_HANDLE_SIGNAL_READABLE,
                                         base::TimeTicks());
  handles.handle0.reset();
  base::RunLoop run_loop;
  run_loop.RunUntilIdle();
  EXPECT_EQ(1, handler.error_count());
  EXPECT_EQ(0, handler.success_count());
}

}  // namespace test
}  // namespace common
}  // namespace mojo