Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion packages/cache/__tests__/downloadUtils.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
import * as core from '@actions/core'
import {DownloadProgress} from '../src/internal/downloadUtils'
import * as fs from 'fs'
import * as path from 'path'
import {
DownloadProgress,
downloadCacheStorageSDK
} from '../src/internal/downloadUtils'

const mockGetProperties = jest.fn()
const mockDownloadToBuffer = jest.fn()

jest.mock('@azure/storage-blob', () => ({
BlockBlobClient: jest.fn().mockImplementation(() => ({
getProperties: mockGetProperties,
downloadToBuffer: mockDownloadToBuffer
}))
}))

test('download progress tracked correctly', () => {
const progress = new DownloadProgress(1000)
Expand Down Expand Up @@ -158,3 +173,25 @@ test('display does not print completed line twice', () => {
expect(progress.displayedComplete).toBe(true)
expect(infoMock).toHaveBeenCalledTimes(3)
})

test('storage download clears segment timeout when download rejects', async () => {
jest.useFakeTimers()
const archivePath = path.join(__dirname, '_temp', 'rejected-download')
fs.mkdirSync(path.dirname(archivePath), {recursive: true})
mockGetProperties.mockResolvedValue({contentLength: 1})
mockDownloadToBuffer.mockRejectedValue(new Error('download failed'))

try {
await expect(
downloadCacheStorageSDK('https://example.test/cache', archivePath, {
segmentTimeoutInMs: 600000
})
).rejects.toThrow('download failed')

expect(jest.getTimerCount()).toBe(0)
} finally {
jest.clearAllTimers()
jest.useRealTimers()
fs.rmSync(archivePath, {force: true})
}
})
3 changes: 1 addition & 2 deletions packages/cache/src/internal/downloadUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,8 +454,7 @@ const promiseWithTimeout = async <T>(
timeoutHandle = setTimeout(() => resolve('timeout'), timeoutMs)
})

return Promise.race([promise, timeoutPromise]).then(result => {
return Promise.race([promise, timeoutPromise]).finally(() => {
clearTimeout(timeoutHandle)
return result
})
}