summaryrefslogtreecommitdiffstats
path: root/devtools/server/tests/xpcshell/test_connection_closes_all_pools.js
blob: d69291485dc08b880161fff8ab6c7f0d67d03fc6 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const { Pool } = require("resource://devtools/shared/protocol/Pool.js");
const {
  DevToolsServerConnection,
} = require("resource://devtools/server/devtools-server-connection.js");
const {
  LocalDebuggerTransport,
} = require("resource://devtools/shared/transport/local-transport.js");

// Helper class to assert how many times a Pool was destroyed
class FakeActor extends Pool {
  constructor(...args) {
    super(...args);
    this.destroyedCount = 0;
  }

  destroy() {
    this.destroyedCount++;
    super.destroy();
  }
}

add_task(async function () {
  const transport = new LocalDebuggerTransport();
  const conn = new DevToolsServerConnection("prefix", transport);

  // Setup a flat pool hierarchy with multiple pools:
  //
  // - pool1
  //   |
  //   \- actor1
  //
  // - pool2
  //   |
  //   |- actor2a
  //   |
  //   \- actor2b
  //
  // From the point of view of the DevToolsServerConnection, the only pools
  // registered in _extraPools should be pool1 and pool2. Even though actor1,
  // actor2a and actor2b extend Pool, they don't manage other pools.
  const actor1 = new FakeActor(conn);
  const pool1 = new Pool(conn, "pool-1");
  pool1.manage(actor1);

  const actor2a = new FakeActor(conn);
  const actor2b = new FakeActor(conn);
  const pool2 = new Pool(conn, "pool-2");
  pool2.manage(actor2a);
  pool2.manage(actor2b);

  ok(!!actor1.actorID, "actor1 has a valid actorID");
  ok(!!actor2a.actorID, "actor2a has a valid actorID");
  ok(!!actor2b.actorID, "actor2b has a valid actorID");

  conn.close();

  equal(actor1.destroyedCount, 1, "actor1 was successfully destroyed");
  equal(actor2a.destroyedCount, 1, "actor2 was successfully destroyed");
  equal(actor2b.destroyedCount, 1, "actor2 was successfully destroyed");
});

add_task(async function () {
  const transport = new LocalDebuggerTransport();
  const conn = new DevToolsServerConnection("prefix", transport);

  // Setup a nested pool hierarchy:
  //
  // - pool
  //   |
  //   \- parentActor
  //      |
  //      \- childActor
  //
  // Since parentActor is also a Pool from the point of view of the
  // DevToolsServerConnection, it will attempt to destroy it when looping on
  // this._extraPools. But since `parentActor` is also a direct child of `pool`,
  // it has already been destroyed by the Pool destroy() mechanism.
  //
  // Here we check that we don't call destroy() too many times on a single Pool.
  // Even though Pool::destroy() is stable when called multiple times, we can't
  // guarantee the same for classes inheriting Pool.
  const childActor = new FakeActor(conn);
  const parentActor = new FakeActor(conn);
  const pool = new Pool(conn, "pool");
  pool.manage(parentActor);
  parentActor.manage(childActor);

  ok(!!parentActor.actorID, "customActor has a valid actorID");
  ok(!!childActor.actorID, "childActor has a valid actorID");

  conn.close();

  equal(parentActor.destroyedCount, 1, "parentActor was destroyed once");
  equal(parentActor.destroyedCount, 1, "customActor was destroyed once");
});