|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT license. |
| 3 | + |
| 4 | +const chai = require("chai"); |
| 5 | +const chaiAsPromised = require("chai-as-promised"); |
| 6 | +chai.use(chaiAsPromised); |
| 7 | +const expect = chai.expect; |
| 8 | +const { load } = require("../dist/index"); |
| 9 | +const { createMockedConnectionString } = require("./utils/testHelper"); |
| 10 | +const nock = require('nock'); |
| 11 | + |
| 12 | +class HttpRequestCountPolicy { |
| 13 | + constructor() { |
| 14 | + this.count = 0; |
| 15 | + this.name = "HttpRequestCountPolicy"; |
| 16 | + } |
| 17 | + sendRequest(req, next) { |
| 18 | + this.count++; |
| 19 | + return next(req).then(resp => { resp.status = 500; return resp; }); |
| 20 | + } |
| 21 | + resetCount() { |
| 22 | + this.count = 0; |
| 23 | + } |
| 24 | +}; |
| 25 | + |
| 26 | +describe("custom client options", function () { |
| 27 | + const fakeEndpoint = "https://azure.azconfig.io"; |
| 28 | + before(() => { |
| 29 | + // Thus here mock it to reply 500, in which case the retry mechanism works. |
| 30 | + nock(fakeEndpoint).persist().get(() => true).reply(500); |
| 31 | + }); |
| 32 | + |
| 33 | + after(() => { |
| 34 | + nock.restore(); |
| 35 | + }) |
| 36 | + |
| 37 | + it("should retry 2 times by default", async () => { |
| 38 | + const countPolicy = new HttpRequestCountPolicy(); |
| 39 | + const loadPromise = () => { |
| 40 | + return load(createMockedConnectionString(fakeEndpoint), { |
| 41 | + clientOptions: { |
| 42 | + additionalPolicies: [{ |
| 43 | + policy: countPolicy, |
| 44 | + position: "perRetry" |
| 45 | + }] |
| 46 | + } |
| 47 | + }) |
| 48 | + }; |
| 49 | + let error; |
| 50 | + try { |
| 51 | + await loadPromise(); |
| 52 | + } catch (e) { |
| 53 | + error = e; |
| 54 | + } |
| 55 | + expect(error).not.undefined; |
| 56 | + expect(countPolicy.count).eq(3); |
| 57 | + }); |
| 58 | + |
| 59 | + it("should override default retry options", async () => { |
| 60 | + const countPolicy = new HttpRequestCountPolicy(); |
| 61 | + const loadWithMaxRetries = (maxRetries) => { |
| 62 | + return load(createMockedConnectionString(fakeEndpoint), { |
| 63 | + clientOptions: { |
| 64 | + additionalPolicies: [{ |
| 65 | + policy: countPolicy, |
| 66 | + position: "perRetry" |
| 67 | + }], |
| 68 | + retryOptions: { |
| 69 | + maxRetries |
| 70 | + } |
| 71 | + } |
| 72 | + }) |
| 73 | + }; |
| 74 | + |
| 75 | + let error; |
| 76 | + try { |
| 77 | + error = undefined; |
| 78 | + await loadWithMaxRetries(0); |
| 79 | + } catch (e) { |
| 80 | + error = e; |
| 81 | + } |
| 82 | + expect(error).not.undefined; |
| 83 | + expect(countPolicy.count).eq(1); |
| 84 | + |
| 85 | + countPolicy.resetCount(); |
| 86 | + try { |
| 87 | + error = undefined; |
| 88 | + await loadWithMaxRetries(1); |
| 89 | + } catch (e) { |
| 90 | + error = e; |
| 91 | + } |
| 92 | + expect(error).not.undefined; |
| 93 | + expect(countPolicy.count).eq(2); |
| 94 | + }); |
| 95 | + |
| 96 | + // Note: |
| 97 | + // core-rest-pipeline skips the retry throwing `RestError: getaddrinfo ENOTFOUND azure.azconfig.io` |
| 98 | + // Probably would be fixed in upstream libs. |
| 99 | + // See https://github.com/Azure/azure-sdk-for-js/issues/27037 |
| 100 | + it("should retry on DNS failure"); |
| 101 | +}) |
0 commit comments