tor-browser

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

close.any.js (2265B)


      1 // How long (in ms) these tests should wait before deciding no further messages
      2 // will be received.
      3 const time_to_wait_for_messages = 100;
      4 
      5 async_test(t => {
      6    const c = new MessageChannel();
      7    c.port1.onmessage = t.unreached_func('Should not have delivered message');
      8    c.port1.close();
      9    c.port2.postMessage('TEST');
     10    setTimeout(t.step_func_done(), time_to_wait_for_messages);
     11  }, 'Message sent to closed port should not arrive.');
     12 
     13 async_test(t => {
     14    const c = new MessageChannel();
     15    c.port1.onmessage = t.unreached_func('Should not have delivered message');
     16    c.port2.close();
     17    c.port2.postMessage('TEST');
     18    setTimeout(t.step_func_done(), time_to_wait_for_messages);
     19  }, 'Message sent from closed port should not arrive.');
     20 
     21 async_test(t => {
     22    const c = new MessageChannel();
     23    c.port1.onmessage = t.unreached_func('Should not have delivered message');
     24    c.port1.close();
     25    const c2 = new MessageChannel();
     26    c2.port1.onmessage = t.step_func(e => {
     27        e.ports[0].postMessage('TESTMSG');
     28        setTimeout(t.step_func_done(), time_to_wait_for_messages);
     29      });
     30    c2.port2.postMessage('TEST', [c.port2]);
     31  }, 'Message sent to closed port from transferred port should not arrive.');
     32 
     33 async_test(t => {
     34    const c = new MessageChannel();
     35    let isClosed = false;
     36    c.port1.onmessage = t.step_func_done(e => {
     37        assert_true(isClosed);
     38        assert_equals(e.data, 'TEST');
     39      });
     40    c.port2.postMessage('TEST');
     41    c.port2.close();
     42    isClosed = true;
     43  }, 'Inflight messages should be delivered even when sending port is closed afterwards.');
     44 
     45 async_test(t => {
     46    const c = new MessageChannel();
     47    c.port1.onmessage = t.step_func_done(e => {
     48        if (e.data == 'DONE') t.done();
     49        assert_equals(e.data, 'TEST');
     50        c.port1.close();
     51      });
     52    c.port2.postMessage('TEST');
     53    c.port2.postMessage('DONE');
     54  }, 'Close in onmessage should not cancel inflight messages.');
     55 
     56 test(() => {
     57  const c1 = new MessageChannel();
     58  const c2 = new MessageChannel();
     59  c1.port1.close();
     60  assert_throws_dom("DataCloneError", () => c2.port1.postMessage(null, [c1.port1]));
     61  c2.port1.postMessage(null, [c1.port2]);
     62 }, "close() detaches a MessagePort (but not the one its entangled with)");