Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SingleNodeExecutor hangs when Clock service is being used #858

Open
almoehi opened this issue Jul 4, 2023 · 1 comment
Open

SingleNodeExecutor hangs when Clock service is being used #858

almoehi opened this issue Jul 4, 2023 · 1 comment

Comments

@almoehi
Copy link

almoehi commented Jul 4, 2023

Hey - I have some code that uses ZIO Clock service.
It's a very simple service method which does the following:

  • get current nanos using Clock.currentTime
  • create a token with timeout (timeoutNanos)
  • store token & timeout in Redis: redis.set(token, currentNanos + timeoutNanos)

When I run tests (which then uses TestClock) and a real Redis container the SingleNodeExecutor hangs indefinitly and it gives me the following warning:

thread=#zio-fiber-236 message="Warning: A test is using time, but is not advancing the test clock, which may result in the test hanging. Use TestClock.adjust to manually advance the time." location=zio.redis.SingleNodeExecutor.run file=SingleNodeExecutor.scala line=40

Relevant method from TokenService service:

  def create(lifetime: Option[zio.Duration], refId: Option[String]): IO[TokenStoreError, Token] = (for {
    nowNanos <- Clock.currentTime(TimeUnit.NANOSECONDS)
    token <- generate()
    maybeSuffix = refId.map("." + _).getOrElse("")
    _ <- (lifetime match {
      case Some(Duration.Infinity) => redis.set(Token.unwrap(token) + maybeSuffix, 0, None)
      case Some(Duration.Zero) => ZIO.succeed(true)
      case Some(finite) => redis.set(Token.unwrap(token) + maybeSuffix, (nowNanos + finite.toNanos), Some(finite))
      case None => ZIO.succeed(true)
    })
  } yield token)
    .logError(s"Failed to create new Token with lifetime $lifetime and refId $refId")
    .mapError(e => TokenStoreError.TokenCreationFailed(e.getMessage))

code in zio-test spec (that test should fail):

      test("Token expiration") {
        for {
          fibre <- TokenStore.create(Some(Duration.fromMillis(200))).fork
          _ <- TestClock.adjust(1.minute)
          token <- fibre.join
          _ <- TokenStore.verify(token)
        } yield assertTrue(token.length > 0)
      }
@mijicd
Copy link
Member

mijicd commented Sep 14, 2023

Sorry for the (very) delayed response. I don't think that running the tests against the "real" Redis with a test clock will work.

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

No branches or pull requests

2 participants