Skip to content

Commit

Permalink
wip: add StackOverflowError test for RebalanceCallback
Browse files Browse the repository at this point in the history
  • Loading branch information
nikitapecasa committed Apr 9, 2021
1 parent 0674dc2 commit 757623f
Showing 1 changed file with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import java.util.{Collection => CollectionJ, List => ListJ, Map => MapJ, Set =>

import cats.data.{NonEmptyMap => Nem, NonEmptySet => Nes}
import cats.effect.IO
import com.evolutiongaming.skafka.IOSuite._
import com.evolutiongaming.skafka.consumer.RebalanceCallback._
import com.evolutiongaming.skafka.consumer.RebalanceCallbackSpec._
import com.evolutiongaming.skafka.{Offset, Partition, TopicPartition}
Expand All @@ -21,12 +22,11 @@ import org.apache.kafka.clients.consumer.{
import org.apache.kafka.common.{Metric, MetricName, PartitionInfo, TopicPartition => TopicPartitionJ}
import org.scalatest.freespec.AnyFreeSpec
import org.scalatest.matchers.must.Matchers
import com.evolutiongaming.skafka.IOSuite._

import scala.concurrent.duration._
import scala.jdk.CollectionConverters._
import scala.util.{Failure, Try}
import scala.util.control.NoStackTrace
import scala.util.{Failure, Try}

class RebalanceCallbackSpec extends AnyFreeSpec with Matchers {

Expand Down Expand Up @@ -166,6 +166,24 @@ class RebalanceCallbackSpec extends AnyFreeSpec with Matchers {
list mustBe List("one", "two", "3")
}

"flatMap is free from StackOverflowError" in {
val stackOverflowErrorDepth = 1000000
// check that deep enough recursion results in StackOverflowError with current JVM settings
try {
// trigger SOE with given stackOverflowErrorDepth
triggerStackOverflowError(stackOverflowErrorDepth)
fail(
s"expected a StackOverflowError from $stackOverflowErrorDepth-deep recursion, consider increasing the depth in test"
)
} catch {
case _: StackOverflowError => // stackOverflowErrorDepth has correct value
}

val rc = List.fill(stackOverflowErrorDepth)(noOp[Try]).fold(noOp[Try]) { (agg, e) => agg.flatMap(_ => e) }

tryRun(rc, null) mustBe Try(())
}

}

}
Expand Down Expand Up @@ -323,4 +341,9 @@ object RebalanceCallbackSpec {
DurationJ.ofSeconds(7),
7.seconds
)

def triggerStackOverflowError(n: Int): Int = {
if (n <= 0) n
else n + triggerStackOverflowError(n - 1)
}
}

0 comments on commit 757623f

Please sign in to comment.