From b5c141b4ed5e2db673a306a2ba0c23a1fbe2d9ee Mon Sep 17 00:00:00 2001 From: Philipp Haller Date: Thu, 7 Jan 2010 11:35:41 +0000 Subject: Fixed issue in Reactor/Actor that could lead to... Fixed issue in Reactor/Actor that could lead to premature termination of actors. Added test that could reproduce it (occurred more often on larger inputs, but test should not take too much time). The issue also caused the reactor-exceptionOnSend test to timeout sometimes. Review by plocinic. --- test/files/jvm/reactor-producer-consumer.scala | 75 ++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 test/files/jvm/reactor-producer-consumer.scala (limited to 'test/files/jvm/reactor-producer-consumer.scala') diff --git a/test/files/jvm/reactor-producer-consumer.scala b/test/files/jvm/reactor-producer-consumer.scala new file mode 100644 index 0000000000..946e1561ce --- /dev/null +++ b/test/files/jvm/reactor-producer-consumer.scala @@ -0,0 +1,75 @@ +import scala.actors.Reactor + +object Test { + case class Stop() + case class Get(from: Reactor) + case class Put(x: Int) + + class UnboundedBuffer extends Reactor { + def act() { + react { + case Stop() => + case Get(from) => + val consumer = from + react { + case msg @ Put(x) => + consumer ! x + act() + } + } + } + } + + class Producer(buf: UnboundedBuffer, n: Int, delay: Long, parent: Reactor) extends Reactor { + def act() { + var i = 0 + while (i < n) { + i += 1 + if (delay > 0) Thread.sleep(delay) + buf ! Put(42) + } + parent ! Stop() + } + } + + class Consumer(buf: UnboundedBuffer, n: Int, delay: Long, parent: Reactor) extends Reactor { + val step = n / 10 + var i = 0 + def act() { + if (i < n) { + i += 1 + if (delay > 0) Thread.sleep(delay) + buf ! Get(this) + react { + case res => + if (i % step == 0) + println(res) + act() + } + } else { + parent ! Stop() + } + } + } + + def main(args: Array[String]) { + val parent = new Reactor { + def act() { + val buffer = new UnboundedBuffer + buffer.start() + val producer = new Producer(buffer, 10000, 0, this) + producer.start() + val consumer = new Consumer(buffer, 10000, 0, this) + consumer.start() + react { + case Stop() => + react { + case Stop() => + buffer ! Stop() + } + } + } + } + parent.start() + } +} -- cgit v1.2.3