Skip to content

Commit

Permalink
Merge pull request actions#513 from dhadka/dhadka/download-progress
Browse files Browse the repository at this point in the history
Display download progress when using Azure Storage SDK
  • Loading branch information
aiqiaoy authored Jul 14, 2020
2 parents 4964b0c + c4a92b0 commit 8fdeff4
Show file tree
Hide file tree
Showing 6 changed files with 400 additions and 13 deletions.
1 change: 1 addition & 0 deletions packages/cache/RELEASES.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@

### 1.0.0
- Downloads Azure-hosted caches using the Azure SDK for speed and reliability
- Displays download progress
- Includes changes that break compatibility with earlier versions, including:
- `retry`, `retryTypedResponse`, and `retryHttpClientResponse` moved from `cacheHttpClient` to `requestUtils`
159 changes: 159 additions & 0 deletions packages/cache/__tests__/downloadUtils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
import * as core from '@actions/core'
import {DownloadProgress} from '../src/internal/downloadUtils'

test('download progress tracked correctly', () => {
const progress = new DownloadProgress(1000)

expect(progress.contentLength).toBe(1000)
expect(progress.receivedBytes).toBe(0)
expect(progress.segmentIndex).toBe(0)
expect(progress.segmentOffset).toBe(0)
expect(progress.segmentSize).toBe(0)
expect(progress.displayedComplete).toBe(false)
expect(progress.timeoutHandle).toBeUndefined()
expect(progress.getTransferredBytes()).toBe(0)
expect(progress.isDone()).toBe(false)

progress.nextSegment(500)

expect(progress.contentLength).toBe(1000)
expect(progress.receivedBytes).toBe(0)
expect(progress.segmentIndex).toBe(1)
expect(progress.segmentOffset).toBe(0)
expect(progress.segmentSize).toBe(500)
expect(progress.displayedComplete).toBe(false)
expect(progress.timeoutHandle).toBeUndefined()
expect(progress.getTransferredBytes()).toBe(0)
expect(progress.isDone()).toBe(false)

progress.setReceivedBytes(250)

expect(progress.contentLength).toBe(1000)
expect(progress.receivedBytes).toBe(250)
expect(progress.segmentIndex).toBe(1)
expect(progress.segmentOffset).toBe(0)
expect(progress.segmentSize).toBe(500)
expect(progress.displayedComplete).toBe(false)
expect(progress.timeoutHandle).toBeUndefined()
expect(progress.getTransferredBytes()).toBe(250)
expect(progress.isDone()).toBe(false)

progress.setReceivedBytes(500)

expect(progress.contentLength).toBe(1000)
expect(progress.receivedBytes).toBe(500)
expect(progress.segmentIndex).toBe(1)
expect(progress.segmentOffset).toBe(0)
expect(progress.segmentSize).toBe(500)
expect(progress.displayedComplete).toBe(false)
expect(progress.timeoutHandle).toBeUndefined()
expect(progress.getTransferredBytes()).toBe(500)
expect(progress.isDone()).toBe(false)

progress.nextSegment(500)

expect(progress.contentLength).toBe(1000)
expect(progress.receivedBytes).toBe(0)
expect(progress.segmentIndex).toBe(2)
expect(progress.segmentOffset).toBe(500)
expect(progress.segmentSize).toBe(500)
expect(progress.displayedComplete).toBe(false)
expect(progress.timeoutHandle).toBeUndefined()
expect(progress.getTransferredBytes()).toBe(500)
expect(progress.isDone()).toBe(false)

progress.setReceivedBytes(250)

expect(progress.contentLength).toBe(1000)
expect(progress.receivedBytes).toBe(250)
expect(progress.segmentIndex).toBe(2)
expect(progress.segmentOffset).toBe(500)
expect(progress.segmentSize).toBe(500)
expect(progress.displayedComplete).toBe(false)
expect(progress.timeoutHandle).toBeUndefined()
expect(progress.getTransferredBytes()).toBe(750)
expect(progress.isDone()).toBe(false)

progress.setReceivedBytes(500)

expect(progress.contentLength).toBe(1000)
expect(progress.receivedBytes).toBe(500)
expect(progress.segmentIndex).toBe(2)
expect(progress.segmentOffset).toBe(500)
expect(progress.segmentSize).toBe(500)
expect(progress.displayedComplete).toBe(false)
expect(progress.timeoutHandle).toBeUndefined()
expect(progress.getTransferredBytes()).toBe(1000)
expect(progress.isDone()).toBe(true)
})

test('display timer works correctly', () => {
const progress = new DownloadProgress(1000)

const infoMock = jest.spyOn(core, 'info')
infoMock.mockImplementation(() => {})

const check = (): void => {
expect(infoMock).toHaveBeenLastCalledWith(
expect.stringContaining('Received 500 of 1000')
)
}

// Validate no further updates are displayed after stopping the timer.
const test2 = (): void => {
check()
expect(progress.timeoutHandle).toBeUndefined()
}

// Validate the progress is displayed, stop the timer, and call test2.
const test1 = (): void => {
check()

progress.stopDisplayTimer()
progress.setReceivedBytes(1000)

setTimeout(() => test2(), 100)
}

// Start the timer, update the received bytes, and call test1.
const start = (): void => {
progress.startDisplayTimer(10)
expect(progress.timeoutHandle).toBeDefined()

progress.setReceivedBytes(500)

setTimeout(() => test1(), 100)
}

start()
})

test('display does not print completed line twice', () => {
const progress = new DownloadProgress(1000)

const infoMock = jest.spyOn(core, 'info')
infoMock.mockImplementation(() => {})

progress.display()

expect(progress.displayedComplete).toBe(false)
expect(infoMock).toHaveBeenCalledTimes(1)

progress.nextSegment(1000)
progress.setReceivedBytes(500)
progress.display()

expect(progress.displayedComplete).toBe(false)
expect(infoMock).toHaveBeenCalledTimes(2)

progress.setReceivedBytes(1000)
progress.display()

expect(progress.displayedComplete).toBe(true)
expect(infoMock).toHaveBeenCalledTimes(3)

progress.display()

expect(progress.displayedComplete).toBe(true)
expect(infoMock).toHaveBeenCalledTimes(3)
})
86 changes: 85 additions & 1 deletion packages/cache/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/cache/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"@actions/glob": "^0.1.0",
"@actions/http-client": "^1.0.8",
"@actions/io": "^1.0.1",
"@azure/ms-rest-js": "^2.0.7",
"@azure/storage-blob": "^12.1.2",
"semver": "^6.1.0",
"uuid": "^3.3.3"
Expand Down
Loading

0 comments on commit 8fdeff4

Please sign in to comment.