-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathUtBotTool2.kt
More file actions
152 lines (118 loc) · 4.58 KB
/
UtBotTool2.kt
File metadata and controls
152 lines (118 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package org.utbot.runtool
import org.utbot.contest.ContestMessage
import java.io.BufferedReader
import java.io.File
import java.io.PrintStream
import java.io.PrintWriter
import java.io.StringWriter
import sbst.runtool.ITestingTool
class UtBotTool2 : ITestingTool {
override fun getExtraClassPath(): List<File> {
return mutableListOf(File("lib", "mockito-core-4.11.0.jar"))
}
lateinit var sootClasspathString: String
lateinit var classfileDir: String
var utbotProcess: Process? = null
override fun initialize(src: File, bin: File, classPath: List<File>) {
classfileDir = bin.absolutePath
sootClasspathString =
classfileDir + File.pathSeparator + classPath.joinToString(separator = File.pathSeparator) { it.absolutePath }
tempDir.listFiles { _, name -> name.endsWith(".log") }?.forEach { it.delete() }
restartProcess()
while (true) {
val line = inputChannel.readLine()
if (line == null) {
log("INIT FAILED, INPUT LINE IS <NULL>")
break
}
logUtbotOut(line)
if (line.startsWith("${ContestMessage.INIT}"))
break
}
}
lateinit var inputChannel: BufferedReader
lateinit var outputChannel: PrintStream
var restartOrdinal = 0
val homeDir = File(".").canonicalFile
val tempDir = File(homeDir, "temp")
private fun log(msg: String, ex: Throwable? = null) {
val logfile = File(tempDir, "utbot-runtool.log")
logfile.appendText(msg + "\n")
val sw = StringWriter()
ex?.printStackTrace(PrintWriter(sw, true))
logfile.appendText(sw.toString() + "\n")
}
private fun logUtbotOut(msg: String) {
val logfile = File(tempDir, "utbot-${restartOrdinal}-out.log")
logfile.appendText(msg + "\n")
}
private fun restartProcess() {
restartOrdinal++
val prevProcess = utbotProcess
try {
if (prevProcess?.isAlive == true) {
prevProcess.destroyForcibly()
}
} catch (e: Throwable) {
log("FAILED TO TERMINATED PREVIOUS UTBOT", e)
}
//TODO I think we have some strange problems with z3, this addition must absent
val additionalUnpackedZ3 = File("utbot-framework/build/resources/main").run {
if (isDirectory && exists())
File.pathSeparator + absolutePath
else
""
}
val utBotAppClasspath = System.getProperty("java.class.path") +
File.pathSeparator + sootClasspathString +
additionalUnpackedZ3
val generatedTestsOutputDir = File(tempDir, "testcases")
val errorFile = File(tempDir, "utbot-${restartOrdinal}-err.log")
errorFile.parentFile.mkdirs()
val pb = ProcessBuilder(
listOfNotNull(
"${System.getenv("JAVA_HOME")}/bin/java",
System.getenv("UTBOT_EXTRA_PARAMS"),
"-cp",
utBotAppClasspath,
"org.utbot.contest.ContestKt",
classfileDir,
sootClasspathString,
generatedTestsOutputDir.canonicalPath
)
).directory(homeDir).redirectError(errorFile)
val newProcess = pb.start()
log("Started process (ordinal=$restartOrdinal): \n\t" + pb.command().joinToString("\n\t"))
utbotProcess = newProcess
inputChannel = newProcess.inputStream.reader().buffered()
outputChannel = PrintStream(newProcess.outputStream, true)
}
override fun run(cName: String, timeBudget: Long) {
val start = System.currentTimeMillis()
try {
log("Started RUN $cName $timeBudget")
if (utbotProcess?.isAlive != true) {
restartProcess()
}
outputChannel.println("${ContestMessage.RUN} $cName $timeBudget")
while (true) {
val line = inputChannel.readLine()
if (line == null) {
log("INPUT FROM UTBOT IS NULL, RESTARTING UTBOT")
restartProcess()
break
}
logUtbotOut(line)
if (line.startsWith("${ContestMessage.READY}"))
break
else
continue
}
} catch (e: Throwable) {
log("ERROR HAPPENED, RESTARTING UTBOT", e)
restartProcess()
} finally {
log("Finished RUN (elapsed ${System.currentTimeMillis() - start} ms) $cName")
}
}
}