Skip to content

Remove scalacheck fork by implementing buildableOfCollCond #358

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

Merged
merged 1 commit into from
Feb 9, 2023
Merged
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
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ lazy val core = project
"com.typesafe.akka" %% "akka-stream" % akkaVersion % Test,
"org.scalatest" %% "scalatest" % scalaTestVersion % Test,
"org.scalatestplus" %% scalaTestScalaCheckArtifact % scalaTestScalaCheckVersion % Test,
"org.mdedetrich" %% "scalacheck" % scalaCheckVersion % Test,
"org.scalacheck" %% "scalacheck" % scalaCheckVersion % Test,
"com.rallyhealth" %% "scalacheck-ops_1-16" % scalaCheckOpsVersion % Test,
"com.softwaremill.diffx" %% "diffx-scalatest-must" % diffxVersion % Test,
"com.typesafe.akka" %% "akka-stream-testkit" % akkaVersion % Test,
Expand Down
22 changes: 21 additions & 1 deletion core/src/test/scala/io/aiven/guardian/kafka/Generators.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.aiven.guardian.kafka
import io.aiven.guardian.kafka.models.ReducedConsumerRecord
import org.apache.kafka.common.record.TimestampType
import org.scalacheck.Gen
import org.scalacheck.util.Buildable

import scala.collection.mutable.ListBuffer
import scala.concurrent.duration.FiniteDuration
Expand Down Expand Up @@ -113,6 +114,25 @@ object Generators {
Gen.choose[Long](head.timestamp, last.timestamp - 1).map(millis => FiniteDuration(millis, MILLISECONDS))
}

@SuppressWarnings(
Array(
"scalafix:DisableSyntax.while"
)
)
private def buildableOfCollCond[C <: Iterable[T], T](cond: C => Boolean, g: Gen[C])(implicit
evb: Buildable[T, C]
): Gen[C] =
Gen.infiniteLazyList(g).map { ll =>
val it = ll.iterator
val bldr = evb.builder
while (!cond(bldr.result()))
Copy link
Contributor

Choose a reason for hiding this comment

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

The only option I see (without introducing the mutable state) is something like this (pseudo code, probably needs some scalafication):

while (true) {
  val result = bldr.result();
  if (cond(result)) {
    return result;
  }
  bldr ++= it.next()
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for recommendation, I wanted to avoid this because it involved having to circumvent Scala's lint warnings but I managed to find a way. Just pushed it now to see if tests pass.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So I tried a solution, i.e.

@SuppressWarnings(
  Array(
    "scalafix:DisableSyntax.while",
    "scalafix:DisableSyntax.return"
  )
)
@scala.annotation.nowarn("msg=return statement uses an exception")
private def buildableOfCollCond[C <: Iterable[T], T](cond: C => Boolean, g: Gen[C])(implicit
    evb: Buildable[T, C]
): Gen[C] =
  Gen.infiniteLazyList(g).map { ll =>
    val it   = ll.iterator
    val bldr = evb.builder
    while (true) {
      val result = bldr.result()
      if (cond(result)) {
        return result
      }
      bldr ++= it.next()
    }
    return bldr.result()
  }

And it didn't work/terminate. Don't have time to look at this now so I will make an issue to look at it later as an improvement.

bldr ++= it.next()
bldr.result() // sub-optimal: is called twice for the same result, can be improved!
}

private def listOfFillCond[T](finishCondition: List[T] => Boolean, g: => Gen[List[T]]) =
buildableOfCollCond[List[T], T](finishCondition, g)

def kafkaDateGen(min: Int = 2,
max: Int = 100,
padTimestampsMillis: Range = Range.inclusive(1, 10),
Expand All @@ -121,7 +141,7 @@ object Generators {
topic <- kafkaTopic
records <- {
val base = Generators.kafkaReducedConsumerRecordsGen(topic, min, max, padTimestampsMillis)
condition.fold(base)(c => Gen.listOfFillCond(c, base))
condition.fold(base)(c => listOfFillCond(c, base))
}
} yield records

Expand Down