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

Fix flaky tests #434

Open
wants to merge 6 commits into
base: series/2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package zio.process

import zio.test._
import zio.{ durationInt, Chunk }

trait ZIOProcessBaseSpec extends ZIOSpecDefault {
override def aspects = Chunk(TestAspect.timeout(30.seconds), TestAspect.flaky)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I don't think we want to make the default TestAspect.flaky. That could cover up legitimate problems. If any tests are flaky (and can't easily be fixed), I'd rather mark them @@ TestAspect.flaky at the individual level so that it's clear which tests have problems.

But maybe this was unintentional or temporary code? Because I see you also do @@ TestAspect.nonFlaky at the suite level below.

Copy link
Member Author

@pablf pablf Aug 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, this is a typo, it was intended to be nonFlaky... The nonFlaky at suite level is just for one suite because in CommandSpec for Scala Native the nonFlaky times out and it passed it anyway.

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package zio.process

import zio.test._
import zio.{ durationInt, Chunk }

trait ZIOProcessBaseSpec extends ZIOSpecDefault {
override def aspects = Chunk(TestAspect.timeout(30.seconds), TestAspect.flaky)
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@ final case class ProcessStream(
private[process] val outputStream: Option[OutputStream] = None
) {

private def close: ZIO[Any, Nothing, Unit] =
private def close: ZIO[Any, CommandError, Unit] =
outputStream match {
case None => ZIO.unit
case Some(out) => ZIO.succeed(out.close())
case Some(out) =>
ZIO.attemptBlocking(out.close()).mapError { case e: Throwable =>
CommandError.Error(e)
}
}

/**
Expand Down Expand Up @@ -94,9 +97,12 @@ final case class ProcessStream(
* Note: Needs Java 9 or greater.
*/
def string(charset: Charset): ZIO[Any, CommandError, String] =
ZIO.attemptBlockingCancelable {
new String(inputStream.readAllBytes(), charset)
}(ZIO.succeed(inputStream.close())).refineOrDie { case CommandThrowable.IOError(e) =>
ZIO.scoped {
close *> ProcessPlatformSpecific.wait(inputStream) *> ZIO.attemptBlockingCancelable {
Comment on lines +100 to +101
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand the purpose of the initial close as well as the ZIO.scoped. Could this implementation be this instead?

(for {
  _ <- ProcessPlatformSpecific.wait(inputStream)
  s <- ZIO.attemptBlockingCancelable {
    new String(inputStream.readAllBytes(), charset)
  }(ZIO.succeed(inputStream.close()))
} yield s).refineOrDie { case CommandThrowable.IOError(e) =>
  e
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried the code I pasted locally and it seems like all the tests pass.

Copy link
Member Author

@pablf pablf Aug 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically, close closes the OutputStream from where the InputStream that we want to read is getting data while ProcessPlatformSpecific.wait in ScalaJS waits for that OutputStream to end so it seems like they might be equivalent. Maybe they could have different behaviour with infinite streaming? In current tests they should behave the same.

new String(inputStream.readAllBytes(), charset)
}(ZIO.succeed(inputStream.close()))
}.refineOrDie { case CommandThrowable.IOError(e) =>
e
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,5 @@ object PipedCommandSpec extends ZIOProcessBaseSpec {
equalTo(NonEmptyChunk(false, false, true))
)
}
)
) @@ TestAspect.nonFlaky
}
Loading