Eine aufbereitete Darstellung der Quelle

 
     
 
 
Anforderungen  |   Konzepte  |   Entwurf  |   Entwicklung  |   Qualitätssicherung  |   Lebenszyklus  |   Steuerung
 
 
 
 

Benutzer

Quelle  batch-http.test.ts

  Sprache: JAVA
 

import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest";

vi.mock("../../infra/retry.js", () => ({
  retryAsync: vi.fn(async (run: () => Promise<unknown>) => await run()),
}));

vi.mock("./post-json.js", () => ({
  postJson: vi.fn(),
}));

describe("postJsonWithRetry", () => {
  let retryAsyncMock: ReturnType<
    typeof vi.mocked<typeof import("../../infra/retry.js").retryAsync>
  >;
  let postJsonMock: ReturnType<typeof vi.mocked<typeof import("./post-json.js").postJson>>;
  let postJsonWithRetry: typeof import("./batch-http.js").postJsonWithRetry;

  beforeAll(async () => {
    ({ postJsonWithRetry } = await import("./batch-http.js"));
    const retryModule = await import("../../infra/retry.js");
    const postJsonModule = await import("./post-json.js");
    retryAsyncMock = vi.mocked(retryModule.retryAsync);
    postJsonMock = vi.mocked(postJsonModule.postJson);
  });

  beforeEach(() => {
    vi.clearAllMocks();
  });

  it("posts JSON and returns parsed response payload", async () => {
    postJsonMock.mockImplementationOnce(async (params) => {
      return await params.parse({ ok: true, ids: [12] });
    });

    const result = await postJsonWithRetry<{ ok: boolean; ids: number[] }>({
      url: "https://memory.example/v1/batch",
      headers: { Authorization: "Bearer test" },
      body: { chunks: ["a""b"] },
      errorPrefix: "memory batch failed",
    });

    expect(result).toEqual({ ok: true, ids: [12] });
    expect(postJsonMock).toHaveBeenCalledWith(
      expect.objectContaining({
        url: "https://memory.example/v1/batch",
        headers: { Authorization: "Bearer test" },
        body: { chunks: ["a""b"] },
        errorPrefix: "memory batch failed",
        attachStatus: true,
      }),
    );

    const retryOptions = retryAsyncMock.mock.calls[0]?.[1] as
      | {
          attempts: number;
          minDelayMs: number;
          maxDelayMs: number;
          shouldRetry: (err: unknown) => boolean;
        }
      | undefined;
    expect(retryOptions?.attempts).toBe(3);
    expect(retryOptions?.minDelayMs).toBe(300);
    expect(retryOptions?.maxDelayMs).toBe(2000);
    expect(retryOptions?.shouldRetry({ status: 429 })).toBe(true);
    expect(retryOptions?.shouldRetry({ status: 503 })).toBe(true);
    expect(retryOptions?.shouldRetry({ status: 400 })).toBe(false);
  });

  it("attaches status to non-ok errors", async () => {
    postJsonMock.mockRejectedValueOnce(
      Object.assign(new Error("memory batch failed: 503 backend down"), { status: 503 }),
    );

    await expect(
      postJsonWithRetry({
        url: "https://memory.example/v1/batch",
        headers: {},
        body: { chunks: [] },
        errorPrefix: "memory batch failed",
      }),
    ).rejects.toMatchObject({
      message: expect.stringContaining("memory batch failed: 503 backend down"),
      status: 503,
    });
  });
});

Messung V0.5 in Prozent
C=100 H=98 G=98

¤ Dauer der Verarbeitung: 0.1 Sekunden  (vorverarbeitet am  2026-06-10) ¤

*© Formatika GbR, Deutschland






Wurzel

Suchen

PVS Prover

Isabelle Prover

NIST Cobol Testsuite

Cephes Mathematical Library

Vienna Development Method

Haftungshinweis

Die Informationen auf dieser Webseite wurden nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit, noch Qualität der bereit gestellten Informationen zugesichert.

Bemerkung:

Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.






                                                                                                                                                                                                                                                                                                                                                                                                     


Neuigkeiten

     Aktuelles
     Motto des Tages

Software

     Quellcodebibliothek
     Eigene Quellcodes
     Fremde Quellcodes
     Suchen

Aktivitäten

     Artikel über Sicherheit
     Anleitung zur Aktivierung von SSL

Muße

     Gedichte
     Musik
     Bilder

Jenseits des Üblichen ....
    

Besucherstatistik

Besucherstatistik