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

Side by Side Diff: remoting/host/ack_or_timeout_reporter_unittest.cc

Issue 719983002: Reporting of policy errors via host-offline-reason: part 3 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@hor-nohoststatussender
Patch Set: git cl format Created 6 years 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2014 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 "remoting/host/ack_or_timeout_reporter.h"
6
7 #include "base/bind.h"
8 #include "base/location.h"
9 #include "base/memory/weak_ptr.h"
10 #include "base/sequenced_task_runner.h"
11 #include "base/time/time.h"
12 #include "remoting/host/mock_callback.h"
13
14 #include "testing/gmock/include/gmock/gmock.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16
17 namespace remoting {
18
19 class MockSequencedTaskRunner : public base::SequencedTaskRunner {
20 public:
21 MOCK_METHOD3(PostDelayedTask,
22 bool(const tracked_objects::Location&,
23 const base::Closure&,
24 base::TimeDelta));
25
26 MOCK_CONST_METHOD0(RunsTasksOnCurrentThread, bool());
27
28 MOCK_METHOD3(PostNonNestableDelayedTask,
29 bool(const tracked_objects::Location&,
30 const base::Closure&,
31 base::TimeDelta));
32
33 private:
34 ~MockSequencedTaskRunner() {}
35 };
36
37 const base::TimeDelta kTestTimeout = base::TimeDelta::FromSeconds(123);
38
39 static void CallReportAckOrTimeoutAndCaptureCallbacks(
40 const base::Callback<void(AckOrTimeout)>& ack_or_timeout_callback,
41 /* out */ base::Closure* captured_ack_callback,
42 /* out */ base::Closure* captured_timeout_callback) {
43 // Setup mocks.
44 MockCallback<void(const base::Closure&)> mock_function_that_acks;
45 scoped_refptr<MockSequencedTaskRunner> mock_task_runner(
46 new MockSequencedTaskRunner());
47
48 // Capture |captured_ack_callback| and |captured_timeout_callback|.
49 EXPECT_CALL(mock_function_that_acks, Run(testing::_))
50 .WillOnce(testing::SaveArg<0>(captured_ack_callback));
51 EXPECT_CALL(*mock_task_runner,
52 PostDelayedTask(testing::_, testing::_, kTestTimeout))
53 .WillOnce(testing::DoAll(testing::SaveArg<1>(captured_timeout_callback),
54 testing::Return(true)));
55
56 // Call function-under-test.
57 ReportAckOrTimeout(mock_function_that_acks.GetCallback(), kTestTimeout,
58 mock_task_runner, ack_or_timeout_callback);
59 }
60
61 TEST(AckOrTimeoutReporterTest, FirstAckCalledAsynchronously) {
62 MockCallback<void(AckOrTimeout)> mock_ack_or_timeout_callback;
63 base::Closure captured_ack_callback;
64 base::Closure captured_timeout_callback;
65
66 // |mock_ack_or_timeout_callback| should not be called until
67 // |captured_ack_callback| or |captured_timeout_callback| is called.
68 EXPECT_CALL(mock_ack_or_timeout_callback, Run(testing::_)).Times(0);
69
70 CallReportAckOrTimeoutAndCaptureCallbacks(
71 mock_ack_or_timeout_callback.GetCallback(), &captured_ack_callback,
72 &captured_timeout_callback);
73
74 {
75 // |mock_ack_or_timeout_callback| should be called as soon as
76 // |captured_ack_callback| runs.
77 EXPECT_CALL(mock_ack_or_timeout_callback, Run(AckOrTimeout::Ack));
78 captured_ack_callback.Run();
79 }
80
81 // |ack_or_timeout_callback| should be dropped after being called once.
82 // This is expectation is depended on by |MinimumHeartbeatSupporter| to
83 // manage its lifetime (i.e. release references to self after getting an
84 // ack).
85 EXPECT_FALSE(mock_ack_or_timeout_callback.HasRemainingCallbacks());
86
87 // It should be safe to call the callbacks multiple times.
88 captured_ack_callback.Run();
89 captured_timeout_callback.Run();
90 }
91
92 TEST(AckOrTimeoutReporterTest, FirstTimeoutCalledAsynchronously) {
93 MockCallback<void(AckOrTimeout)> mock_ack_or_timeout_callback;
94 base::Closure captured_ack_callback;
95 base::Closure captured_timeout_callback;
96
97 // |mock_ack_or_timeout_callback| should not be called until
98 // |captured_ack_callback| or |captured_timeout_callback| is called.
99 EXPECT_CALL(mock_ack_or_timeout_callback, Run(testing::_)).Times(0);
100
101 CallReportAckOrTimeoutAndCaptureCallbacks(
102 mock_ack_or_timeout_callback.GetCallback(), &captured_ack_callback,
103 &captured_timeout_callback);
104
105 {
106 // |mock_ack_or_timeout_callback| should be called as soon as
107 // |captured_ack_callback| runs.
108 EXPECT_CALL(mock_ack_or_timeout_callback, Run(AckOrTimeout::Timeout));
109 captured_timeout_callback.Run();
110 }
111
112 // |ack_or_timeout_callback| should be dropped after being called once.
113 // This is expectation is depended on by |MinimumHeartbeatSupporter| to
114 // manage its lifetime (i.e. release references to self after getting an
115 // ack).
116 EXPECT_FALSE(mock_ack_or_timeout_callback.HasRemainingCallbacks());
117
118 // It should be safe to call the callbacks multiple times.
119 captured_ack_callback.Run();
120 captured_timeout_callback.Run();
121 }
122
123 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698