-
Notifications
You must be signed in to change notification settings - Fork 0
/
testing_batch.go
329 lines (264 loc) · 8.65 KB
/
testing_batch.go
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package scold
import (
"context"
"fmt"
"strings"
"sync"
"time"
)
// Verdict represents a verdict asssigned by the judge.
type Verdict int
// The set of all possible judge verdicts that can be assigned. The
// abbreviatons are due to competitive programming online judges.
const (
OK Verdict = iota
// Internal Error
IE
// Wrong Answer
WA
// Runtime Error
RE
// Time Limit
TL
)
// TLError is an error that can occur during Processer execution that
// indicates that it was prematurely killed by TestingBatch, because
// it exceeded the time limit.
const TLError StringError = "Time limit exceeded"
// TestingEventListener provides a way for users of scold to subscribe
// to the events produced by TestingBatch and to operate in a reactive fashion.
// The functions will stall the TestingBatch event loop, and thus could be not
// thread-safe.
type TestingEventListener interface {
// TestStarted will be called upon test case lounch.
TestStarted(id int)
// TestFinished is called when a test case finishes execution. Usually,
// one would like to print the test case result information.
//
// It accepts a pointer to the contents of the Test and a pointer to the
// test case result that contains id of the test, its verdict, time, and
// other useful information related to the executable's execution.
TestFinished(*Test, *TestResult)
// SuiteFinished is called when all test cases have finished.
// Accessing testingBatch at this point is safe, and no events will
// be sent to TestingEventListener after this event.
SuiteFinished(*TestingBatch)
}
// StubTestingEventListener implements TestingEventsListener, but does
// nothing. It is used when constructing TestingBatch.
type StubTestingEventListener struct{}
// TestStarted is a stub that does nothing.
func (*StubTestingEventListener) TestStarted(int) {}
// TestFinished is a stub that does nothing.
func (*StubTestingEventListener) TestFinished(*Test, *TestResult) {}
// SuiteFinished is a stub that does nothing.
func (*StubTestingEventListener) SuiteFinished(*TestingBatch) {}
// TestFinishedCallback allows to use a simple function in places where
// TestingEventLister is needed, to handle the TestFinished event.
type TestFinishedCallback func(*Test, *TestResult)
// TestStarted is a stub that does nothing.
func (cb TestFinishedCallback) TestStarted(int) {}
// TestFinished will call the underlying function with its arguments.
func (cb TestFinishedCallback) TestFinished(test *Test, result *TestResult) {
cb(test, result)
}
// SuiteFinished is a stub that does nothing
func (cb TestFinishedCallback) SuiteFinished(*TestingBatch) {}
// SpyPrinter implements TestingEventListener and is only concerned
// with storing the information received from the callbacks, so that
// its correctness can be assessed later.
type SpyPrinter struct {
StartedIDs []int
FinishedIDs []int
FinishedTests []*Test
Finished bool
}
// TestStarted amends the ID of the test to StartedIDs
func (l *SpyPrinter) TestStarted(id int) {
l.StartedIDs = append(l.StartedIDs, id)
}
// TestFinished amends the test and the ID of the test to
// FinishedTests and FinishedIDs respectively.
func (l *SpyPrinter) TestFinished(test *Test, result *TestResult) {
l.FinishedIDs = append(l.FinishedIDs, result.ID)
l.FinishedTests = append(l.FinishedTests, test)
}
// SutieFinished turn Finished true.
func (l *SpyPrinter) SuiteFinished(*TestingBatch) {
l.Finished = true
}
// TestExecutionResult carries an output of the process together with the
// corresponding test ID and a TestingBatch related error if any (panics,
// etc.).
type TestExecutionResult struct {
ID int
Err error
Out ExecutionResult
}
// TestResult encapsulates all the information TestingBatch produced for a
// particular test.
type TestResult struct {
RichOut []RichText
RichAnswer []RichText
Verdict Verdict
Time time.Duration
TestExecutionResult
}
// TestingBatch is responsible for running tests and evaluating the verdicts
// for tests. For each test case, the verdict and execution time are stored.
// It utilizes an instance of Processer to run tests, and an instance of
// Stopwatcher to track time limit. Optionally, user can set ResultPrinter
// to a custom function to output useful statistics about test case's result.
type TestingBatch struct {
inputs Inputs
complete chan TestExecutionResult
Results map[int]*TestResult
Lx *Lexer
startTimes map[int]time.Time
Proc Processer
procCancels map[int]func()
procCancelsMu sync.Mutex
ThreadPool WorkerPool
Swatch Stopwatcher
Listener TestingEventListener
}
// NewTestingBatch will initialize channels and maps inside TestingBatch and
// will assign respective dependency injections.
func NewTestingBatch(inputs Inputs, proc Processer, swatch Stopwatcher, pool WorkerPool) *TestingBatch {
return &TestingBatch{
inputs: inputs,
complete: make(chan TestExecutionResult),
Results: make(map[int]*TestResult),
Lx: &Lexer{
Precision: uint(inputs.Config.Prec),
},
startTimes: make(map[int]time.Time),
Proc: proc,
procCancels: make(map[int]func()),
ThreadPool: pool,
Swatch: swatch,
Listener: &StubTestingEventListener{},
}
}
func (b *TestingBatch) launchTest(id int, in string) {
defer func() {
if e := recover(); e != nil {
b.complete <- TestExecutionResult{
ID: id,
Err: fmt.Errorf("internal: %v", e),
Out: ExecutionResult{},
}
}
}()
ctx, cancel := context.WithCancel(context.Background())
b.procCancelsMu.Lock()
if _, exists := b.procCancels[id]; !exists {
b.procCancels[id] = cancel
} else {
b.procCancelsMu.Unlock()
cancel()
b.complete <- TestExecutionResult{
ID: id,
Err: TLError,
}
return
}
b.procCancelsMu.Unlock()
out, err := b.Proc.Run(ctx, strings.NewReader(in))
if ctx.Err() != nil {
err = TLError
}
b.complete <- TestExecutionResult{
ID: id,
Err: err,
Out: out,
}
}
func (b *TestingBatch) nextOldestRunning(previous int) int {
for id := previous + 1; id < len(b.inputs.Tests)+1; id++ {
_, finished := b.Results[id]
if !finished {
return id
}
}
return len(b.inputs.Tests) + 1
}
// Run will lauch test cases in parallel and then will wait for each test to
// finish or for the time to timelimit. When a test is finished the verdict
// and the time it took to execute are remembered. Additionally, ResultPrinter
// is called on the test case's statistics. When a time limit is reached,
// each not-yet-judged test is assigned TL verdict and the ResultPrinter is
// also called on each test.
func (b *TestingBatch) Run() {
nextTestID := 1
for ; nextTestID-1 < len(b.inputs.Tests) && nextTestID-1 < b.ThreadPool.WorkerCount(); nextTestID++ {
// Local variable is deliberate, since RunnableFunc below will capture
// variables by reference, nextTestID will be len(b.inputs.Tests)+1 when
// the worker picks up the job, and so cause panic
id := nextTestID
err := b.ThreadPool.Execute(RunnableFunc(func() {
b.launchTest(id, b.inputs.Tests[id-1].Input)
}))
if err != nil {
break
}
b.Listener.TestStarted(id)
b.startTimes[id] = b.Swatch.Now()
}
oldestRunningID := 1
for len(b.Results) != len(b.inputs.Tests) {
result := &TestResult{}
select {
case <-b.Swatch.TimeLimit(b.startTimes[oldestRunningID]):
b.procCancelsMu.Lock()
if cancel, exists := b.procCancels[oldestRunningID]; exists {
cancel()
} else {
// Notify the launchTest func not to run the thread
b.procCancels[oldestRunningID] = func() {}
}
b.procCancelsMu.Unlock()
oldestRunningID = b.nextOldestRunning(oldestRunningID)
continue
case result.TestExecutionResult = <-b.complete:
}
// A worker is now free, run another test if any
if nextTestID-1 < len(b.inputs.Tests) {
id := nextTestID
err := b.ThreadPool.Execute(RunnableFunc(func() {
b.launchTest(id, b.inputs.Tests[id-1].Input)
}))
if err == nil {
b.Listener.TestStarted(id)
b.startTimes[id] = b.Swatch.Now()
nextTestID++
}
}
id := result.ID
test := b.inputs.Tests[id-1]
result.Time = b.Swatch.Elapsed(b.startTimes[id])
answerLexemes := b.Lx.Scan(test.Output)
result.RichAnswer, _ = b.Lx.Compare(answerLexemes, nil)
if result.Err == TLError {
result.Verdict = TL
} else if result.Err != nil {
result.Verdict = IE
} else if result.Out.ExitCode != 0 {
result.Verdict = RE
} else {
got := b.Lx.Scan(result.Out.Stdout)
var okOut, okAns bool
result.RichOut, okOut = b.Lx.Compare(got, answerLexemes)
result.RichAnswer, okAns = b.Lx.Compare(answerLexemes, got)
same := okOut && okAns
if !same {
result.Verdict = WA
} else {
result.Verdict = OK
}
}
b.Results[id] = result
b.Listener.TestFinished(&b.inputs.Tests[id-1], result)
}
b.Listener.SuiteFinished(b)
}