<commit_before>const assert = require("assert");
const {
  ApolloClient,
  InMemoryCache,
  gql,
  makeVar,
} = require("@apollo/client/core");

function itAsync(message, testFn) {
  let timeout;
  (function pollGC() {
    gc(); // enabled by --expose-gc
    timeout = setTimeout(pollGC, 100);
  })();
  return it(message, () => new Promise(testFn).finally(() => {
    clearTimeout(timeout);
  }));
}

describe("garbage collection", () => {
  itAsync("should collect client.cache after client.stop() called", resolve => {
    const registry = new FinalizationRegistry(key => {
      clearTimeout(timeout);
      assert.strictEqual(key, "client.cache");
      resolve();
    });

    const localVar = makeVar(123);

    (function (client) {
      registry.register(client.cache, "client.cache");

      client.watchQuery({
        query: gql`query { local }`,
      }).subscribe({
        next(result) {
          assert.deepStrictEqual(result.data, {
            local: 123,
          });

          client.stop();
        },
      });

    })(new ApolloClient({
      cache: new InMemoryCache({
        typePolicies: {
          Query: {
            fields: {
              local() {
                return localVar();
              },
            },
          },
        },
      }),
    }));
  });
});
<commit_msg>Make sure FinalizationRegistry itself not prematurely collected.<commit_after>const assert = require("assert");
const {
  ApolloClient,
  InMemoryCache,
  gql,
  makeVar,
} = require("@apollo/client/core");

function itAsync(message, testFn) {
  let timeout;
  (function pollGC() {
    gc(); // enabled by --expose-gc
    timeout = setTimeout(pollGC, 100);
  })();
  return it(message, () => new Promise(testFn).finally(() => {
    clearTimeout(timeout);
  }));
}

const registries = [];
function makeRegistry(callback, reject) {
  assert.strictEqual(typeof callback, "function");
  assert.strictEqual(typeof reject, "function");

  const registry = new FinalizationRegistry(key => {
    try {
      callback(key);
    } catch (error) {
      // Exceptions thrown in FinalizationRegistry callbacks can be tricky
      // for test frameworks to catch, without some help.
      reject(error);
    }
  });

  // If the registry object itself gets garbage collected before the
  // callback fires, the callback might never be called:
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/FinalizationRegistry#notes_on_cleanup_callbacks
  registries.push(registry);

  return registry;
}

describe("garbage collection", () => {
  itAsync("should collect client.cache after client.stop()", (resolve, reject) => {
    const registry = makeRegistry(key => {
      assert.strictEqual(key, "client.cache");
      resolve();
    }, reject);

    const localVar = makeVar(123);

    (function (client) {
      registry.register(client.cache, "client.cache");

      client.watchQuery({
        query: gql`query { local }`,
      }).subscribe({
        next(result) {
          assert.deepStrictEqual(result.data, {
            local: 123,
          });

          client.stop();
        },
      });

    })(new ApolloClient({
      cache: new InMemoryCache({
        typePolicies: {
          Query: {
            fields: {
              local() {
                return localVar();
              },
            },
          },
        },
      }),
    }));
  });
});

{'fraction_non_alphanumeric': 0.1462125660598943, 'fraction_numerical': 0.005284791544333529, 'mean_word_length': 2.3599605522682445, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 14, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5181601', 'n_tokens_mistral': 1006, 'n_tokens_neox': 963, 'n_words': 209}