Skip to content

Commit

Permalink
Add a stress test for creating lots of futures
Browse files Browse the repository at this point in the history
  • Loading branch information
natsukagami committed Dec 18, 2023
1 parent c3d1c36 commit 6b64571
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,5 @@ jobs:
run: sudo apt-get install clang libstdc++-12-dev libgc-dev
- name: Test
run: sbt rootNative/test
- name: Stress Test with Lower Memory
run: env GC_MAXIMUM_HEAP_SIZE=256M sbt 'rootNative/testOnly StressTest'
23 changes: 23 additions & 0 deletions shared/src/test/scala/Stress.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import gears.async.{Async, Future, AsyncSupport, uninterruptible}
import gears.async.AsyncOperations.*
import gears.async.default.given
import gears.async.Future.MutableCollector
import java.util.concurrent.atomic.AtomicInteger

class StressTest extends munit.FunSuite:
test("survives a stress test that hammers on creating futures") {
val total = 200_000L
Seq[Long](1, 2, 4, 16, 1024).foreach: parallelism =>
val k = AtomicInteger(0)
def compute(using Async) =
k.incrementAndGet()
Async.blocking:
val collector = MutableCollector((1L to parallelism).map(_ => Future { compute })*)
var sum = 0L
for i <- parallelism + 1 to total do
sum += collector.results.read().right.get.await
collector += Future { compute }
for i <- 1L to parallelism do sum += collector.results.read().right.get.await
assertEquals(sum, total * (total + 1) / 2)

}

0 comments on commit 6b64571

Please sign in to comment.