|
| 1 | +import { cbor } from "@smithy/core/cbor"; |
| 2 | +import { HttpResponse } from "@smithy/protocol-http"; |
| 3 | +import { requireRequestsFrom } from "@smithy/util-test/src"; |
| 4 | +import { Readable } from "node:stream"; |
| 5 | +import { describe, expect, test as it } from "vitest"; |
| 6 | +import { XYZService } from "xyz"; |
| 7 | + |
| 8 | +describe("retries", () => { |
| 9 | + function createCborResponse(body: any, status = 200) { |
| 10 | + const bytes = cbor.serialize(body); |
| 11 | + return new HttpResponse({ |
| 12 | + headers: { |
| 13 | + "smithy-protocol": "rpc-v2-cbor", |
| 14 | + }, |
| 15 | + body: Readable.from(bytes), |
| 16 | + statusCode: status, |
| 17 | + }); |
| 18 | + } |
| 19 | + |
| 20 | + it("should retry throttling and transient-error status codes", async () => { |
| 21 | + const client = new XYZService({ |
| 22 | + endpoint: "https://localhost/nowhere", |
| 23 | + }); |
| 24 | + |
| 25 | + requireRequestsFrom(client) |
| 26 | + .toMatch({ |
| 27 | + hostname: /localhost/, |
| 28 | + }) |
| 29 | + .respondWith( |
| 30 | + createCborResponse( |
| 31 | + { |
| 32 | + __type: "HaltError", |
| 33 | + }, |
| 34 | + 429 |
| 35 | + ), |
| 36 | + createCborResponse( |
| 37 | + { |
| 38 | + __type: "HaltError", |
| 39 | + }, |
| 40 | + 500 |
| 41 | + ), |
| 42 | + createCborResponse("", 200) |
| 43 | + ); |
| 44 | + |
| 45 | + const response = await client.getNumbers().catch((e) => e); |
| 46 | + |
| 47 | + expect(response.$metadata.attempts).toEqual(3); |
| 48 | + }); |
| 49 | + |
| 50 | + it("should retry when a retryable trait is modeled", async () => { |
| 51 | + const client = new XYZService({ |
| 52 | + endpoint: "https://localhost/nowhere", |
| 53 | + }); |
| 54 | + |
| 55 | + requireRequestsFrom(client) |
| 56 | + .toMatch({ |
| 57 | + hostname: /localhost/, |
| 58 | + }) |
| 59 | + .respondWith( |
| 60 | + createCborResponse( |
| 61 | + { |
| 62 | + __type: "RetryableError", |
| 63 | + }, |
| 64 | + 400 // not retryable status code |
| 65 | + ), |
| 66 | + createCborResponse( |
| 67 | + { |
| 68 | + __type: "RetryableError", |
| 69 | + }, |
| 70 | + 400 // not retryable status code |
| 71 | + ), |
| 72 | + createCborResponse("", 200) |
| 73 | + ); |
| 74 | + |
| 75 | + const response = await client.getNumbers().catch((e) => e); |
| 76 | + |
| 77 | + expect(response.$metadata.attempts).toEqual(3); |
| 78 | + }); |
| 79 | + |
| 80 | + it("should retry retryable trait with throttling", async () => { |
| 81 | + const client = new XYZService({ |
| 82 | + endpoint: "https://localhost/nowhere", |
| 83 | + }); |
| 84 | + |
| 85 | + requireRequestsFrom(client) |
| 86 | + .toMatch({ |
| 87 | + hostname: /localhost/, |
| 88 | + }) |
| 89 | + .respondWith( |
| 90 | + createCborResponse( |
| 91 | + { |
| 92 | + __type: "CodedThrottlingError", |
| 93 | + }, |
| 94 | + 429 |
| 95 | + ), |
| 96 | + createCborResponse( |
| 97 | + { |
| 98 | + __type: "MysteryThrottlingError", |
| 99 | + }, |
| 100 | + 400 // not a retryable status code, but error is modeled as retryable. |
| 101 | + ), |
| 102 | + createCborResponse("", 200) |
| 103 | + ); |
| 104 | + |
| 105 | + const response = await client.getNumbers().catch((e) => e); |
| 106 | + |
| 107 | + expect(response.$metadata.attempts).toEqual(3); |
| 108 | + }); |
| 109 | + |
| 110 | + it("should not retry if the error is not modeled with retryable trait and is not otherwise retryable", async () => { |
| 111 | + const client = new XYZService({ |
| 112 | + endpoint: "https://localhost/nowhere", |
| 113 | + }); |
| 114 | + |
| 115 | + requireRequestsFrom(client) |
| 116 | + .toMatch({ |
| 117 | + hostname: /localhost/, |
| 118 | + }) |
| 119 | + .respondWith( |
| 120 | + createCborResponse( |
| 121 | + { |
| 122 | + __type: "HaltError", |
| 123 | + }, |
| 124 | + 429 // not modeled as retryable, but this is a retryable status code. |
| 125 | + ), |
| 126 | + createCborResponse( |
| 127 | + { |
| 128 | + __type: "HaltError", |
| 129 | + }, |
| 130 | + 400 |
| 131 | + ), |
| 132 | + createCborResponse("", 200) |
| 133 | + ); |
| 134 | + |
| 135 | + const response = await client.getNumbers().catch((e) => e); |
| 136 | + |
| 137 | + // stopped at the second error. |
| 138 | + expect(response.$metadata.attempts).toEqual(2); |
| 139 | + }); |
| 140 | +}); |
0 commit comments