Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@adamgfraser
Copy link
Contributor

The current implementation of MockClock could be subject to a race condition in some circumstances. Consider the following snippet:

for {
  mockClock <- MockClock.makeMock(DefaultData)
  fiber     <- mockClock.sleep(2.millis).zipPar(mockClock.sleep(1.millis)).fork
  _         <- mockClock.adjust(2.millis)
  _         <- fiber.join
  result    <- mockClock.fiberState.get
} yield result.nanoTime == 2000000L

In the current implementation this would be flaky. When we adjust the MockClock we wake up both the left and the right effects in zipPar. Although we wake up the right effect first, since the effects are on separate fibers their order of completion is indeterminate. So if the left effect completes first, the right effect will set the fiber time to 1 millisecond, leading to data corruption.

With the new functionality for specifying custom logic for inheriting FiberRef instances implemented in #1879 we can address this by always using the maximum fiber time when combining data from two fibers.

@adamgfraser adamgfraser requested a review from ghostdogpr October 3, 2019 03:24
@adamgfraser adamgfraser merged commit 6782834 into zio:master Oct 3, 2019
@adamgfraser adamgfraser deleted the mockclock branch October 3, 2019 03:36
Twizty pushed a commit to Twizty/zio that referenced this pull request Nov 13, 2019
* harden MockClock

* cleanup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants