krzysztofzablocki / Sourcery

Meta-programming for Swift, stop writing boilerplate code.
http://merowing.info
MIT License
7.59k stars 605 forks source link

[QUESTION] Thread-Safe Mocks #1275

Open pklapuch opened 4 months ago

pklapuch commented 4 months ago

Hi,

Did anyone consider threading issues? Would it be hard to generate serialized access to Mock properties? I guess Mock could either be generated as actor or it could wrap it's properties using queue.sync { }.

This especially becomes a problem with arrays (i.e. the ...invocations) which leads to EXT_BAD_ACCESS but can have undefined behavior with just about any read/write property.. which leads to inconsistent state at the end of the test.

(I guess most tests are simple and run in isolated/serialized context, but now and then we have features that support concurrent execution and that becomes a problem).

In these cases I either end up creating custom Mocks or I extend generated mock, override methods and wrap them with queue.sync etc...

Any suggestions would be welcome :) Thanks!

krzysztofzablocki commented 4 months ago

hey, many companies just take the default macro we bundle and tweak them so there really isn't much stopping you from doing so, if you do then you could add a variant of the template as a pr 😉

Greensource commented 1 month ago

@pklapuch I ended with the same issue. Did you starting work on a PR about that ?

pklapuch commented 1 month ago

@Greensource I did come up with a naive but working solution (I'm terrified of stencil :P also hate it...) - it pretty much came to adding queue.sync here and there (in 3x places), feel free to use my template as inspiration.

ThreadSafeAutoMockable.txt