Skip to content

Commit a3d3d4b

Browse files
committed
Leverage Scala and its idioms
1 parent 47abcd0 commit a3d3d4b

File tree

5 files changed

+84
-110
lines changed

5 files changed

+84
-110
lines changed

compiler/test/dotty/tools/vulpix/ChildJVMMain.java

Lines changed: 0 additions & 67 deletions
This file was deleted.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package dotty.tools.vulpix
2+
3+
import java.io.{BufferedReader, File, InputStreamReader}
4+
import java.net.{URL, URLClassLoader}
5+
import java.util.ArrayList
6+
7+
object ChildJVMMain:
8+
val MessageStart = "##THIS IS THE START FOR ME, HELLO##"
9+
val MessageEnd = "##THIS IS THE END FOR ME, GOODBYE##"
10+
11+
def runMain(dir: String): Unit =
12+
def meth =
13+
val jcp = System.getProperty("java.class.path")
14+
val sep = File.pathSeparator
15+
System.setProperty("java.class.path", if jcp == null then dir else dir + sep + jcp)
16+
17+
val loader =
18+
val cp = ArrayList[URL]()
19+
val paths = dir.split(sep)
20+
for path <- paths do
21+
cp.add(File(path).toURI().toURL())
22+
val urls = cp.toArray(Array.ofDim[URL](cp.size))
23+
URLClassLoader(urls)
24+
25+
val cls = loader.loadClass("Test")
26+
cls.getMethod("main", classOf[Array[String]])
27+
end meth
28+
val m =
29+
try meth
30+
catch t =>
31+
// Include the failure stack trace to the test output
32+
System.out.println(MessageStart)
33+
t.printStackTrace()
34+
throw t
35+
System.out.println(MessageStart);
36+
m.invoke(null, null)
37+
38+
def main(args: Array[String]): Unit =
39+
inline def savingSystem[T](inline body: => T): T =
40+
val savedIn = System.in
41+
val savedOut = System.out
42+
val savedErr = System.err
43+
try body
44+
finally
45+
System.setIn(savedIn)
46+
System.setOut(savedOut)
47+
System.setErr(savedErr)
48+
val stdin = BufferedReader(InputStreamReader(System.in))
49+
while true do
50+
savingSystem:
51+
runMain(stdin.readLine())
52+
println(MessageEnd)

compiler/test/dotty/tools/vulpix/ParallelTesting.scala

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,21 +1292,19 @@ trait ParallelTesting extends RunnerOrchestration:
12921292
this
12931293
}
12941294

1295-
/** Extract `Failure` set and render from `Test` */
1296-
private def reasonsForFailure(test: Test): String = {
1297-
val failureReport =
1298-
if test.failureCount == 0 then ""
1299-
else s"encountered ${test.failureCount} test failure(s):\n"
1300-
1301-
failureReport + test.failureReasons.collect {
1302-
case test.TimeoutFailure(title) =>
1303-
s" - test '$title' timed out"
1304-
case test.JavaCompilationFailure(msg) =>
1305-
s" - java compilation failed with:\n${ msg.linesIterator.map(" " + _).mkString("\n") }"
1306-
case test.Generic =>
1307-
" - generic failure (see test output)"
1308-
}.mkString("\n")
1309-
}
1295+
/** Extracts `Failure` set and renders from `Test`. */
1296+
private def reasonsForFailure(test: Test): String =
1297+
if test.failureCount == 0 then ""
1298+
else
1299+
test.failureReasons.collect {
1300+
case test.TimeoutFailure(title) =>
1301+
s" - test '$title' timed out"
1302+
case test.JavaCompilationFailure(msg) =>
1303+
val header = " - java compilation failed with:\n"
1304+
msg.linesIterator.map(" " + _).mkString(header, "\n", "")
1305+
case test.Generic =>
1306+
" - generic failure (see test output)"
1307+
}.mkString(s"encountered ${test.failureCount} test failure(s):\n", "\n", "")
13101308

13111309
/** Copies `file` to `dir` - taking into account if `file` is a directory,
13121310
* and if so copying recursively

compiler/test/dotty/tools/vulpix/RunnerOrchestration.scala

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -98,40 +98,35 @@ trait RunnerOrchestration {
9898

9999
def readLine(): String =
100100
stdout.readLine() match
101-
case s"Listening for transport dt_socket at address: $port" =>
102-
throw new IOException(
103-
s"Unexpected transport dt_socket message." +
104-
" The port is going to be lost and no debugger will be able to connect."
105-
)
106-
case line => line
101+
case s"Listening for transport dt_socket at address: $port" =>
102+
throw IOException(
103+
"Unexpected transport dt_socket message." +
104+
" The port is going to be lost and no debugger will be able to connect."
105+
)
106+
case line => line
107107

108108
def printLine(line: String): Unit = stdin.println(line)
109109

110110
def getJdiPort(): Int =
111111
stdout.readLine() match
112-
case s"Listening for transport dt_socket at address: $port" => port.toInt
113-
case line => throw new IOException(s"Failed getting JDI port of child JVM: got $line")
112+
case s"Listening for transport dt_socket at address: $port" => port.toInt
113+
case line => throw IOException(s"Failed getting JDI port of child JVM: got $line")
114114

115-
export p.{exitValue, isAlive, destroy}
115+
def isAlive: Boolean = p.isAlive // export p.isAlive sans parens
116+
117+
export p.{exitValue, destroy}
116118
end RunnerProcess
117119

118120
private class Runner(private var process: RunnerProcess):
119-
/** Checks if `process` is still alive
120-
*
121-
* When `process.exitValue()` is called on an active process the caught
122-
* exception is thrown. As such we can know if the subprocess exited or
123-
* not.
124-
*/
125-
def isAlive: Boolean =
126-
try { process.exitValue(); false }
127-
catch case _: IllegalThreadStateException => true
128-
129-
/** Destroys the underlying process and kills IO streams */
121+
/** Checks whether the underlying process is still alive. */
122+
def isAlive: Boolean = process.isAlive
123+
124+
/** Destroys the underlying process and kills IO streams. */
130125
def kill(): Unit =
131126
if (process ne null) process.destroy()
132127
process = null
133128

134-
/** Blocks less than `maxDuration` while running `Test.main` from `dir` */
129+
/** Blocks less than `maxDuration` while running `Test.main` from `dir`. */
135130
def runMain(classPath: String): Status =
136131
assert(process ne null, "Runner was killed and then reused without setting a new process")
137132
awaitStatusOrRespawn(startMain(classPath))
@@ -172,7 +167,7 @@ trait RunnerOrchestration {
172167
sb.append(childOutput).append(System.lineSeparator)
173168
childOutput = process.readLine()
174169

175-
if process.isAlive() && childOutput != null then Success(sb.toString)
170+
if isAlive && childOutput != null then Success(sb.toString)
176171
else Failure(sb.toString)
177172
end startMain
178173

@@ -198,7 +193,7 @@ trait RunnerOrchestration {
198193
* scala library.
199194
*/
200195
private def createProcess(): RunnerProcess =
201-
val url = classOf[ChildJVMMain].getProtectionDomain.getCodeSource.getLocation
196+
val url = classOf[ChildJVMMain.type].getProtectionDomain.getCodeSource.getLocation
202197
val cp = Paths.get(url.toURI).toString + JFile.pathSeparator + Properties.scalaLibrary
203198
val javaBin = Paths.get(sys.props("java.home"), "bin", "java").toString
204199
val args = Seq("-Dfile.encoding=UTF-8", "-Duser.language=en", "-Duser.country=US", "-Xmx1g", "-cp", cp) ++
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
object Test {
2-
def main(args: Array[String]): Unit = {
3-
Thread.sleep(10 * 1000)
4-
}
5-
}
1+
@main def Test = Thread.sleep(1000 * 1000)

0 commit comments

Comments
 (0)