Skip to content

Commit deda0c8

Browse files
[ci-visibility] Add gatherPayloadsMaxTimeout, a faster version of gatherPayloads (#2957)
1 parent bb61b06 commit deda0c8

File tree

3 files changed

+41
-12
lines changed

3 files changed

+41
-12
lines changed

integration-tests/ci-visibility-intake.js

+33
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,39 @@ class FakeCiVisIntake extends FakeAgent {
169169
infoResponse = DEFAULT_INFO_RESPONSE
170170
}
171171

172+
// Similar to gatherPayloads but resolves if enough payloads have been gathered
173+
// to make the assertions pass. It times out after maxGatheringTime so it should
174+
// always be faster or as fast as gatherPayloads
175+
gatherPayloadsMaxTimeout (payloadMatch, onPayload, maxGatheringTime = 15000) {
176+
const payloads = []
177+
return new Promise((resolve, reject) => {
178+
const timeoutId = setTimeout(() => {
179+
try {
180+
onPayload(payloads)
181+
resolve()
182+
} catch (e) {
183+
reject(e)
184+
} finally {
185+
this.off('message', messageHandler)
186+
}
187+
}, maxGatheringTime)
188+
const messageHandler = (message) => {
189+
if (!payloadMatch || payloadMatch(message)) {
190+
payloads.push(message)
191+
try {
192+
onPayload(payloads)
193+
clearTimeout(timeoutId)
194+
this.off('message', messageHandler)
195+
resolve()
196+
} catch (e) {
197+
// we'll try again when a new payload arrives
198+
}
199+
}
200+
}
201+
this.on('message', messageHandler)
202+
})
203+
}
204+
172205
gatherPayloads (payloadMatch, gatheringTime = 15000) {
173206
const payloads = []
174207
return new Promise((resolve, reject) => {

integration-tests/cypress/cypress.spec.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ versions.forEach((version) => {
6161
? getCiVisAgentlessConfig(receiver.port) : getCiVisEvpProxyConfig(receiver.port)
6262
const reportUrl = reportMethod === 'agentless' ? '/api/v2/citestcycle' : '/evp_proxy/v2/api/v2/citestcycle'
6363

64-
receiver.gatherPayloads(({ url }) => url === reportUrl, 25000).then((payloads) => {
64+
receiver.gatherPayloadsMaxTimeout(({ url }) => url === reportUrl, payloads => {
6565
const events = payloads.flatMap(({ payload }) => payload.events)
6666

6767
const testSessionEvent = events.find(event => event.type === 'test_session_end')
@@ -141,9 +141,7 @@ versions.forEach((version) => {
141141
assert.equal(testModuleId.toString(10), testModuleEventContent.test_module_id.toString(10))
142142
assert.equal(testSessionId.toString(10), testSessionEventContent.test_session_id.toString(10))
143143
})
144-
145-
done()
146-
}).catch(done)
144+
}, 25000).then(() => done()).catch(done)
147145

148146
const {
149147
NODE_OPTIONS, // NODE_OPTIONS dd-trace config does not work with cypress
@@ -160,7 +158,7 @@ versions.forEach((version) => {
160158
...restEnvVars,
161159
CYPRESS_BASE_URL: `http://localhost:${webAppPort}`
162160
},
163-
stdio: 'on'
161+
stdio: 'pipe'
164162
}
165163
)
166164
})

integration-tests/playwright/playwright.spec.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ versions.forEach((version) => {
5858
? getCiVisAgentlessConfig(receiver.port) : getCiVisEvpProxyConfig(receiver.port)
5959
const reportUrl = reportMethod === 'agentless' ? '/api/v2/citestcycle' : '/evp_proxy/v2/api/v2/citestcycle'
6060

61-
receiver.gatherPayloads(({ url }) => url === reportUrl).then((payloads) => {
61+
receiver.gatherPayloadsMaxTimeout(({ url }) => url === reportUrl, payloads => {
6262
const events = payloads.flatMap(({ payload }) => payload.events)
6363

6464
const testSessionEvent = events.find(event => event.type === 'test_session_end')
@@ -99,9 +99,7 @@ versions.forEach((version) => {
9999
assert.equal(stepEvent.content.name, 'playwright.step')
100100
assert.property(stepEvent.content.meta, 'playwright.step')
101101
})
102-
103-
done()
104-
}).catch(done)
102+
}).then(() => done()).catch(done)
105103

106104
childProcess = exec(
107105
'./node_modules/.bin/playwright test -c playwright.config.js',
@@ -119,7 +117,8 @@ versions.forEach((version) => {
119117
})
120118
it('works when tests are compiled to a different location', (done) => {
121119
let testOutput = ''
122-
receiver.gatherPayloads(({ url }) => url === '/api/v2/citestcycle').then((payloads) => {
120+
121+
receiver.gatherPayloadsMaxTimeout(({ url }) => url === '/api/v2/citestcycle', payloads => {
123122
const events = payloads.flatMap(({ payload }) => payload.events)
124123
const testEvents = events.filter(event => event.type === 'test')
125124
assert.includeMembers(testEvents.map(test => test.content.resource), [
@@ -129,8 +128,7 @@ versions.forEach((version) => {
129128
assert.include(testOutput, '1 passed')
130129
assert.include(testOutput, '1 skipped')
131130
assert.notInclude(testOutput, 'TypeError')
132-
done()
133-
}).catch(done)
131+
}).then(() => done()).catch(done)
134132

135133
childProcess = exec(
136134
'node ./node_modules/typescript/bin/tsc' +

0 commit comments

Comments
 (0)