summaryrefslogtreecommitdiffstats
path: root/dom/media/webrtc/transport/test/runnable_utils_unittest.cpp
blob: 70707b148f4074fad6ff485d4cbdd6fea440d824 (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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

// Original author: ekr@rtfm.com
#include <iostream>

#include "nsCOMPtr.h"
#include "nsNetCID.h"

#include "mozilla/RefPtr.h"
#include "mozilla/UniquePtr.h"

#include "nsServiceManagerUtils.h"
#include "nsThreadUtils.h"

#include "runnable_utils.h"

#define GTEST_HAS_RTTI 0
#include "gtest/gtest.h"
#include "gtest_utils.h"

using namespace mozilla;

namespace {

// Helper used to make sure args are properly copied and/or moved.
struct CtorDtorState {
  enum class State { Empty, Copy, Explicit, Move, Moved };

  static const char* ToStr(const State& state) {
    switch (state) {
      case State::Empty:
        return "empty";
      case State::Copy:
        return "copy";
      case State::Explicit:
        return "explicit";
      case State::Move:
        return "move";
      case State::Moved:
        return "moved";
      default:
        return "unknown";
    }
  }

  void DumpState() const { std::cerr << ToStr(state_) << std::endl; }

  CtorDtorState() { DumpState(); }

  explicit CtorDtorState(int* destroyed)
      : dtor_count_(destroyed), state_(State::Explicit) {
    DumpState();
  }

  CtorDtorState(const CtorDtorState& other)
      : dtor_count_(other.dtor_count_), state_(State::Copy) {
    DumpState();
  }

  // Clear the other's dtor counter so it's not counted if moved.
  CtorDtorState(CtorDtorState&& other)
      : dtor_count_(std::exchange(other.dtor_count_, nullptr)),
        state_(State::Move) {
    other.state_ = State::Moved;
    DumpState();
  }

  ~CtorDtorState() {
    const char* const state = ToStr(state_);
    std::cerr << "Destructor called with end state: " << state << std::endl;

    if (dtor_count_) {
      ++*dtor_count_;
    }
  }

  int* dtor_count_ = nullptr;
  State state_ = State::Empty;
};

class Destructor {
 private:
  ~Destructor() {
    std::cerr << "Destructor called" << std::endl;
    *destroyed_ = true;
  }

 public:
  explicit Destructor(bool* destroyed) : destroyed_(destroyed) {}

  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(Destructor)

 private:
  bool* destroyed_;
};

class TargetClass {
 public:
  explicit TargetClass(int* ran) : ran_(ran) {}

  void m1(int x) {
    std::cerr << __FUNCTION__ << " " << x << std::endl;
    *ran_ = 1;
  }

  void m2(int x, int y) {
    std::cerr << __FUNCTION__ << " " << x << " " << y << std::endl;
    *ran_ = 2;
  }

  void m1set(bool* z) {
    std::cerr << __FUNCTION__ << std::endl;
    *z = true;
  }
  int return_int(int x) {
    std::cerr << __FUNCTION__ << std::endl;
    return x;
  }

  void destructor_target_ref(RefPtr<Destructor> destructor) {}

  int* ran_;
};

class RunnableArgsTest : public MtransportTest {
 public:
  RunnableArgsTest() : MtransportTest(), ran_(0), cl_(&ran_) {}

  void Test1Arg() {
    Runnable* r = WrapRunnable(&cl_, &TargetClass::m1, 1);
    r->Run();
    ASSERT_EQ(1, ran_);
  }

  void Test2Args() {
    Runnable* r = WrapRunnable(&cl_, &TargetClass::m2, 1, 2);
    r->Run();
    ASSERT_EQ(2, ran_);
  }

 private:
  int ran_;
  TargetClass cl_;
};

class DispatchTest : public MtransportTest {
 public:
  DispatchTest() : MtransportTest(), ran_(0), cl_(&ran_) {}

  void SetUp() {
    MtransportTest::SetUp();

    nsresult rv;
    target_ = do_GetService(NS_SOCKETTRANSPORTSERVICE_CONTRACTID, &rv);
    ASSERT_TRUE(NS_SUCCEEDED(rv));
  }

  void Test1Arg() {
    Runnable* r = WrapRunnable(&cl_, &TargetClass::m1, 1);
    NS_DispatchAndSpinEventLoopUntilComplete("DispatchTest::Test1Arg"_ns,
                                             target_, do_AddRef(r));
    ASSERT_EQ(1, ran_);
  }

  void Test2Args() {
    Runnable* r = WrapRunnable(&cl_, &TargetClass::m2, 1, 2);
    NS_DispatchAndSpinEventLoopUntilComplete("DispatchTest::Test2Args"_ns,
                                             target_, do_AddRef(r));
    ASSERT_EQ(2, ran_);
  }

  void Test1Set() {
    bool x = false;
    NS_DispatchAndSpinEventLoopUntilComplete(
        "DispatchTest::Test1Set"_ns, target_,
        do_AddRef(WrapRunnable(&cl_, &TargetClass::m1set, &x)));
    ASSERT_TRUE(x);
  }

  void TestRet() {
    int z;
    int x = 10;

    NS_DispatchAndSpinEventLoopUntilComplete(
        "DispatchTest::TestRet"_ns, target_,
        do_AddRef(WrapRunnableRet(&z, &cl_, &TargetClass::return_int, x)));
    ASSERT_EQ(10, z);
  }

 protected:
  int ran_;
  TargetClass cl_;
  nsCOMPtr<nsIEventTarget> target_;
};

TEST_F(RunnableArgsTest, OneArgument) { Test1Arg(); }

TEST_F(RunnableArgsTest, TwoArguments) { Test2Args(); }

TEST_F(DispatchTest, OneArgument) { Test1Arg(); }

TEST_F(DispatchTest, TwoArguments) { Test2Args(); }

TEST_F(DispatchTest, Test1Set) { Test1Set(); }

TEST_F(DispatchTest, TestRet) { TestRet(); }

void SetNonMethod(TargetClass* cl, int x) { cl->m1(x); }

int SetNonMethodRet(TargetClass* cl, int x) {
  cl->m1(x);

  return x;
}

TEST_F(DispatchTest, TestNonMethod) {
  test_utils_->SyncDispatchToSTS(WrapRunnableNM(SetNonMethod, &cl_, 10));

  ASSERT_EQ(1, ran_);
}

TEST_F(DispatchTest, TestNonMethodRet) {
  int z;

  test_utils_->SyncDispatchToSTS(
      WrapRunnableNMRet(&z, SetNonMethodRet, &cl_, 10));

  ASSERT_EQ(1, ran_);
  ASSERT_EQ(10, z);
}

TEST_F(DispatchTest, TestDestructorRef) {
  bool destroyed = false;
  {
    RefPtr<Destructor> destructor = new Destructor(&destroyed);
    NS_DispatchAndSpinEventLoopUntilComplete(
        "DispatchTest::TestDestructorRef"_ns, target_,
        do_AddRef(WrapRunnable(&cl_, &TargetClass::destructor_target_ref,
                               destructor)));
    ASSERT_FALSE(destroyed);
  }
  ASSERT_TRUE(destroyed);

  // Now try with a move.
  destroyed = false;
  {
    RefPtr<Destructor> destructor = new Destructor(&destroyed);
    NS_DispatchAndSpinEventLoopUntilComplete(
        "DispatchTest::TestDestructorRef"_ns, target_,
        do_AddRef(WrapRunnable(&cl_, &TargetClass::destructor_target_ref,
                               std::move(destructor))));
    ASSERT_TRUE(destroyed);
  }
}

TEST_F(DispatchTest, TestMove) {
  int destroyed = 0;
  {
    CtorDtorState state(&destroyed);

    // Dispatch with:
    //   - moved arg
    //   - by-val capture in function should consume a move
    //   - expect destruction in the function scope
    NS_DispatchAndSpinEventLoopUntilComplete(
        "DispatchTest::TestMove"_ns, target_,
        do_AddRef(WrapRunnableNM([](CtorDtorState s) {}, std::move(state))));
    ASSERT_EQ(1, destroyed);
  }
  // Still shouldn't count when we go out of scope as it was moved.
  ASSERT_EQ(1, destroyed);

  {
    CtorDtorState state(&destroyed);

    // Dispatch with:
    //   - copied arg
    //   - by-val capture in function should consume a move
    //   - expect destruction in the function scope and call scope
    NS_DispatchAndSpinEventLoopUntilComplete(
        "DispatchTest::TestMove"_ns, target_,
        do_AddRef(WrapRunnableNM([](CtorDtorState s) {}, state)));
    ASSERT_EQ(2, destroyed);
  }
  // Original state should be destroyed
  ASSERT_EQ(3, destroyed);

  {
    CtorDtorState state(&destroyed);

    // Dispatch with:
    //   - moved arg
    //   - by-ref in function should accept a moved arg
    //   - expect destruction in the wrapper invocation scope
    NS_DispatchAndSpinEventLoopUntilComplete(
        "DispatchTest::TestMove"_ns, target_,
        do_AddRef(
            WrapRunnableNM([](const CtorDtorState& s) {}, std::move(state))));
    ASSERT_EQ(4, destroyed);
  }
  // Still shouldn't count when we go out of scope as it was moved.
  ASSERT_EQ(4, destroyed);

  {
    CtorDtorState state(&destroyed);

    // Dispatch with:
    //   - moved arg
    //   - r-value function should accept a moved arg
    //   - expect destruction in the wrapper invocation scope
    NS_DispatchAndSpinEventLoopUntilComplete(
        "DispatchTest::TestMove"_ns, target_,
        do_AddRef(WrapRunnableNM([](CtorDtorState&& s) {}, std::move(state))));
    ASSERT_EQ(5, destroyed);
  }
  // Still shouldn't count when we go out of scope as it was moved.
  ASSERT_EQ(5, destroyed);
}

TEST_F(DispatchTest, TestUniquePtr) {
  // Test that holding the class in UniquePtr works
  int ran = 0;
  auto cl = MakeUnique<TargetClass>(&ran);

  NS_DispatchAndSpinEventLoopUntilComplete(
      "DispatchTest::TestUniquePtr"_ns, target_,
      do_AddRef(WrapRunnable(std::move(cl), &TargetClass::m1, 1)));
  ASSERT_EQ(1, ran);

  // Test that UniquePtr works as a param to the runnable
  int destroyed = 0;
  {
    auto state = MakeUnique<CtorDtorState>(&destroyed);

    // Dispatch with:
    //   - moved arg
    //   - Function should move construct from arg
    //   - expect destruction in the wrapper invocation scope
    NS_DispatchAndSpinEventLoopUntilComplete(
        "DispatchTest::TestUniquePtr"_ns, target_,
        do_AddRef(WrapRunnableNM([](UniquePtr<CtorDtorState> s) {},
                                 std::move(state))));
    ASSERT_EQ(1, destroyed);
  }
  // Still shouldn't count when we go out of scope as it was moved.
  ASSERT_EQ(1, destroyed);
}

}  // end of namespace