chromium/chrome/test/data/extensions/api_test/service_worker/messaging/connect_to_worker/connect_and_post/service_worker_background.js

// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

let messageCount = 0;
chrome.runtime.onConnect.addListener(port => {
  // Prevent port to be garbage collected, which may result in closing
  // the port.
  self.port = port;
  port.onMessage.addListener(msg => {
    messageCount++;
    // Messages are sent every 100ms.
    // This means the service worker is alive for at least 2 seconds.
    if (messageCount == 20) {
      chrome.test.succeed();
    }
  });
});