Fix occasional race conditions when testing #414
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
When testing in Xcode, I was encountering an occasional crash that was happening when other tests were being run (tests other than mine).
They appear to be related to these tests not calling PublishingContext.initialize() before calling render(), though actually there are a few other tests that don't call PublishingContext.initialize() before calling render() and don't seem to cause this issue.
This behavior can be reproduced pretty reliably by control-clicking the test button on line 14 of IncludeTests in Include.swift and choosing "Run 'Include Tests' Repeatedly…"
![Run 'Include Tests'](https://private-user-images.githubusercontent.com/18646300/407423431-29e40b5d-3bc6-4250-8944-e8cf402c37d5.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzk1ODYyNTcsIm5iZiI6MTczOTU4NTk1NywicGF0aCI6Ii8xODY0NjMwMC80MDc0MjM0MzEtMjllNDBiNWQtM2JjNi00MjUwLTg5NDQtZThjZjQwMmMzN2Q1LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMTUlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjE1VDAyMTkxN1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWUyYTRlOTQyZDU1ZjczNmNjNTg5YzQ5ODczZTE3YTRjMDVkZTFkYWI3MjFjNDg3N2Y4Y2IzZmRhMTIzMjg5MjcmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.W7sQ7qE2QxbA0iZ8otOgCJUgcxJJHn-9p9eWMdduTIc)
As I say, there are a few tests that can cause this crash, and it's flakey exactly when it will happen.
Still, by calling
try PublishingContext.initialize(for: TestSite(), from: #filePath)
on init, this crash can be avoided. This is at least the approach that others have used in tests like SubsiteLinkTests in Link.swift.So I added this to a few tests that seem to cause this crash for me. If others encounter this with other tests, they can feel free to copy this approach.