Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add throttling plugin when retries is configured #456

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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()
})
})