tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_connection_closes_all_pools.js (3148B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 const { Pool } = require("resource://devtools/shared/protocol/Pool.js");
      7 const {
      8  DevToolsServerConnection,
      9 } = require("resource://devtools/server/devtools-server-connection.js");
     10 const {
     11  LocalDebuggerTransport,
     12 } = require("resource://devtools/shared/transport/local-transport.js");
     13 
     14 // Helper class to assert how many times a Pool was destroyed
     15 class FakeActor extends Pool {
     16  constructor(...args) {
     17    super(...args);
     18    this.destroyedCount = 0;
     19  }
     20 
     21  destroy() {
     22    this.destroyedCount++;
     23    super.destroy();
     24  }
     25 }
     26 
     27 add_task(async function () {
     28  const transport = new LocalDebuggerTransport();
     29  const conn = new DevToolsServerConnection("prefix", transport);
     30 
     31  // Setup a flat pool hierarchy with multiple pools:
     32  //
     33  // - pool1
     34  //   |
     35  //   \- actor1
     36  //
     37  // - pool2
     38  //   |
     39  //   |- actor2a
     40  //   |
     41  //   \- actor2b
     42  //
     43  // From the point of view of the DevToolsServerConnection, the only pools
     44  // registered in _extraPools should be pool1 and pool2. Even though actor1,
     45  // actor2a and actor2b extend Pool, they don't manage other pools.
     46  const actor1 = new FakeActor(conn);
     47  const pool1 = new Pool(conn, "pool-1");
     48  pool1.manage(actor1);
     49 
     50  const actor2a = new FakeActor(conn);
     51  const actor2b = new FakeActor(conn);
     52  const pool2 = new Pool(conn, "pool-2");
     53  pool2.manage(actor2a);
     54  pool2.manage(actor2b);
     55 
     56  ok(!!actor1.actorID, "actor1 has a valid actorID");
     57  ok(!!actor2a.actorID, "actor2a has a valid actorID");
     58  ok(!!actor2b.actorID, "actor2b has a valid actorID");
     59 
     60  conn.close();
     61 
     62  equal(actor1.destroyedCount, 1, "actor1 was successfully destroyed");
     63  equal(actor2a.destroyedCount, 1, "actor2 was successfully destroyed");
     64  equal(actor2b.destroyedCount, 1, "actor2 was successfully destroyed");
     65 });
     66 
     67 add_task(async function () {
     68  const transport = new LocalDebuggerTransport();
     69  const conn = new DevToolsServerConnection("prefix", transport);
     70 
     71  // Setup a nested pool hierarchy:
     72  //
     73  // - pool
     74  //   |
     75  //   \- parentActor
     76  //      |
     77  //      \- childActor
     78  //
     79  // Since parentActor is also a Pool from the point of view of the
     80  // DevToolsServerConnection, it will attempt to destroy it when looping on
     81  // this._extraPools. But since `parentActor` is also a direct child of `pool`,
     82  // it has already been destroyed by the Pool destroy() mechanism.
     83  //
     84  // Here we check that we don't call destroy() too many times on a single Pool.
     85  // Even though Pool::destroy() is stable when called multiple times, we can't
     86  // guarantee the same for classes inheriting Pool.
     87  const childActor = new FakeActor(conn);
     88  const parentActor = new FakeActor(conn);
     89  const pool = new Pool(conn, "pool");
     90  pool.manage(parentActor);
     91  parentActor.manage(childActor);
     92 
     93  ok(!!parentActor.actorID, "customActor has a valid actorID");
     94  ok(!!childActor.actorID, "childActor has a valid actorID");
     95 
     96  conn.close();
     97 
     98  equal(parentActor.destroyedCount, 1, "parentActor was destroyed once");
     99  equal(parentActor.destroyedCount, 1, "customActor was destroyed once");
    100 });