Skip to content

Commit

Permalink
feat: add throttling plugin when retries is configured
Browse files Browse the repository at this point in the history
  • Loading branch information
jBouyoud committed Feb 28, 2024
1 parent 60a0d83 commit 691bd6b
Show file tree
Hide file tree
Showing 6 changed files with 370 additions and 36 deletions.
39 changes: 29 additions & 10 deletions __test__/get-retry-options.test.ts
Expand Up @@ -4,7 +4,7 @@ import {getRetryOptions} from '../src/retry-options'

describe('getRequestOptions', () => {
test('retries disabled if retries == 0', async () => {
const [retryOptions, requestOptions] = getRetryOptions(
const [retryOptions, requestOptions, throttlingOptions] = getRetryOptions(
0,
[400, 500, 502],
[]
Expand All @@ -14,10 +14,12 @@ describe('getRequestOptions', () => {
expect(retryOptions.doNotRetry).toBeFalsy()

expect(requestOptions?.retries).toBeFalsy()
expect(throttlingOptions?.onRateLimit).toBeFalsy()
expect(throttlingOptions?.onSecondaryRateLimit).toBeFalsy()
})

test('properties set if retries > 0', async () => {
const [retryOptions, requestOptions] = getRetryOptions(
const [retryOptions, requestOptions, throttlingOptions] = getRetryOptions(
1,
[400, 500, 502],
[]
Expand All @@ -27,10 +29,12 @@ describe('getRequestOptions', () => {
expect(retryOptions.doNotRetry).toEqual([400, 500, 502])

expect(requestOptions?.retries).toEqual(1)
expect(throttlingOptions?.onRateLimit).toBeDefined()
expect(throttlingOptions?.onSecondaryRateLimit).toBeDefined()
})

test('properties set if retries > 0', async () => {
const [retryOptions, requestOptions] = getRetryOptions(
const [retryOptions, requestOptions, throttlingOptions] = getRetryOptions(
1,
[400, 500, 502],
[]
Expand All @@ -40,30 +44,45 @@ describe('getRequestOptions', () => {
expect(retryOptions.doNotRetry).toEqual([400, 500, 502])

expect(requestOptions?.retries).toEqual(1)
expect(throttlingOptions?.onRateLimit).toBeDefined()
expect(throttlingOptions?.onSecondaryRateLimit).toBeDefined()
})

test('retryOptions.doNotRetry not set if exemptStatusCodes isEmpty', async () => {
const [retryOptions, requestOptions] = getRetryOptions(1, [], [])
const [retryOptions, requestOptions, throttlingOptions] = getRetryOptions(
1,
[],
[]
)

expect(retryOptions.enabled).toBe(true)
expect(retryOptions.doNotRetry).toBeUndefined()

expect(requestOptions?.retries).toEqual(1)
expect(throttlingOptions?.onRateLimit).toBeDefined()
expect(throttlingOptions?.onSecondaryRateLimit).toBeDefined()
})

test('requestOptions does not override defaults from @actions/github', async () => {
const [retryOptions, requestOptions] = getRetryOptions(1, [], {
request: {
agent: 'default-user-agent'
},
foo: 'bar'
})
const [retryOptions, requestOptions, throttlingOptions] = getRetryOptions(
1,
[],
{
request: {
agent: 'default-user-agent'
},
foo: 'bar'
}
)

expect(retryOptions.enabled).toBe(true)
expect(retryOptions.doNotRetry).toBeUndefined()

expect(requestOptions?.retries).toEqual(1)
expect(requestOptions?.agent).toEqual('default-user-agent')
expect(requestOptions?.foo).toBeUndefined() // this should not be in the `options.request` object, but at the same level as `request`

expect(throttlingOptions?.onRateLimit).toBeDefined()
expect(throttlingOptions?.onSecondaryRateLimit).toBeDefined()
})
})

0 comments on commit 691bd6b

Please sign in to comment.