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
media / mojo / services / deferred_destroy_unique_receiver_set_unittest.cc [blame]
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#include "media/mojo/services/deferred_destroy_unique_receiver_set.h"
#include <memory>
#include <utility>
#include "base/run_loop.h"
#include "base/test/task_environment.h"
#include "mojo/public/interfaces/bindings/tests/ping_service.test-mojom.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace media {
namespace {
using PingService = mojo::test::PingService;
class DeferredDestroyPingImpl : public DeferredDestroy<PingService> {
public:
DeferredDestroyPingImpl() { ++instance_count; }
~DeferredDestroyPingImpl() override { --instance_count; }
// DeferredDestroy<PingService> implementation.
void Ping(PingCallback callback) override {}
void OnDestroyPending(base::OnceClosure destroy_cb) override {
destroy_cb_ = std::move(destroy_cb);
if (can_destroy_)
std::move(destroy_cb_).Run();
}
void set_can_destroy() {
can_destroy_ = true;
if (destroy_cb_)
std::move(destroy_cb_).Run();
}
static int instance_count;
private:
bool can_destroy_ = false;
base::OnceClosure destroy_cb_;
};
int DeferredDestroyPingImpl::instance_count = 0;
DeferredDestroyPingImpl* AddDeferredDestroyReceiver(
DeferredDestroyUniqueReceiverSet<PingService>* receivers,
mojo::PendingRemote<PingService>* ptr) {
auto impl = std::make_unique<DeferredDestroyPingImpl>();
DeferredDestroyPingImpl* impl_ptr = impl.get();
receivers->Add(std::move(impl), ptr->InitWithNewPipeAndPassReceiver());
return impl_ptr;
}
class DeferredDestroyUniqueReceiverSetTest : public testing::Test {
public:
DeferredDestroyUniqueReceiverSetTest() = default;
~DeferredDestroyUniqueReceiverSetTest() override = default;
protected:
base::test::TaskEnvironment task_environment_;
};
TEST_F(DeferredDestroyUniqueReceiverSetTest, Destructor) {
mojo::PendingRemote<PingService> ping[2];
auto receivers =
std::make_unique<DeferredDestroyUniqueReceiverSet<PingService>>();
for (int i = 0; i < 2; ++i)
AddDeferredDestroyReceiver(receivers.get(), ping + i);
EXPECT_EQ(2, DeferredDestroyPingImpl::instance_count);
receivers.reset();
EXPECT_EQ(0, DeferredDestroyPingImpl::instance_count);
}
TEST_F(DeferredDestroyUniqueReceiverSetTest, ConnectionError) {
mojo::PendingRemote<PingService> ping[4];
DeferredDestroyPingImpl* impl[4];
auto receivers =
std::make_unique<DeferredDestroyUniqueReceiverSet<PingService>>();
for (int i = 0; i < 4; ++i)
impl[i] = AddDeferredDestroyReceiver(receivers.get(), ping + i);
EXPECT_EQ(4, DeferredDestroyPingImpl::instance_count);
// Destroy deferred after disconnection until set_can_destroy()..
ping[0].reset();
base::RunLoop().RunUntilIdle();
EXPECT_EQ(4, DeferredDestroyPingImpl::instance_count);
impl[0]->set_can_destroy();
base::RunLoop().RunUntilIdle();
EXPECT_EQ(3, DeferredDestroyPingImpl::instance_count);
// Destroyed immediately after disconnection if set_can_destroy() in
// advance.
impl[1]->set_can_destroy();
ping[1].reset();
base::RunLoop().RunUntilIdle();
EXPECT_EQ(2, DeferredDestroyPingImpl::instance_count);
// Deferred after connection until receiver set destruction.
ping[2].reset();
base::RunLoop().RunUntilIdle();
EXPECT_EQ(2, DeferredDestroyPingImpl::instance_count);
// Destructing the receiver set will destruct all impls, including deferred
// destroy impls.
receivers.reset();
EXPECT_EQ(0, DeferredDestroyPingImpl::instance_count);
}
TEST_F(DeferredDestroyUniqueReceiverSetTest, CloseAllReceivers) {
mojo::PendingRemote<PingService> ping[3];
DeferredDestroyPingImpl* impl[3];
DeferredDestroyUniqueReceiverSet<PingService> receivers;
for (int i = 0; i < 2; ++i)
impl[i] = AddDeferredDestroyReceiver(&receivers, ping + i);
EXPECT_EQ(2, DeferredDestroyPingImpl::instance_count);
EXPECT_FALSE(receivers.empty());
receivers.CloseAllReceivers();
EXPECT_EQ(0, DeferredDestroyPingImpl::instance_count);
EXPECT_TRUE(receivers.empty());
// After CloseAllReceivers, new added receivers can still be deferred
// destroyed.
impl[2] = AddDeferredDestroyReceiver(&receivers, ping + 2);
EXPECT_EQ(1, DeferredDestroyPingImpl::instance_count);
ping[2].reset();
base::RunLoop().RunUntilIdle();
EXPECT_EQ(1, DeferredDestroyPingImpl::instance_count);
impl[2]->set_can_destroy();
base::RunLoop().RunUntilIdle();
EXPECT_EQ(0, DeferredDestroyPingImpl::instance_count);
}
} // namespace
} // namespace media