/
SreRunContext.sarl
497 lines (460 loc) · 14.2 KB
/
SreRunContext.sarl
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/*
* $Id$
*
* SARL is an general-purpose agent programming language.
* More details on http://www.sarl.io
*
* Copyright (C) 2014-2020 the original authors or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.sarl.sre.tests.testutils.^extension
import com.google.inject.Module
import io.sarl.lang.annotation.PrivateAPI
import io.sarl.sre.Kernel
import io.sarl.sre.boot.SreMain
import io.sarl.sre.boot.configs.subconfigs.BootConfig
import io.sarl.sre.services.logging.LoggingService
import io.sarl.sre.tests.testutils.agents.TestingAgent
import io.sarl.sre.tests.testutils.exception.SreTimeoutException
import java.util.HashMap
import java.util.List
import java.util.Map
import java.util.UUID
import java.util.concurrent.TimeoutException
import java.util.logging.Handler
import java.util.logging.Level
import java.util.logging.LogRecord
import org.arakhne.afc.bootique.log4j.configs.Log4jIntegrationConfig
import org.arakhne.afc.bootique.variables.VariableNames
import org.eclipse.xtend.lib.annotations.Accessors
import org.opentest4j.AssertionFailedError
import static io.sarl.sre.tests.testutils.Constants.STANDARD_TIMEOUT
import static org.junit.jupiter.api.Assertions.*
import static extension org.junit.jupiter.api.Assertions.assertEquals
import io.sarl.lang.core.Agent
/**
* Context for running the SRE.
*
* @author $Author: sgalland$
* @version $FullVersion$
* @mavengroupid $GroupId$
* @mavenartifactid $ArtifactId$
* @since 0.11
*/
class SreRunContext {
/** The logging level when logging is enable during tests.
*/
public static val TEST_LOGGING_LEVEL = Level::ALL
/**
* Reference to the instance of the SRE kernel.
*/
@Accessors(PUBLIC_GETTER)
var sreKernel : Kernel
/**
* Reference to the instance of the SRE bootstrap.
*/
@Accessors(PUBLIC_GETTER)
var bootstrap : SreMain
var results = new HashMap<UUID, List<Object>>
var module : Class<? extends Module>
/** Replies the identifier of the lastly booted agent.
*
* @return the identifier of the agent, or {@code null} if no agent was booted.
*/
def getBootAgent : UUID {
this.bootstrap.bootAgentIdentifier
}
/**
* Replies result at the given index of the run of the agent.
*
* @param source - the source of the data.
* @param type - the type of the result.
* @param index - the index of the result.
* @return the value; or {@code null} if no result.
*/
def get(source : List<?>, type : Class<T>, index : int) : T with T {
var element = source.get(index);
if (element === null || type.isInstance(element)) {
return type.cast(element);
}
return null
}
/**
* Replies result at the given index of the run of the agent.
*
* @param agentId the identifier of the agent.
* @param type - the type of the result.
* @param index - the index of the result.
* @return the value; or {@code null} if no result.
*/
def getResult(agentId : UUID = null, type : Class<T>, index : int) : T with T {
if (this.results !== null) {
try {
var res : List<Object>
synchronized (this.results) {
res = this.results.get(agentId ?: bootAgent)
}
if (res !== null) {
synchronized (res) {
return type.cast(res.get(index))
}
}
} catch (exception : Throwable) {
//
}
}
return null
}
/** Replies the number of results that are provided by the agent.
*
* @return the number of results.
*/
def getNumberOfResults(agentId : UUID = null) : int {
if (this.results !== null) {
var list : List<Object>
synchronized (this.results) {
list = this.results.get(agentId ?: bootAgent)
}
if (list !== null) {
synchronized (list) {
return list.size
}
}
}
return 0
}
/**
* Test if the number of results provided by the SRE platform is equal to the given number.
*
* @param expected - the expected number of results.
*/
def assertNumberOfResults(expected : int) {
expected.assertEquals(getNumberOfResults)["Invalid number of results provided by the platform."]
}
/**
* Replies result for the boot agent or the agent with the given identifier.
*
* @param agentId the identifier of the agent for which the results must be retrieved.
* If {@code null} or not provided, the results associated to the boot agent are
* replied.
* @return the results.
*/
def getResults(agentId : UUID = null) : List<Object> {
if (this.results !== null) {
var res : List<Object>
synchronized (this.results) {
res = this.results.get(agentId ?: bootAgent)
}
if (res !== null) {
synchronized (res) {
return res.unmodifiableView
}
}
}
return emptyList
}
/**
* Add a value to the list of results.
*
* @param agentId the identifier of the agent for which the results must be retrieved.
* If {@code null} or not provided, the results associated to the boot agent are
* replied.
* @param value the new value.
*/
def addResult(agentId : UUID = null, value : Object) : void {
if (this.results !== null) {
var res : List<Object>
synchronized (this.results) {
val id = agentId ?: bootAgent
res = this.results.get(id)
if (res === null) {
res = newArrayList
this.results.put(id, res)
}
}
res += value
}
}
/**
* Replies all the results for all the agents.
* @return the results.
*/
def getAllResultsPerAgent() : Map<UUID, List<Object>> {
if (this.results !== null) {
return results
}
return emptyMap
}
/**
* Replies all the results for all the agents.
* @return the results.
*/
def getAllResults() : List<Object> {
if (this.results !== null) {
var all = newArrayList
synchronized (this.results) {
for (values : this.results.values) {
all += values
}
}
return all
}
return emptyList
}
/**
* Replies the initialization parameters for the agents.
* @return the parameters.
*/
def getAgentInitializationParameters : Object[] {
#[this.results]
}
/**
* Replies the index of the first result of the given type starting at the given index.
*
* @param agentId the identifier of the agent.
* @param type - the type of the result.
* @param fromIndex - the start index.
* @return the index; or <code>-1</code> if not found.
*/
def indexOfResult(agentId : UUID = null, type : Class<?>, fromIndex : int = 0) : int {
if (this.results !== null) {
try {
var res : List<Object>
synchronized (this.results) {
res = this.results.get(agentId ?: bootAgent)
}
if (res !== null) {
synchronized (res) {
for (var i = fromIndex; i < res.size; i++) {
var r = res.get(i)
if (type.isInstance(r)) {
return i
}
}
}
}
} catch (exception : Throwable) {
//
}
}
return -1
}
/**
* Start the SRE platform.
*
* @param type - the type of the agent to launch at start-up.
* @param loggingLevel indicates the level of logging.
* @param trackLogErrors indicates if the logged errors should be tracked.
* @param deleteLogHandlers indicates if the loggers' handler should be removed.
* @param timeout - the maximum waiting time in seconds, or <code>-1</code> to ignore the timeout.
* See {@link #STANDARD_TIMEOUT}, {@link #EXTRA_TIMEOUT} or {@link #NO_TIMEOUT}.
* @return the kernel.
* @throws Exception - if the kernel cannot be launched.
*/
@SuppressWarnings("use_reserved_sarl_annotation")
@PrivateAPI(isCallerOnly = true)
def runSre(type : Class<? extends TestingAgent>, loggingLevel : Level = TEST_LOGGING_LEVEL,
trackLogErrors : boolean = true, deleteLogHandlers : boolean = true,
timeout : int = STANDARD_TIMEOUT) : Kernel {
val kern = setupTheSreKernel(type, loggingLevel, trackLogErrors, deleteLogHandlers)
try {
waitForTheKernel(timeout)
} catch (exception : SreTimeoutException | TimeoutException) {
Kernel::executeKernelStopWhenNoAgentAlive(kern)
throw exception
}
return kern
}
/** Replies the module that should be injected in order to proceed tests.
*
* @return the module, never {@code null}.
*/
def getTestingModule : Class<? extends Module> {
this.module
}
/** Replies the module that should be injected in order to proceed tests.
*
* @param module the module, never {@code null}.
*/
def setTestingModule(module : Class<? extends Module>) {
assert module !== null
this.module = module
}
/** Assert that the given kernel has no error on its logs.
*
* @param kern the kernel.
*/
def assertNoErrorLog(kern : Kernel) {
for (obj : getResults(null)) {
if (obj instanceof LogRecord) {
throw new AssertionFailedError("Unexpected error log", "", obj.toString)
}
}
}
/** Assert that not exception is inside the results of the test run.
*/
def assertNoExceptionInResults {
var list = newArrayList
for (obj : allResults) {
if (obj instanceof Throwable) {
list += obj
}
}
if (!list.empty) {
// Only the first is logged
throw list.get(0)
}
}
/**
* Set-up the SRE platform.
*
* @param type - the type of the agent to launch at start-up.
* @param loggingLevel - indicates the level of logging.
* @param trackLogErrors indicates if the logged errors should be tracked.
* @param deleteLogHandlers indicates if the loggers' handler should be removed.
* @return the kernel.
* @throws Exception - if the kernel cannot be launched.
*/
def setupTheSreKernel(type : Class<? extends TestingAgent>, loggingLevel : Level = TEST_LOGGING_LEVEL,
trackLogErrors : boolean, deleteLogHandlers : boolean) : Kernel {
System::setProperty(VariableNames::toPropertyName(BootConfig::BOOT_AGENT_NAME), type.name)
var k = setupTheSreKernel(loggingLevel, trackLogErrors, deleteLogHandlers)
this.bootstrap.startAgent(type, getAgentInitializationParameters)
return k
}
/**
* Set-up the SRE platform.
*
* @param loggingLevel indicates the level of logging.
* @param trackLogErrors indicates if the logged errors should be tracked.
* @param deleteLogHandlers indicates if the loggers' handler should be removed.
* @return the kernel.
* @throws Exception - if the kernel cannot be launched.
*/
def setupTheSreKernel(loggingLevel : Level = TEST_LOGGING_LEVEL, trackLogErrors : boolean,
deleteLogHandlers : boolean) : Kernel {
assertNull(this.sreKernel, "SRE already launched.")
val lvl = loggingLevel ?: Level::OFF
val glvl = org.arakhne.afc.bootique.log4j.configs.Level::valueOf(lvl)
System::setProperty(VariableNames::toPropertyName(Log4jIntegrationConfig::LEVEL), glvl.name)
val module = getTestingModule
this.results = newHashMap
this.bootstrap = new SreMain
this.bootstrap.startWithoutAgent(module)
this.sreKernel = this.bootstrap.kernel
val logger = this.sreKernel.getService(typeof(LoggingService)).platformLogger
if (deleteLogHandlers) {
for (handler : logger.handlers) {
logger.removeHandler(handler)
}
}
if (trackLogErrors) {
logger.addHandler(new Handler {
override publish(record : LogRecord) {
if (record.level === Level.SEVERE) {
addResult(record)
}
}
override flush {
}
override close {
}
})
} else if (deleteLogHandlers) {
logger.addHandler(new Handler {
override publish(record : LogRecord) {
}
override flush {
}
override close {
}
})
}
return this.sreKernel
}
/**
* Wait for the end of the SRE platform.
*
* @param timeout - the maximum waiting time in seconds, or <code>-1</code> to ignore the timeout.
* See {@link #STANDARD_TIMEOUT}, {@link #EXTRA_TIMEOUT} or {@link #NO_TIMEOUT}.
* @throws Exception - if the kernel cannot be launched.
*/
def waitForTheKernel(timeout : int) {
waitForTheKernel(timeout, null)
}
/**
* Wait for the end of the SRE platform.
*
* @param timeout - the maximum waiting time in seconds, or <code>-1</code> to ignore the timeout.
* See {@link #STANDARD_TIMEOUT}, {@link #EXTRA_TIMEOUT} or {@link #NO_TIMEOUT}.
* @param predicate the predicate to use as stop condition.
* @throws Exception - if the kernel cannot be launched.
*/
@SuppressWarnings("discouraged_reference")
def waitForTheKernel(timeout : int, predicate : (Map<UUID, List<Object>>)=>boolean) {
var endTime : long
if (timeout >= 0) {
endTime = System::currentTimeMillis() + timeout.seconds
} else {
endTime = -1
}
var isSreRunning = this.sreKernel.running
while (isSreRunning && (endTime == -1 || System.currentTimeMillis() <= endTime)) {
isSreRunning = this.sreKernel.isRunning() || (predicate !== null && (!(predicate.apply(this.results))))
Thread.sleep(100)
}
if (isSreRunning) {
var cause : Throwable = null
for (result : allResults) {
if (result instanceof Throwable) {
cause = result
break
}
}
if (cause !== null) {
throw new SreTimeoutException(cause)
}
throw new SreTimeoutException
}
}
/**
* Forget any reference to a Janus kernel.
*/
def forgetTheKernel : void {
this.sreKernel = null;
}
/**
* Spawn an agent of the given type, and pass the parameters replied by
* {@link #getAgentInitializationParameters()} and the given parameters to its initialization function.
*
* @param kernel the kernel.
* @param agent the type of the agent to spawn.
* @param callback the function invoked each time an agent is spawned.
* @param params the list of the parameters to pass to the agent initialization function.
* @return the identifier of the agent, never {@code null}.
*/
def spawnInTest(kernel : Kernel, ^agent : Class<? extends Agent>, callback : (UUID)=>void, arguments : Object*) {
var allParameters : Object[]
if (arguments === null || arguments.length === 0) {
allParameters = agentInitializationParameters
} else {
var ags = agentInitializationParameters
var tab = newArrayOfSize(ags.length + arguments.length)
System::arraycopy(ags, 0, tab, 0, ags.length)
System::arraycopy(arguments, 0, tab, ags.length, arguments.length)
allParameters = tab
}
kernel.spawn(^agent, callback, allParameters)
}
}