Cuckoo icon indicating copy to clipboard operation
Cuckoo copied to clipboard

Mock Manager Thread Safety

Open ERussel opened this issue 5 years ago • 3 comments

It turns out that now mocked object is not thread safe due to internal mock manager object whose implementation is not thread safe. That can cause problems when one needs to test asynchronous behaviour.

Is there any recommendations how to solve the problem?

One way which I am using currently: generating mocked object, changing implementation to make it thread safe and removing it from build phase regeneration.

In long term I am considering 3 options but all of them requiring fork of the library:

  1. Obvious one is to introduce synchronisation mechanisms to MockManager implementation.
  2. Create a wrapper inheriting MockManager class and overriding methods with synchronisation feature.
  3. Introduce a way to add thread safety to mocked object during generation.

ERussel avatar Jun 09 '19 09:06 ERussel

@ERussel Hey, thank you for this report. Could you fork the Cuckoo repository and create a test that would replicate the problem? It'll be easier to find it and implement a solution.

TadeasKriz avatar Jun 14 '19 12:06 TadeasKriz

Sure. Will try to do it asap.

ERussel avatar Jun 14 '19 19:06 ERussel

I ran into this the other day with a snippet like this:

        let numConcurrentRequests = 1000

        DispatchQueue.concurrentPerform(iterations: numConcurrentRequests) { index in
            queuedRequestRetrier.should(retry: requests[index], with: MockRequestError.anError) { (shouldRetry, _) in
                XCTAssertTrue(shouldRetry)
                retryExpectation.fulfill()
            }
         }

...where queuedRequestRetrier has a mocked network API dependency injected into it. It blew up on every run.

jamezilla avatar Sep 17 '19 20:09 jamezilla