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 all commits
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.nonFlaky)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package zio.process

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

import FilePlatformSpecific._
import java.util.Optional

// TODO: Add aspects for different OSes? scala.util.Properties.isWin, etc. Also try to make this as OS agnostic as possible in the first place
object CommandPlatformSpecificSpec extends ZIOProcessBaseSpec {
object CommandPlatformSpecificSpec extends ZIOSpecDefault {

override def aspects = Chunk(TestAspect.timeout(30.seconds))

def spec = suite("CommandSpec")(
test("killTree also kills child processes") {
Expand Down
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.nonFlaky)
}
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