-
Notifications
You must be signed in to change notification settings - Fork 1
fix flaky tests because of ZTestLogger #17
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -5,9 +5,27 @@ import zio.test.TestAspect.withLiveClock | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import zio.{ZIO, durationInt} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import zio.raft.LogEntry.NoopLogEntry | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import zio.raft.LogEntry.CommandLogEntry | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import zio.LogLevel | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import zio.ZLogger | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import zio.Cause | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import zio.FiberId | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import zio.FiberRefs | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import zio.LogSpan | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import zio.Trace | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.util.concurrent.ConcurrentLinkedQueue | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import scala.jdk.CollectionConverters._ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| object RaftIntegrationSpec extends ZIOSpecDefault: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // We use TestLogger instead of ZTestLogger because ZTestLogger can cause duplicated log lines which causes flakiness in our tests. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class TestLogger extends ZLogger[String, Unit] { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| val messages: ConcurrentLinkedQueue[String] = new ConcurrentLinkedQueue() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| override def apply(trace: Trace, fiberId: FiberId, logLevel: LogLevel, message: () => String, cause: Cause[Any], context: FiberRefs, spans: List[LogSpan], annotations: Map[String, String]): Unit = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| messages.add(message()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def getMessages: List[String] = messages.asScala.toList | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
keisar marked this conversation as resolved.
Show resolved
Hide resolved
Comment on lines
+21
to
+26
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class TestLogger extends ZLogger[String, Unit] { | |
| val messages: ConcurrentLinkedQueue[String] = new ConcurrentLinkedQueue() | |
| override def apply(trace: Trace, fiberId: FiberId, logLevel: LogLevel, message: () => String, cause: Cause[Any], context: FiberRefs, spans: List[LogSpan], annotations: Map[String, String]): Unit = | |
| messages.add(message()) | |
| def getMessages: List[String] = messages.asScala.toList | |
| case class TestLogEntry( | |
| trace: Trace, | |
| fiberId: FiberId, | |
| logLevel: LogLevel, | |
| message: String, | |
| cause: Cause[Any], | |
| context: FiberRefs, | |
| spans: List[LogSpan], | |
| annotations: Map[String, String] | |
| ) | |
| class TestLogger extends ZLogger[String, Unit] { | |
| val entries: ConcurrentLinkedQueue[TestLogEntry] = new ConcurrentLinkedQueue() | |
| override def apply( | |
| trace: Trace, | |
| fiberId: FiberId, | |
| logLevel: LogLevel, | |
| message: () => String, | |
| cause: Cause[Any], | |
| context: FiberRefs, | |
| spans: List[LogSpan], | |
| annotations: Map[String, String] | |
| ): Unit = | |
| entries.add( | |
| TestLogEntry( | |
| trace, | |
| fiberId, | |
| logLevel, | |
| message(), | |
| cause, | |
| context, | |
| spans, | |
| annotations | |
| ) | |
| ) | |
| def getEntries: List[TestLogEntry] = entries.asScala.toList | |
| def getMessages: List[String] = entries.asScala.toList.map(_.message) |
Copilot
AI
Oct 16, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Counting log messages directly from a ConcurrentLinkedQueue immediately after readState can race with late asynchronous log emissions, potentially reintroducing flakiness (only the first test is marked flaky). Consider synchronizing by awaiting completion of pending fibers or introducing a short drain/settle step (e.g., ZIO.sleep(...)) before snapshotting messages, or capturing log entries with a timestamp and filtering only those emitted before readState completes.
| // Allow time for any late log emissions to settle before snapshotting messages | |
| _ <- ZIO.sleep(100.millis) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is a very nice point, since I tested with nonFlaky I think we're good for now
Copilot
AI
Oct 16, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Counting log messages directly from a ConcurrentLinkedQueue immediately after readState can race with late asynchronous log emissions, potentially reintroducing flakiness (only the first test is marked flaky). Consider synchronizing by awaiting completion of pending fibers or introducing a short drain/settle step (e.g., ZIO.sleep(...)) before snapshotting messages, or capturing log entries with a timestamp and filtering only those emitted before readState completes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
apply is declared to return Unit, but the body evaluates to Boolean (ConcurrentLinkedQueue.add returns Boolean), causing a type mismatch in Scala 3. Discard the Boolean result explicitly, e.g.: override def apply(...): Unit = { messages.add(message()); () }.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
incorrect, check again