Skip to content

Commit f406cc5

Browse files
Attempting to increase reliability of some Pester tests (#265)
We're seeing some inconsistent failures in some of the Pester tests. The hypothesis is that GitHub may need a little bit more time after the creation of objects before performing certain operations on them (like renaming repos), or may need more time after deleting them before it will successfully return a 404 on a successive Get request. I have added a number of `Start-Sleep`'s throughout the test codebase wherever we've encountered inconsistent failures, and that appears to have resolved the problem. We may need to continue to do more of these if additional ones pop up. The duration of the sleep itself is controlled by `$script:defaultSleepSecondsForReliability` which is defined in `Tests/Common.ps1`. Long term, I have opened #267 which poses the idea of switching over to mocking out `Invoke-WebRequest` for the majority of the tests, and instead focus on validating the data that it's sending matches the expected values per the API documentation, and having just a limited number of tests that do actual end-to-end testing. Fixes #264
1 parent 8e55f5a commit f406cc5

8 files changed

+251
-6
lines changed

Tests/Common.ps1

+4
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ function Initialize-CommonTestSetup
100100
Set-GitHubConfiguration -LogRequestBody # Make it easier to debug UT failures
101101
Set-GitHubConfiguration -MultiRequestProgressThreshold 0 # Status corrupts the raw CI logs for Linux and Mac, and makes runs take slightly longer.
102102
Set-GitHubConfiguration -DisableUpdateCheck # The update check is unnecessary during tests.
103+
104+
# The number of seconds to sleep after performing some operations to ensure that successive
105+
# API calls properly reflect previously updated state.
106+
$script:defaultSleepSecondsForReliability = 3
103107
}
104108

105109
Initialize-CommonTestSetup

Tests/GitHubAssignees.tests.ps1

+41
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,22 @@ try
107107

108108
Context 'Adding and removing an assignee via parameters' {
109109
$issue = $repo | New-GitHubIssue -Title "Test issue"
110+
111+
# The CI build has been unreliable with this test.
112+
# Adding a short sleep to ensure successive queries reflect updated state.
113+
Start-Sleep -Seconds $script:defaultSleepSecondsForReliability
114+
110115
It 'Should have no assignees when created' {
111116
$issue.assignee.login | Should -BeNullOrEmpty
112117
$issue.assignees | Should -BeNullOrEmpty
113118
}
114119

115120
$updatedIssue = Add-GitHubAssignee -OwnerName $script:ownerName -RepositoryName $repo.name -Issue $issue.number -Assignee $owner.login
121+
122+
# The CI build has been unreliable with this test.
123+
# Adding a short sleep to ensure successive queries reflect updated state.
124+
Start-Sleep -Seconds $script:defaultSleepSecondsForReliability
125+
116126
It 'Should have returned the same issue' {
117127
$updatedIssue.number | Should -Be $issue.number
118128
}
@@ -144,12 +154,22 @@ try
144154

145155
Context 'Adding an assignee with the repo on the pipeline' {
146156
$issue = $repo | New-GitHubIssue -Title "Test issue"
157+
158+
# The CI build has been unreliable with this test.
159+
# Adding a short sleep to ensure successive queries reflect updated state.
160+
Start-Sleep -Seconds $script:defaultSleepSecondsForReliability
161+
147162
It 'Should have no assignees when created' {
148163
$issue.assignee.login | Should -BeNullOrEmpty
149164
$issue.assignees | Should -BeNullOrEmpty
150165
}
151166

152167
$updatedIssue = $repo | Add-GitHubAssignee -Issue $issue.number -Assignee $owner.login
168+
169+
# The CI build has been unreliable with this test.
170+
# Adding a short sleep to ensure successive queries reflect updated state.
171+
Start-Sleep -Seconds $script:defaultSleepSecondsForReliability
172+
153173
It 'Should have returned the same issue' {
154174
$updatedIssue.number | Should -Be $issue.number
155175
}
@@ -181,12 +201,22 @@ try
181201

182202
Context 'Adding an assignee with the issue on the pipeline' {
183203
$issue = $repo | New-GitHubIssue -Title "Test issue"
204+
205+
# The CI build has been unreliable with this test.
206+
# Adding a short sleep to ensure successive queries reflect updated state.
207+
Start-Sleep -Seconds $script:defaultSleepSecondsForReliability
208+
184209
It 'Should have no assignees when created' {
185210
$issue.assignee.login | Should -BeNullOrEmpty
186211
$issue.assignees | Should -BeNullOrEmpty
187212
}
188213

189214
$updatedIssue = $issue | Add-GitHubAssignee -Assignee $owner.login
215+
216+
# The CI build has been unreliable with this test.
217+
# Adding a short sleep to ensure successive queries reflect updated state.
218+
Start-Sleep -Seconds $script:defaultSleepSecondsForReliability
219+
190220
It 'Should have returned the same issue' {
191221
$updatedIssue.number | Should -Be $issue.number
192222
}
@@ -218,12 +248,22 @@ try
218248

219249
Context 'Adding an assignee with the assignee user object on the pipeline' {
220250
$issue = $repo | New-GitHubIssue -Title "Test issue"
251+
252+
# The CI build has been unreliable with this test.
253+
# Adding a short sleep to ensure successive queries reflect updated state.
254+
Start-Sleep -Seconds $script:defaultSleepSecondsForReliability
255+
221256
It 'Should have no assignees when created' {
222257
$issue.assignee.login | Should -BeNullOrEmpty
223258
$issue.assignees | Should -BeNullOrEmpty
224259
}
225260

226261
$updatedIssue = $owner | Add-GitHubAssignee -OwnerName $script:ownerName -RepositoryName $repo.name -Issue $issue.number
262+
263+
# The CI build has been unreliable with this test.
264+
# Adding a short sleep to ensure successive queries reflect updated state.
265+
Start-Sleep -Seconds $script:defaultSleepSecondsForReliability
266+
227267
It 'Should have returned the same issue' {
228268
$updatedIssue.number | Should -Be $issue.number
229269
}
@@ -239,6 +279,7 @@ try
239279
}
240280

241281
$updatedIssue = $owner | Remove-GitHubAssignee -OwnerName $script:ownerName -RepositoryName $repo.name -Issue $issue.number -Force
282+
242283
It 'Should have returned the same issue' {
243284
$updatedIssue.number | Should -Be $issue.number
244285
}

Tests/GitHubContents.tests.ps1

+8
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ try
3737
BeforeAll {
3838
# AutoInit will create a readme with the GUID of the repo name
3939
$repo = New-GitHubRepository -RepositoryName ($repoGuid) -AutoInit
40+
41+
# The CI build has been unreliable with this test.
42+
# Adding a short sleep to ensure successive queries reflect updated state.
43+
Start-Sleep -Seconds $script:defaultSleepSecondsForReliability
4044
}
4145

4246
AfterAll {
@@ -260,6 +264,10 @@ try
260264
$repoName = [Guid]::NewGuid().Guid
261265

262266
$repo = New-GitHubRepository -RepositoryName $repoName -AutoInit
267+
268+
# The CI build has been unreliable with this test.
269+
# Adding a short sleep to ensure successive queries reflect updated state.
270+
Start-Sleep -Seconds $script:defaultSleepSecondsForReliability
263271
}
264272

265273
Context 'When setting new file content' {

Tests/GitHubIssues.tests.ps1

+8-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,9 @@ try
161161
for ($i = 0; $i -lt 4; $i++)
162162
{
163163
$newIssues += New-GitHubIssue -OwnerName $script:ownerName -RepositoryName $repo.name -Title ([Guid]::NewGuid().Guid)
164-
Start-Sleep -Seconds 1 # Needed to ensure that there is a unique creation timestamp between issues
164+
165+
# Needed to ensure that there is a unique creation timestamp between issues
166+
Start-Sleep -Seconds $script:defaultSleepSecondsForReliability
165167
}
166168

167169
$newIssues[0] = Set-GitHubIssue -OwnerName $script:ownerName -RepositoryName $repo.name -Issue $newIssues[0].number -State Closed
@@ -547,6 +549,11 @@ try
547549
}
548550

549551
Lock-GitHubIssue -OwnerName $script:OwnerName -RepositoryName $repo.name -Issue $issue.number
552+
553+
# The CI build has been unreliable with this test.
554+
# Adding a short sleep to ensure successive queries reflect updated state.
555+
Start-Sleep -Seconds $script:defaultSleepSecondsForReliability
556+
550557
$timeline = @(Get-GitHubIssueTimeline -OwnerName $script:OwnerName -RepositoryName $repo.name -Issue $issue.number)
551558
It 'Should have an event now' {
552559
$timeline.Count | Should -Be 1

Tests/GitHubLabels.tests.ps1

+65
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ try
8181
$repositoryName = [Guid]::NewGuid().Guid
8282
$repo = New-GitHubRepository -RepositoryName $repositoryName
8383

84+
# The CI build has been unreliable with this test.
85+
# Adding a short sleep to ensure successive queries reflect updated state.
86+
Start-Sleep -Seconds $script:defaultSleepSecondsForReliability
87+
8488
Initialize-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Label $defaultLabels
8589
}
8690

@@ -205,6 +209,10 @@ try
205209
BeforeAll {
206210
$repositoryName = [Guid]::NewGuid().Guid
207211
$repo = New-GitHubRepository -RepositoryName $repositoryName
212+
213+
# The CI build has been unreliable with this test.
214+
# Adding a short sleep to ensure successive queries reflect updated state.
215+
Start-Sleep -Seconds $script:defaultSleepSecondsForReliability
208216
}
209217

210218
AfterAll {
@@ -319,6 +327,10 @@ try
319327
BeforeAll {
320328
$repositoryName = [Guid]::NewGuid().Guid
321329
$repo = New-GitHubRepository -RepositoryName $repositoryName
330+
331+
# The CI build has been unreliable with this test.
332+
# Adding a short sleep to ensure successive queries reflect updated state.
333+
Start-Sleep -Seconds $script:defaultSleepSecondsForReliability
322334
}
323335

324336
AfterAll {
@@ -327,6 +339,11 @@ try
327339

328340
Context 'Removing a label with parameters' {
329341
$label = $repo | New-GitHubLabel -Label 'test' -Color 'CCCCCC'
342+
343+
# The CI build has been unreliable with this test.
344+
# Adding a short sleep to ensure successive queries reflect updated state.
345+
Start-Sleep -Seconds $script:defaultSleepSecondsForReliability
346+
330347
Remove-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Label $label.name -Force
331348

332349
It 'Should be gone after being removed by parameter' {
@@ -336,6 +353,11 @@ try
336353

337354
Context 'Removing a label with the repo on the pipeline' {
338355
$label = $repo | New-GitHubLabel -Label 'test' -Color 'CCCCCC'
356+
357+
# The CI build has been unreliable with this test.
358+
# Adding a short sleep to ensure successive queries reflect updated state.
359+
Start-Sleep -Seconds $script:defaultSleepSecondsForReliability
360+
339361
$repo | Remove-GitHubLabel -Label $label.name -Confirm:$false
340362

341363
It 'Should be gone after being removed by parameter' {
@@ -345,6 +367,11 @@ try
345367

346368
Context 'Removing a label with the name on the pipeline' {
347369
$label = $repo | New-GitHubLabel -Label 'test' -Color 'CCCCCC'
370+
371+
# The CI build has been unreliable with this test.
372+
# Adding a short sleep to ensure successive queries reflect updated state.
373+
Start-Sleep -Seconds $script:defaultSleepSecondsForReliability
374+
348375
$label.name | Remove-GitHubLabel -OwnerName $script:ownerName -RepositoryName $repositoryName -Force
349376

350377
It 'Should be gone after being removed by parameter' {
@@ -354,6 +381,11 @@ try
354381

355382
Context 'Removing a label with the label object on the pipeline' {
356383
$label = $repo | New-GitHubLabel -Label 'test' -Color 'CCCCCC'
384+
385+
# The CI build has been unreliable with this test.
386+
# Adding a short sleep to ensure successive queries reflect updated state.
387+
Start-Sleep -Seconds $script:defaultSleepSecondsForReliability
388+
357389
$label | Remove-GitHubLabel -Force
358390

359391
It 'Should be gone after being removed by parameter' {
@@ -366,6 +398,10 @@ try
366398
BeforeAll {
367399
$repositoryName = [Guid]::NewGuid().Guid
368400
$repo = New-GitHubRepository -RepositoryName $repositoryName
401+
402+
# The CI build has been unreliable with this test.
403+
# Adding a short sleep to ensure successive queries reflect updated state.
404+
Start-Sleep -Seconds $script:defaultSleepSecondsForReliability
369405
}
370406

371407
AfterAll {
@@ -543,6 +579,10 @@ try
543579
BeforeAll {
544580
$repositoryName = [Guid]::NewGuid().Guid
545581
$repo = New-GitHubRepository -RepositoryName $repositoryName
582+
583+
# The CI build has been unreliable with this test.
584+
# Adding a short sleep to ensure successive queries reflect updated state.
585+
Start-Sleep -Seconds $script:defaultSleepSecondsForReliability
546586
}
547587

548588
AfterAll {
@@ -617,6 +657,11 @@ try
617657
BeforeAll {
618658
$repositoryName = [Guid]::NewGuid().Guid
619659
$repo = New-GitHubRepository -RepositoryName $repositoryName
660+
661+
# The CI build has been unreliable with this test.
662+
# Adding a short sleep to ensure successive queries reflect updated state.
663+
Start-Sleep -Seconds $script:defaultSleepSecondsForReliability
664+
620665
$repo | Initialize-GitHubLabel -Label $defaultLabels
621666
}
622667

@@ -845,6 +890,11 @@ try
845890
BeforeAll {
846891
$repositoryName = [Guid]::NewGuid().Guid
847892
$repo = New-GitHubRepository -RepositoryName $repositoryName
893+
894+
# The CI build has been unreliable with this test.
895+
# Adding a short sleep to ensure successive queries reflect updated state.
896+
Start-Sleep -Seconds $script:defaultSleepSecondsForReliability
897+
848898
$repo | Initialize-GitHubLabel -Label $defaultLabels
849899
}
850900

@@ -911,6 +961,11 @@ try
911961
BeforeAll {
912962
$repositoryName = [Guid]::NewGuid().Guid
913963
$repo = New-GitHubRepository -RepositoryName $repositoryName
964+
965+
# The CI build has been unreliable with this test.
966+
# Adding a short sleep to ensure successive queries reflect updated state.
967+
Start-Sleep -Seconds $script:defaultSleepSecondsForReliability
968+
914969
$repo | Initialize-GitHubLabel -Label $defaultLabels
915970
}
916971

@@ -1078,6 +1133,11 @@ try
10781133
BeforeAll {
10791134
$repositoryName = [Guid]::NewGuid().Guid
10801135
$repo = New-GitHubRepository -RepositoryName $repositoryName
1136+
1137+
# The CI build has been unreliable with this test.
1138+
# Adding a short sleep to ensure successive queries reflect updated state.
1139+
Start-Sleep -Seconds $script:defaultSleepSecondsForReliability
1140+
10811141
$repo | Initialize-GitHubLabel -Label $defaultLabels
10821142
}
10831143

@@ -1227,6 +1287,11 @@ try
12271287
BeforeAll {
12281288
$repositoryName = [Guid]::NewGuid().Guid
12291289
$repo = New-GitHubRepository -RepositoryName $repositoryName
1290+
1291+
# The CI build has been unreliable with this test.
1292+
# Adding a short sleep to ensure successive queries reflect updated state.
1293+
Start-Sleep -Seconds $script:defaultSleepSecondsForReliability
1294+
12301295
$repo | Initialize-GitHubLabel -Label $defaultLabels
12311296

12321297
$milestone = $repo | New-GitHubMilestone -Title 'test milestone'

Tests/GitHubProjects.tests.ps1

+8
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,10 @@ try
611611
$project = New-GitHubProject -OwnerName $script:ownerName -RepositoryName $repo.name -ProjectName $defaultRepoProject -Description $defaultRepoProjectDesc
612612
$null = Remove-GitHubProject -Project $project.id -Confirm:$false
613613
It 'Project should be removed' {
614+
# The CI build has been unreliable with this test.
615+
# Adding a short sleep to ensure successive queries reflect updated state.
616+
Start-Sleep -Seconds $script:defaultSleepSecondsForReliability
617+
614618
{Get-GitHubProject -Project $project.id} | Should -Throw
615619
}
616620
}
@@ -619,6 +623,10 @@ try
619623
$project = $repo | New-GitHubProject -ProjectName $defaultRepoProject -Description $defaultRepoProjectDesc
620624
$project | Remove-GitHubProject -Force
621625
It 'Project should be removed' {
626+
# The CI build has been unreliable with this test.
627+
# Adding a short sleep to ensure successive queries reflect updated state.
628+
Start-Sleep -Seconds $script:defaultSleepSecondsForReliability
629+
622630
{$project | Get-GitHubProject} | Should -Throw
623631
}
624632
}

0 commit comments

Comments
 (0)