tor-browser

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

test_connection_interrupt.js (3519B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 // This file tests the functionality of mozIStorageAsyncConnection::interrupt
      5 // in the asynchronous case.
      6 add_task(async function test_wr_async_conn() {
      7  // Interrupt cannot be used on R/W async connections.
      8  let db = await openAsyncDatabase(getTestDB());
      9  await db.interrupt();
     10  info("should be able to interrupt a R/W async connection");
     11  await asyncClose(db);
     12 });
     13 
     14 add_task(async function test_closed_conn() {
     15  let db = await openAsyncDatabase(getTestDB(), { readOnly: true });
     16  await asyncClose(db);
     17  Assert.throws(
     18    () => db.interrupt(),
     19    /NS_ERROR_NOT_INITIALIZED/,
     20    "interrupt() should throw if invoked on a closed connection"
     21  );
     22 });
     23 
     24 add_task(
     25  {
     26    // We use a timeout in the test that may be insufficient on Android emulators.
     27    // We don't really need the Android coverage, so skip on Android.
     28    skip_if: () => AppConstants.platform == "android",
     29  },
     30  async function test_async_conn() {
     31    let db = await openAsyncDatabase(getTestDB(), { readOnly: true });
     32    // This query is built to hang forever.
     33    let stmt = db.createAsyncStatement(`
     34    WITH RECURSIVE test(n) AS (
     35      VALUES(1)
     36      UNION ALL
     37      SELECT n + 1 FROM test
     38    )
     39    SELECT t.n
     40    FROM test,test AS t`);
     41 
     42    let completePromise = new Promise((resolve, reject) => {
     43      let listener = {
     44        handleResult() {
     45          reject();
     46        },
     47        handleError() {
     48          reject();
     49        },
     50        handleCompletion(aReason) {
     51          resolve(aReason);
     52        },
     53      };
     54      stmt.executeAsync(listener);
     55      stmt.finalize();
     56    });
     57 
     58    // Wait for the statement to be executing.
     59    // This is not rock-solid, see the discussion in bug 1320301. A better
     60    // approach will be evaluated in a separate bug.
     61    await new Promise(resolve => do_timeout(500, resolve));
     62 
     63    db.interrupt();
     64 
     65    Assert.equal(
     66      await completePromise,
     67      Ci.mozIStorageStatementCallback.REASON_CANCELED,
     68      "Should have been canceled"
     69    );
     70 
     71    await asyncClose(db);
     72  }
     73 );
     74 
     75 add_task(
     76  {
     77    // We use a timeout in the test that may be insufficient on Android emulators.
     78    // We don't really need the Android coverage, so skip on Android.
     79    skip_if: () => AppConstants.platform == "android",
     80  },
     81  async function test_async_conn() {
     82    let db = await openAsyncDatabase(getTestDB());
     83    // This query is built to hang forever.
     84    let stmt = db.createAsyncStatement(`
     85    WITH RECURSIVE test(n) AS (
     86      VALUES(1)
     87      UNION ALL
     88      SELECT n + 1 FROM test
     89    )
     90    SELECT t.n
     91    FROM test,test AS t`);
     92 
     93    let completePromise = new Promise((resolve, reject) => {
     94      let listener = {
     95        handleResult() {
     96          reject();
     97        },
     98        handleError() {
     99          reject();
    100        },
    101        handleCompletion(aReason) {
    102          resolve(aReason);
    103        },
    104      };
    105      stmt.executeAsync(listener);
    106      stmt.finalize();
    107    });
    108 
    109    // Wait for the statement to be executing.
    110    // This is not rock-solid, see the discussion in bug 1320301. A better
    111    // approach will be evaluated in a separate bug.
    112    await new Promise(resolve => do_timeout(500, resolve));
    113 
    114    // We are going to interrupt a database connection
    115    db.interrupt();
    116 
    117    Assert.equal(
    118      await completePromise,
    119      Ci.mozIStorageStatementCallback.REASON_CANCELED,
    120      "Should have been able to cancel even for R/W database"
    121    );
    122 
    123    await asyncClose(db);
    124  }
    125 );