Skip to content
Merged
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
29 changes: 16 additions & 13 deletions packages/core/src/test/shared/sam/build.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,6 @@ describe('SAM runBuild', () => {
let mockChildProcessClass: sinon.SinonStub
let mockSamBuildChildProcess: sinon.SinonStub

let spyRunInterminal: sinon.SinonSpy

let registry: CloudFormationTemplateRegistry

// Dependency clients
Expand All @@ -296,8 +294,6 @@ describe('SAM runBuild', () => {
templateFile = vscode.Uri.file(await testFolder.write('template.yaml', validTemplateData))
await registry.addItem(templateFile)

spyRunInterminal = sandbox.spy(ProcessTerminalUtils, 'runInTerminal')

mockGetSpawnEnv = sandbox.stub(ResolveEnvModule, 'getSpawnEnv').callsFake(
sandbox.stub().resolves({
AWS_TOOLING_USER_AGENT: 'AWS-Toolkit-For-VSCode/testPluginVersion',
Expand All @@ -312,6 +308,8 @@ describe('SAM runBuild', () => {
})

describe(':) path', () => {
let spyRunInterminal: sinon.SinonSpy

beforeEach(() => {
mockGetSamCliPath = sandbox
.stub(SamUtilsModule, 'getSamCliPathAndVersion')
Expand All @@ -329,6 +327,7 @@ describe('SAM runBuild', () => {
}),
},
})
spyRunInterminal = sandbox.spy(ProcessTerminalUtils, 'runInTerminal')
mockChildProcessClass = sandbox.stub(ProcessUtilsModule, 'ChildProcess').returns(mockSamBuildChildProcess)
})

Expand All @@ -337,10 +336,11 @@ describe('SAM runBuild', () => {
})

const verifyCorrectDependencyCall = () => {
assert(mockGetSamCliPath.calledOnce)
assert(mockChildProcessClass.calledOnce)
assert(mockGetSpawnEnv.calledOnce)
assert(spyRunInterminal.calledOnce)
// Prefer count comparison for debugging flakiness
assert.strictEqual(mockGetSamCliPath.callCount, 1)
assert.strictEqual(mockChildProcessClass.callCount, 1)
assert.strictEqual(mockGetSpawnEnv.callCount, 1)
assert.strictEqual(spyRunInterminal.callCount, 1)
assert.deepEqual(spyRunInterminal.getCall(0).args, [mockSamBuildChildProcess, 'build'])
}

Expand Down Expand Up @@ -398,7 +398,8 @@ describe('SAM runBuild', () => {
.build()

// Invoke sync command from command palette
await runBuild()
// Instead of await runBuild(), prefer this to avoid flakiness due to race condition
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the race condition? Could it affect users or is it test specific?

await delayedRunBuild()

assert.deepEqual(mockChildProcessClass.getCall(0).args, [
'sam-cli-path',
Expand Down Expand Up @@ -433,7 +434,8 @@ describe('SAM runBuild', () => {
projectRoot: projectRoot,
}

await runBuild(new AppNode(expectedSamAppLocation))
// Instead of await runBuild(), prefer this to avoid flakiness due to race condition
await delayedRunBuild(expectedSamAppLocation)

getTestWindow()
.getFirstMessage()
Expand Down Expand Up @@ -509,7 +511,8 @@ describe('SAM runBuild', () => {
})
.build()

await runBuild()
// Instead of await runBuild(), prefer this to avoid flakiness due to race condition
await delayedRunBuild()

assert.deepEqual(mockChildProcessClass.getCall(0).args, [
'sam-cli-path',
Expand Down Expand Up @@ -605,14 +608,14 @@ async function runInParallel(samLocation: SamAppLocation): Promise<[SamBuildResu
}

// We add a small delay to avoid the unlikely but possible race condition.
async function delayedRunBuild(samLocation: SamAppLocation): Promise<SamBuildResult> {
async function delayedRunBuild(samLocation?: SamAppLocation): Promise<SamBuildResult> {
return new Promise(async (resolve, reject) => {
// Add a small delay before returning the build promise
setTimeout(() => {
// Do nothing, just let the delay pass
}, 20)

const buildPromise = runBuild(new AppNode(samLocation))
const buildPromise = samLocation ? runBuild(new AppNode(samLocation)) : runBuild()
buildPromise.then(resolve).catch(reject)
})
}
Expand Down
Loading