<commit_before>package scala.reactive
package test.signal



import org.scalatest._
import org.scalatest.matchers.ShouldMatchers
import util._



class ReactiveGCSpec extends FlatSpec with ShouldMatchers {

  def testGC(num: Int)(afterCheck: Reactive.Emitter[Int] => Boolean) {
    var signsOfLife = Array.fill(num)(false)
    val emitter = new Reactive.Emitter[Int]
    for (i <- 0 until num) emitter foreach { _ =>
      signsOfLife(i) = true
    }

    // flaky test - GC is not obliged to collect
    sys.runtime.gc()
    sys.runtime.gc()
    sys.runtime.gc()

    emitter react 1

    for (i <- 0 until num) signsOfLife(i) should equal (false)
    afterCheck(emitter) should equal (true)
  }

  "A reactive" should "GC a single dependency" in {
    testGC(1) {
      _.demux == null
    }
  }

  it should "GC several dependencies" in {
    testGC(4) {
      _.demux == null
    }
  }

  it should "GC many dependencies" in {
    testGC(32) {
      _.demux == null
    }
  }

  def testOnX(num: Int)(afterCheck: Reactive.Emitter[Int] => Boolean) {
    implicit val canLeak = Permission.newCanLeak

    var signsOfLife = Array.fill(num)(false)
    val emitter = new Reactive.Emitter[Int]
    for (i <- 0 until num) emitter onEvent { _ =>
      signsOfLife(i) = true
    }

    sys.runtime.gc()

    emitter react 1

    for (i <- 0 until num) signsOfLife(i) should equal (true)
    afterCheck(emitter) should equal (true)
  }

  it should "not GC 1 onX dependency" in {
    testOnX(1) {
      _.demux != null
    }
  }

  it should "not GC several onX dependencies" in {
    testOnX(4) {
      _.demux != null
    }
  }

  it should "not GC many onX dependencies" in {
    testOnX(32) {
      _.demux != null
    }
  }

  def testDep(num: Int)(afterCheck: Reactive.Emitter[Int] => Boolean) {
    var signsOfLife = Array.fill(num)(false)
    val emitter = new Reactive.Emitter[Int]
    val subs = for (i <- 0 until num) yield emitter foreach { _ =>
      signsOfLife(i) = true
    }

    sys.runtime.gc()

    emitter react 1

    for (i <- 0 until num) signsOfLife(i) should equal (true)
    afterCheck(emitter) should equal (true)
  }

  it should "accurately reflect a single dependency" in {
    testDep(1) {
      _.demux.isInstanceOf[java.lang.ref.WeakReference[_]]
    }
  }

  it should "accurately reflect several dependency" in {
    testDep(6) {
      _.demux.isInstanceOf[WeakBuffer[_]]
    }
  }

  it should "accurately reflect many dependency" in {
    testDep(32) {
      _.demux.isInstanceOf[WeakHashTable[_]]
    }
  }

  def testGChalf(num: Int)(afterCheck: Reactive.Emitter[Int] => Boolean) {
    var signsOfLife = Array.fill(num)(false)
    val emitter = new Reactive.Emitter[Int]
    val kept = for (i <- 0 until num / 2) yield emitter foreach { _ =>
      signsOfLife(i) = true
    }
    for (i <- num / 2 until num) emitter foreach { _ =>
      signsOfLife(i) = true
    }

    sys.runtime.gc()

    emitter react 1

    for (i <- 0 until num / 2) signsOfLife(i) should equal (true)
    for (i <- num / 2 until num) signsOfLife(i) should equal (false)<commit_msg>Make GC tests more robust.<commit_after>package scala.reactive
package test.signal



import org.scalatest._
import org.scalatest.matchers.ShouldMatchers
import util._



class ReactiveGCSpec extends FlatSpec with ShouldMatchers {

  def testGC(num: Int)(afterCheck: Reactive.Emitter[Int] => Boolean) {
    var signsOfLife = Array.fill(num)(false)
    val emitter = new Reactive.Emitter[Int]
    for (i <- 0 until num) emitter foreach { _ =>
      signsOfLife(i) = true
    }

    // flaky test - GC is not obliged to collect
    sys.runtime.gc()
    sys.runtime.gc()
    sys.runtime.gc()

    emitter react 1

    for (i <- 0 until num) signsOfLife(i) should equal (false)
    afterCheck(emitter) should equal (true)
  }

  "A reactive" should "GC a single dependency" in {
    testGC(1) {
      _.demux == null
    }
  }

  it should "GC several dependencies" in {
    testGC(4) {
      _.demux == null
    }
  }

  it should "GC many dependencies" in {
    testGC(32) {
      _.demux == null
    }
  }

  def testOnX(num: Int)(afterCheck: Reactive.Emitter[Int] => Boolean) {
    implicit val canLeak = Permission.newCanLeak

    var signsOfLife = Array.fill(num)(false)
    val emitter = new Reactive.Emitter[Int]
    for (i <- 0 until num) emitter onEvent { _ =>
      signsOfLife(i) = true
    }

    sys.runtime.gc()
    sys.runtime.gc()
    sys.runtime.gc()

    emitter react 1

    for (i <- 0 until num) signsOfLife(i) should equal (true)
    afterCheck(emitter) should equal (true)
  }

  it should "not GC 1 onX dependency" in {
    testOnX(1) {
      _.demux != null
    }
  }

  it should "not GC several onX dependencies" in {
    testOnX(4) {
      _.demux != null
    }
  }

  it should "not GC many onX dependencies" in {
    testOnX(32) {
      _.demux != null
    }
  }

  def testDep(num: Int)(afterCheck: Reactive.Emitter[Int] => Boolean) {
    var signsOfLife = Array.fill(num)(false)
    val emitter = new Reactive.Emitter[Int]
    val subs = for (i <- 0 until num) yield emitter foreach { _ =>
      signsOfLife(i) = true
    }

    sys.runtime.gc()
    sys.runtime.gc()
    sys.runtime.gc()

    emitter react 1

    for (i <- 0 until num) signsOfLife(i) should equal (true)
    afterCheck(emitter) should equal (true)
  }

  it should "accurately reflect a single dependency" in {
    testDep(1) {
      _.demux.isInstanceOf[java.lang.ref.WeakReference[_]]
    }
  }

  it should "accurately reflect several dependency" in {
    testDep(6) {
      _.demux.isInstanceOf[WeakBuffer[_]]
    }
  }

  it should "accurately reflect many dependency" in {
    testDep(32) {
      _.demux.isInstanceOf[WeakHashTable[_]]
    }
  }

  def testGChalf(num: Int)(afterCheck: Reactive.Emitter[Int] => Boolean) {
    var signsOfLife = Array.fill(num)(false)
    val emitter = new Reactive.Emitter[Int]
    val kept = for (i <- 0 until num / 2) yield emitter foreach { _ =>
      signsOfLife(i) = true
    }
    for (i <- num / 2 until num) emitter foreach { _ =>
      signsOfLife(i) = true
    }

    sys.runtime.gc()
    sys.runtime.gc()
    sys.runtime.gc()

    emitter react 1

    for (i <- 0 until num / 2) signsOfLife(i) should equal (true)
    for (i <- num / 2 until num) signsOfLife(i) should equal (false)
{'fraction_non_alphanumeric': 0.1172653900933692, 'fraction_numerical': 0.00917866751068207, 'mean_word_length': 2.590909090909091, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5898114', 'n_tokens_mistral': 2245, 'n_tokens_neox': 2105, 'n_words': 601}