Skip to content

Commit

Permalink
fix: Wait for asset discovery finish when awaiting on snapshot resour…
Browse files Browse the repository at this point in the history
…ces in finalize (#423)

* test: Add failing test for snapshots being added after finalize

* fix: Wait for asset discovery finish when awaiting on snapshots

* test: Clean up test file imports

* fix integration tests

* Improve tests

* Code review fixes
  • Loading branch information
Robdel12 authored Nov 22, 2019
1 parent f805d61 commit 4a6deea
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 6 deletions.
9 changes: 6 additions & 3 deletions src/services/agent-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ export class AgentService {
logger.info(`snapshot skipped[max_file_size_exceeded]: '${request.body.name}'`)
return response.json({ success: true })
}
// tslint:disable-next-line
let resolve, reject
const deferred = new Promise((...args) => [resolve, reject] = args)
this.snapshotCreationPromises.push(deferred)

let resources = await this.snapshotService.buildResources(
rootURL,
Expand All @@ -140,15 +144,14 @@ export class AgentService {
this.snapshotService.buildLogResource(snapshotLog),
)

const snapshotCreation = this.snapshotService.create(
this.snapshotService.create(
request.body.name,
resources,
snapshotOptions,
request.body.clientInfo,
request.body.environmentInfo,
)
).then(resolve).catch(reject)

this.snapshotCreationPromises.push(snapshotCreation)
logger.info(`snapshot taken: '${request.body.name}'`)

profile('agentService.handleSnapshot')
Expand Down
2 changes: 1 addition & 1 deletion src/services/resource-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export default class ResourceService extends PercyClientService {
snapshotResponse.response,
snapshotResponse.resources,
)
profile('-> resourceService.uploadMissingResources', {resources: resources.length})
profile(`-> resourceService.uploadMissingResources ${resources.length}`)

return true
} catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion src/services/snapshot-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export default class SnapshotService extends PercyClientService {

profile('-> snapshotService.finalizeSnapshot')
await this.finalize(response.body.data.id)
profile('-> snapshotService.finalizeSnapshot', {snapshotId})
profile(`-> snapshotService.finalizeSnapshot ${snapshotId}`)
return response
}).catch(logError)

Expand Down
27 changes: 27 additions & 0 deletions test/acceptance/asset-discovery.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import expect from 'expect'
import {
run,
setupApiProxy,
setupDummyApp
} from './helpers'

describe('Asset discovery', () => {
let proxy = setupApiProxy()
let dummy = setupDummyApp()

it('waits for snapshot creation before build finalization', async () => {
let [stdout] = await run('percy exec -- node ./test/acceptance/dummy/snapshot-error.js');
let finalizeReqDate = proxy.requests['/builds/123/finalize'][0].timestamp;
let snapshotReqs = proxy.requests['/builds/123/snapshots'];
let lastSnapshotReqDate = snapshotReqs[snapshotReqs.length - 1].timestamp;

expect(finalizeReqDate).toBeGreaterThan(lastSnapshotReqDate)
expect(stdout).toHaveEntries([
'[percy] percy has started.',
/^\[percy\] waiting for \d+ snapshots to complete\.\.\.$/m,
"[percy] snapshot taken: 'Home Page - 0'",
'[percy] done.',
'[percy] finalized build #4: <<build-url>>'
])
})
})
17 changes: 17 additions & 0 deletions test/acceptance/dummy/snapshot-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// register babel for the snapshot helper
require('@babel/register')
require('regenerator-runtime/runtime')

const launch = require('../helpers/snapshot').default

launch(async (page, snapshot) => {
await page.goto('http://localhost:9999')

setTimeout(() => {
throw new Error('Surprise!')
}, 100) // magic number

for (let index = 0; index < 10; index++) {
await snapshot(`Home Page - ${index}`)
}
})
2 changes: 1 addition & 1 deletion test/acceptance/helpers/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export function createApiProxy() {
app.all('*', (req, res, next) => {
let path = req.path.replace(/\/$/, '')
requests[path] = requests[path] || []
requests[path].push(req)
requests[path].push({ timestamp: Date.now(), ...req })
next()
})

Expand Down

0 comments on commit 4a6deea

Please sign in to comment.