aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Izraelevitz2016-06-10 12:49:48 -0700
committerGitHub2016-06-10 12:49:48 -0700
commita81c5ac84fe676b09fa5f6b0789d07fffc455448 (patch)
tree5f72acae64bbc064a89a378343023b446eb63fcb
parent7afe9f6180a53fd9f024c67d78289689a601c8b7 (diff)
parent1eb2e11b84797961d1983cb963ffe16f35a1a895 (diff)
Merge pull request #190 from ucb-bar/threadsafe-test
Add test to check compiler is thread safe
-rw-r--r--src/test/scala/firrtlTests/MultiThreadingSpec.scala52
1 files changed, 52 insertions, 0 deletions
diff --git a/src/test/scala/firrtlTests/MultiThreadingSpec.scala b/src/test/scala/firrtlTests/MultiThreadingSpec.scala
new file mode 100644
index 00000000..8e5ba614
--- /dev/null
+++ b/src/test/scala/firrtlTests/MultiThreadingSpec.scala
@@ -0,0 +1,52 @@
+// See LICENSE for license details.
+
+package firrtlTests
+
+import scala.concurrent.{Future, Await, ExecutionContext}
+import scala.concurrent.duration.Duration
+
+class MultiThreadingSpec extends FirrtlPropSpec {
+
+ // TODO Test with annotations and source locator
+ property("The FIRRTL compiler should be thread safe") {
+ // Run the compiler we're testing
+ def runCompiler(input: Seq[String], compiler: firrtl.Compiler): String = {
+ val writer = new java.io.StringWriter
+ val parsedInput = firrtl.Parser.parse(input)
+ compiler.compile(parsedInput, Seq(), writer)
+ writer.toString
+ }
+ // The parameters we're testing with
+ val compilers = Seq(
+ new firrtl.HighFirrtlCompiler,
+ new firrtl.LowFirrtlCompiler,
+ new firrtl.VerilogCompiler)
+ val inputFilePath = s"/integration/GCDTester.fir" // arbitrary
+ val numThreads = 8 // arbitrary
+
+ // Begin the actual test
+ val inputStream = getClass().getResourceAsStream(inputFilePath)
+ val inputStrings = io.Source.fromInputStream(inputStream).getLines().toList
+
+ import ExecutionContext.Implicits.global
+ try { // Use try-catch because error can manifest in many ways
+ // Execute for each compiler
+ val compilerResults = compilers map { compiler =>
+ // Run compiler serially once
+ val serialResult = runCompiler(inputStrings, compiler)
+ Future {
+ val threadFutures = (0 until numThreads) map { i =>
+ Future {
+ runCompiler(inputStrings, compiler) == serialResult
+ }
+ }
+ Await.result(Future.sequence(threadFutures), Duration.Inf)
+ }
+ }
+ val results = Await.result(Future.sequence(compilerResults), Duration.Inf)
+ assert(results.flatten reduce (_ && _)) // check all true (ie. success)
+ } catch {
+ case _: Throwable => fail("The Compiler is not thread safe")
+ }
+ }
+}