From 55803b2657a473a1ebbebfd9ab7ba4c1b4e27d38 Mon Sep 17 00:00:00 2001 From: Felix Mulder Date: Fri, 7 Apr 2017 13:47:07 +0200 Subject: Implement meta tests suggested by @DarkDimius Note that merging this as-is will not protect us against fork bombs. This is because the timeout of tests is currently 180 seconds. A forkbomb that is allowed to run for that long... --- compiler/test/dotty/tools/vulpix/VulpixTests.scala | 15 ++++++++ tests/partest-test/deadlock.scala | 44 ++++++++++++++++++++++ tests/partest-test/forkbomb.scala | 7 ++++ tests/partest-test/infinite.scala | 9 +++++ tests/partest-test/infiniteAlloc.scala | 9 +++++ tests/partest-test/infiniteTail.scala | 7 ++++ 6 files changed, 91 insertions(+) create mode 100644 tests/partest-test/deadlock.scala create mode 100644 tests/partest-test/forkbomb.scala create mode 100644 tests/partest-test/infinite.scala create mode 100644 tests/partest-test/infiniteAlloc.scala create mode 100644 tests/partest-test/infiniteTail.scala diff --git a/compiler/test/dotty/tools/vulpix/VulpixTests.scala b/compiler/test/dotty/tools/vulpix/VulpixTests.scala index 154008bd1..2483bf6f0 100644 --- a/compiler/test/dotty/tools/vulpix/VulpixTests.scala +++ b/compiler/test/dotty/tools/vulpix/VulpixTests.scala @@ -59,4 +59,19 @@ class VulpixTests extends ParallelTesting { @Test def runOutRedirects: Unit = compileFile("../tests/partest-test/i2147.scala", defaultOptions).expectFailure.checkRuns() + + @Test def infiteNonRec: Unit = + compileFile("../tests/partest-test/infinite.scala", defaultOptions).expectFailure.checkRuns() + + @Test def infiteTailRec: Unit = + compileFile("../tests/partest-test/infiniteTail.scala", defaultOptions).expectFailure.checkRuns() + + @Test def infiniteAlloc: Unit = + compileFile("../tests/partest-test/infiniteAlloc.scala", defaultOptions).expectFailure.checkRuns() + + @Test def deadlock: Unit = + compileFile("../tests/partest-test/deadlock.scala", defaultOptions).expectFailure.checkRuns() + + @Test def forkbomb: Unit = + compileFile("../tests/partest-test/forkbomb.scala", defaultOptions).expectFailure.checkRuns() } diff --git a/tests/partest-test/deadlock.scala b/tests/partest-test/deadlock.scala new file mode 100644 index 000000000..df561aff3 --- /dev/null +++ b/tests/partest-test/deadlock.scala @@ -0,0 +1,44 @@ +object Test { + class Lock + val lock1 = new Lock + val lock2 = new Lock + + private[this] var took2: Boolean = false + def lock2Taken(): Unit = synchronized { + took2 = true + notify() + } + def tookLock2: Boolean = synchronized(took2) + + val thread1 = new Thread { + override def run(): Unit = synchronized { + lock1.synchronized { + while (!tookLock2) wait() + lock2.synchronized { + println("thread1 in lock2!") + } + } + println("thread1, done!") + } + } + + val thread2 = new Thread { + override def run(): Unit = synchronized { + lock2.synchronized { + lock2Taken() + lock1.synchronized { + println("thread2 in lock1!") + } + } + println("thread2, done!") + } + } + + def main(args: Array[String]): Unit = { + thread1.start() // takes lock1 then sleeps 1s - tries to take lock2 + thread2.start() // takes lock2 then sleeps 1s - tries to take lock1 + + thread1.join() // wait for threads to complete, can't because deadlock! + thread2.join() + } +} diff --git a/tests/partest-test/forkbomb.scala b/tests/partest-test/forkbomb.scala new file mode 100644 index 000000000..1d3cda172 --- /dev/null +++ b/tests/partest-test/forkbomb.scala @@ -0,0 +1,7 @@ +object Test { + def main(args: Array[String]): Unit = + while(true) + Runtime + .getRuntime() + .exec(Array("java", "-cp", System.getProperty("java.class.path"), "Test")); +} diff --git a/tests/partest-test/infinite.scala b/tests/partest-test/infinite.scala new file mode 100644 index 000000000..961382fea --- /dev/null +++ b/tests/partest-test/infinite.scala @@ -0,0 +1,9 @@ +object Test { + def main(args: Array[String]): Unit = { + var sum = 0 + while(true) { + sum += 1 + } + println(sum) + } +} diff --git a/tests/partest-test/infiniteAlloc.scala b/tests/partest-test/infiniteAlloc.scala new file mode 100644 index 000000000..89fa5d6ef --- /dev/null +++ b/tests/partest-test/infiniteAlloc.scala @@ -0,0 +1,9 @@ +import scala.collection.mutable +object Test { + val map = mutable.Map.empty[String, String] + + def main(args: Array[String]): Unit = while (true) { + val time = System.currentTimeMillis.toString + map += (time -> time) + } +} diff --git a/tests/partest-test/infiniteTail.scala b/tests/partest-test/infiniteTail.scala new file mode 100644 index 000000000..b3132cc19 --- /dev/null +++ b/tests/partest-test/infiniteTail.scala @@ -0,0 +1,7 @@ +object Test { + def foo: Int = bar + def bar: Int = foo + + def main(args: Array[String]): Unit = + println(foo) +} -- cgit v1.2.3