Skip to content

Commit

Permalink
[tests] Update the testing API in order to allow multiple "on Initial…
Browse files Browse the repository at this point in the history
…ize".

Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Mar 2, 2020
1 parent 2bd7e71 commit 4b89ec1
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 180 deletions.
Expand Up @@ -95,8 +95,6 @@ class Bug934Test {
defaultSpace.spaceID.assertNotEquals(otherSpace2.spaceID)
defaultSpace.spaceID.ID.assertNotEquals(otherSpace2.spaceID.ID)
otherSpace1.assertSame(otherSpace2)

forgetTheKernel
}

@Test
Expand Down Expand Up @@ -132,8 +130,6 @@ class Bug934Test {
otherSpace1.assertNotSame(otherSpace2)
otherSpace1.spaceID.assertNotEquals(otherSpace2.spaceID)
otherSpace1.spaceID.ID.assertNotEquals(otherSpace2.spaceID.ID)

forgetTheKernel
}

}
Expand Up @@ -92,9 +92,6 @@ class Bug942Test {

^space.spaceID.assertEquals(d1.address.spaceID)
^space.spaceID.assertEquals(d2.address.spaceID)

forgetTheKernel()

}

}
Expand Up @@ -21,8 +21,6 @@

package io.sarl.sre.tests.runtime.internal.eventguard.mocks

import io.sarl.sre.tests.testutils.agents.TestingAgent.RunPolicy

agent Agent1 extends Agent0 {

override myfunction : void {
Expand Down
Expand Up @@ -30,6 +30,7 @@ import java.util.Collection
import java.util.List
import java.util.Map
import java.util.UUID
import java.util.concurrent.CopyOnWriteArrayList
import org.eclipse.xtend.lib.annotations.Accessors

/**
Expand All @@ -45,7 +46,9 @@ abstract agent TestingAgent {

uses Lifecycle, Schedules

var results : Map<UUID, List<Object>>
var results : List<Object>

var globalResults : Map<UUID, List<Object>>

var initializationParameters : Object[]

Expand All @@ -54,7 +57,7 @@ abstract agent TestingAgent {

/** Replies the simple name of the given type name
*
* @param typeName the fully qualifed name of a type.
* @param typeName the fully qualified name of a type.
*/
static def simpleName(typeName : String) : String {
var index1 = typeName.lastIndexOf("$")
Expand All @@ -72,22 +75,8 @@ abstract agent TestingAgent {
* @param result - the result.
*/
def addResult(result : Object) {
if (this.results !== null) {
var list : List<Object>
synchronized (this.results) {
val aid = this.ID
list = this.results.get(aid)
if (list === null) {
list = newArrayList
this.results.put(aid, list)
}
}
if (list !== null) {
synchronized (list) {
list += result
}
}
}
var r = getModifiableResults
r += result
}


Expand All @@ -98,18 +87,8 @@ abstract agent TestingAgent {
*/
@Pure
def getNumberOfResults : int {
if (this.results !== null) {
var list : List<Object>
synchronized (this.results) {
list = this.results.get(this.ID)
}
if (list !== null) {
synchronized (list) {
return list.size
}
}
}
return 0
var r = getModifiableResults
return r.size
}

/**
Expand All @@ -118,21 +97,8 @@ abstract agent TestingAgent {
* @param result - the result.
*/
def addResults(results : Collection<?>) {
if (this.results !== null) {
var list : List<Object>
synchronized (this.results) {
list = this.results.get(this.ID)
if (list !== null) {
list = newArrayList
this.results.put(this.ID, list)
}
}
if (list !== null) {
synchronized (list) {
list += results
}
}
}
var r = getModifiableResults
r.addAll(results)
}

/**
Expand All @@ -141,18 +107,8 @@ abstract agent TestingAgent {
*/
@Pure
def getUnmodifiableResults : List<Object> {
if (this.results !== null) {
var list : List<Object>
synchronized (this.results) {
list = this.results.get(this.ID)
}
if (list !== null) {
synchronized (list) {
return list.unmodifiableView
}
}
}
return emptyList
var r = getModifiableResults
return r.unmodifiableView
}

/**
Expand All @@ -161,27 +117,33 @@ abstract agent TestingAgent {
*/
@Pure
def getModifiableResults : List<Object> {
if (this.results !== null) {
var list : List<Object>
synchronized (this.results) {
list = this.results.get(this.ID)
if (list === null) {
list = newArrayList
this.results.put(this.ID, list)
}
}
return list
}
return emptyList
return synchronizeResults
}

/**
* Replies the map of the results for all the agents.
/**
* Replies a modifiable view on the results for the current agent.
* @return the results.
*/
@Pure
def getRawResultMap : Map<UUID, List<Object>> {
this.results
private def synchronizeResults : List<Object> {
synchronized (this) {
if (this.globalResults !== null) {
var list = this.globalResults.get(ID)
if (list === null) {
list = new CopyOnWriteArrayList
this.globalResults.put(ID, list)
}
if (this.results !== null) {
list.addAll(this.results)
this.results = null
}
return list
} else {
if (this.results === null) {
this.results = new CopyOnWriteArrayList
}
return this.results
}
}
}

/** Replies the initialization parameters of the agents.
Expand All @@ -201,15 +163,17 @@ abstract agent TestingAgent {
*/
@Pure
def buildAgentInitializationParameters(values : Object*) : Object[] {
if (values === null || values.length == 0) {
return #[rawResultMap]
synchronized (this) {
if (values === null || values.length == 0) {
return #[this.globalResults]
}
var l = newArrayList()
var ll : List<Object> = values
l.add(this.globalResults)
l.addAll(ll)
var t : Object[] = l
return t
}
var l = newArrayList()
var ll : List<Object> = values
l.add(rawResultMap)
l.addAll(ll)
var t : Object[] = l
return t
}

/** Wait until the condition is true or time out; and run the code.
Expand Down Expand Up @@ -238,9 +202,13 @@ abstract agent TestingAgent {
}

on Initialize {
this.initializationParameters = occurrence.parameters
this.results = occurrence.parameters.get(0) as Map<UUID, List<Object>>
this.spawner = occurrence.spawner
synchronized (this) {
this.initializationParameters = occurrence.parameters
this.globalResults = occurrence.parameters.get(0) as Map<UUID, List<Object>>
// Fill up the buffered results
synchronizeResults
this.spawner = occurrence.spawner
}
try {
var policy = runAgentTest
if (policy !== null) {
Expand All @@ -264,9 +232,9 @@ abstract agent TestingAgent {
}
}

/**
* Invoked to run the unit test. This function is invoked at agent initialization
*
/**
* Invoked to run the unit test. This function is invoked at agent initialization.
*
* @return <code>true</code> for killing the agent during its initialization.
*/
abstract def runAgentTest : RunPolicy
Expand Down

0 comments on commit 4b89ec1

Please sign in to comment.