chromium/third_party/blink/web_tests/webaudio/internals/audioworkletnode-gc.https.html

<!doctype html>
<html>
<head>
  <title>Test GC of AudioWorkletNode</title>
  <script src="../../resources/gc.js"></script>
  <script src="../../resources/testharness.js"></script>
  <script src="../../resources/testharnessreport.js"></script>
</head>
<body>
  <script>
    // Creates an AudioWorkletNode (and AudioWorkletProcessor) and destroy
    // it immediately. Then manually trigger GC and check the number of
    // living WebAudio objects. See: https://crbug.com/1298955
    promise_test(async () => {
      // Just to make sure there's no residues from other internal tests.
      await asyncGC();
      assert_equals(internals.audioHandlerCount(), 0,
                    'the initial handler count should be zero.');

      const context = new AudioContext();
      const filePath = 'worklet-processors/falsy-processor.js';
      await context.audioWorklet.addModule(filePath);

      // Create a graph and start processing.
      let workletNode = new AudioWorkletNode(context, 'falsy-processor');

      return new Promise(resolve => {
        // Prepare steps when the processor returns false.
        workletNode.port.onmessage = async () => {
          workletNode.port.onmessage = null;
          workletNode.port.close();
          workletNode.disconnect();
          workletNode = null;

          // Assuming the "active flag" from the processor successfully marked
          // the mathching AudioWorkletNode, this should collect the node.
          await asyncGC();

          // Force the WebAudio's deferred task handler to clean up
          // deletable AudioHandlers. Without this, the deletable handlers
          // will be deleted a bit later.
          await context.close();

          assert_equals(internals.audioHandlerCount(), 1,
                        'the handler count after GC should be 1.');
          resolve();
        };

        // Start rendering the audio graph.
        workletNode.connect(context.destination);

        // Two handlers: the destination node and a woklet node.
        assert_equals(
            internals.audioHandlerCount(), 2,
            'the handler count should be 2 (destination and worklet)');
      });
    }, 'Test GC of AudioWorkletNode');
  </script>
</body>
</html>