Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

3.1 KB
Raw Blame
/* eslint-disable no-await-in-loop */

import { describe, expect, it } from "vitest";

import { KeyStorePrefixes } from "@app/keystore/keystore";

import {
  APP_CONNECTION_AGGREGATE_CONCURRENCY_LIMIT,
  APP_CONNECTION_CONCURRENCY_LIMIT,
  releaseAppConnectionConcurrency,
  tryAdmitAppConnectionConcurrency
} from "./app-connection-concurrency-fns";

const buildKeyStore = () => {
  const counts = new Map<string, number>();

  return {
    counts,
    incrementByAndRefreshExpiryIfUnderLimit: async (key: string, limit: number) => {
      const next = (counts.get(key) ?? 0) + 1;
      if (next > limit) return -1;
      counts.set(key, next);
      return next;
    },
    decrementByOrDelete: async (key: string) => {
      const next = (counts.get(key) ?? 0) - 1;
      if (next <= 0) counts.delete(key);
      else counts.set(key, next);
      return next;
    }
  };
};

const CONNECTION = "connection-1";
const aggregateKey = KeyStorePrefixes.AppConnectionConcurrentJobs(CONNECTION);

describe("app connection concurrency", () => {
  it("caps a single host at the per-host limit", async () => {
    const keyStore = buildKeyStore();

    for (let i = 0; i < APP_CONNECTION_CONCURRENCY_LIMIT; i += 1) {
      expect(await tryAdmitAppConnectionConcurrency(keyStore, CONNECTION, "host-a")).toBe(true);
    }
    expect(await tryAdmitAppConnectionConcurrency(keyStore, CONNECTION, "host-a")).toBe(false);
  });

  it("caps the connection across every host it backs", async () => {
    const keyStore = buildKeyStore();
    let admitted = 0;

    for (let host = 0; host < 50; host += 1) {
      for (let job = 0; job < APP_CONNECTION_CONCURRENCY_LIMIT; job += 1) {
        if (await tryAdmitAppConnectionConcurrency(keyStore, CONNECTION, `host-${host}`)) admitted += 1;
      }
    }

    expect(admitted).toBe(APP_CONNECTION_AGGREGATE_CONCURRENCY_LIMIT);
  });

  it("does not consume an aggregate slot when the per-host limit refuses", async () => {
    const keyStore = buildKeyStore();

    for (let i = 0; i < APP_CONNECTION_CONCURRENCY_LIMIT; i += 1) {
      await tryAdmitAppConnectionConcurrency(keyStore, CONNECTION, "host-a");
    }
    const aggregateBefore = keyStore.counts.get(aggregateKey);

    expect(await tryAdmitAppConnectionConcurrency(keyStore, CONNECTION, "host-a")).toBe(false);
    expect(keyStore.counts.get(aggregateKey)).toBe(aggregateBefore);
  });

  it("releases both tiers so slots are reusable", async () => {
    const keyStore = buildKeyStore();

    await tryAdmitAppConnectionConcurrency(keyStore, CONNECTION, "host-a");
    await releaseAppConnectionConcurrency(keyStore, CONNECTION, "host-a");

    expect(keyStore.counts.size).toBe(0);
  });

  it("keeps one tier for a hosted sync, where the connection key is the host key", async () => {
    const keyStore = buildKeyStore();

    for (let i = 0; i < APP_CONNECTION_CONCURRENCY_LIMIT; i += 1) {
      expect(await tryAdmitAppConnectionConcurrency(keyStore, CONNECTION)).toBe(true);
    }
    expect(await tryAdmitAppConnectionConcurrency(keyStore, CONNECTION)).toBe(false);
    expect(keyStore.counts.get(aggregateKey)).toBe(APP_CONNECTION_CONCURRENCY_LIMIT);
  });
});