uber / mockolo

Efficient Mock Generator for Swift
Apache License 2.0
804 stars 85 forks source link

Added override for specifying the global actor of mocked async functions #260

Open stuaustin opened 1 month ago

stuaustin commented 1 month ago

Issue

249 Async functions are unsafe in generated mocks, and can crash if used from multiple threads simultaneously

Description

This change lets you specify the global actor that is used for your generated mock's async functions. This can be used to solve the problem described in #249 as when used there is no longer the chance for the async functions to be executed multiple times simultaneously. I have allowed the user to specify the global actor so that they can choose to use a custom GlobalActor instead of assuming they always want to use the MainActor.

sidepelican commented 1 month ago

Thank you for your contribution.

While this feature is opt-in, I don't believe using a GlobalActor is the fundamental solution. Introducing locks would be the correct approach to address exclusive access issues.

However, mockolo currently lacks a suitable lock implementation. NSLock depends on Foundation, and swift-atomics might not always be available. I’m looking forward to the standard library introducing a Mutex. https://github.com/apple/swift-evolution/blob/main/proposals/0433-mutex.md.