Skip to content

Commit

Permalink
[sre] Fixing the invalid emitting of ParticipantJoined.
Browse files Browse the repository at this point in the history
Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Mar 1, 2020
1 parent dd0cd89 commit 97a982c
Show file tree
Hide file tree
Showing 9 changed files with 182 additions and 29 deletions.
Expand Up @@ -475,7 +475,7 @@ final event ContextLeft {

}

/** Notifies other participants the member with agentID has effectively
/** Notifies other participants that a member has effectively
* integrated the space.
* The event is fired within the default space of the context in which the agent entered a new space.
* The joining agent does not receive this event.
Expand Down
Expand Up @@ -144,9 +144,13 @@ class SpaceParticipantEventEmitter implements SpaceParticipantListener {
// Send the event in the default space of the context.
if (this.defaultSpace !== null) {
var participantAddr = participant.address
var addr = new Address(this.defaultSpace.spaceID, participantAddr.UUID)
// The first parameter is null because the event has an address.
this.defaultSpace.emit(null, new ParticipantJoined(addr, participantAddr.spaceID))
val participantId = participantAddr.UUID
var addr = new Address(this.defaultSpace.spaceID, participantId)
// a) The first parameter is null because the event has an address.
// b) The scope is defined for avoiding the participant to receive the event.
this.defaultSpace.emit(null,
new ParticipantJoined(addr, participantAddr.spaceID)
) [participantId != it.UUID]
}
}

Expand Down
Expand Up @@ -22,8 +22,11 @@
package io.sarl.sre.tests.runtime.internal

import io.sarl.core.ParticipantJoined
import io.sarl.lang.core.SpaceID
import io.sarl.sre.tests.runtime.internal.mocks.JoinerAgent0
import io.sarl.sre.tests.runtime.internal.mocks.JoinerAgent2
import io.sarl.sre.tests.runtime.internal.mocks.SpyAgent0
import io.sarl.sre.tests.runtime.internal.mocks.SpyAgent2
import io.sarl.sre.tests.testutils.^extension.PropertyRestoreExtension
import io.sarl.sre.tests.testutils.^extension.SreRunContext
import io.sarl.sre.tests.testutils.^extension.SreRunExtension
Expand All @@ -41,7 +44,6 @@ import static io.sarl.sre.tests.testutils.Constants.*
import static extension org.junit.jupiter.api.Assertions.assertEquals
import static extension org.junit.jupiter.api.Assertions.assertNotEquals
import static extension org.junit.jupiter.api.Assertions.assertNotNull
import org.junit.jupiter.api.Disabled

/**
* @author $Author: sgalland$
Expand All @@ -60,30 +62,60 @@ import org.junit.jupiter.api.Disabled
class OnParticipantJoinedTest {

@Test
@Disabled("tmp")
@DisplayName("ParticipantJoined in default space")
@SuppressWarnings("discouraged_reference")
def run(extension rc : SreRunContext) {
var kern = typeof(SpyAgent0).setupTheSreKernel(null, true, true)
def runParticipantJoinedInDefaultSpace(extension rc : SreRunContext) {
var kern = typeof(SpyAgent0).setupTheSreKernel(true, true)

Thread::sleep(1000)

val id : OutputParameter<UUID> = new OutputParameter

kern.spawn(typeof(JoinerAgent0)) [
kern.spawnInTest(typeof(JoinerAgent0), [
id.set(it)
]
])

waitForTheKernel(STANDARD_TIMEOUT)

var evt = getResult(bootAgent, typeof(ParticipantJoined), 0)
evt.assertNotNull

var spaceId = getResult(id.get, typeof(UUID), 0)
spaceId.assertNotNull

bootAgent.assertEquals(evt.source.UUID)
spaceId.assertNotEquals(evt.source.spaceID.ID)
spaceId.assertEquals(evt.spaceID.ID)

var spcid = getResult(bootAgent, typeof(SpaceID), 1)
spcid.assertNotNull

id.get.assertEquals(evt.source.UUID)
spcid.assertEquals(evt.spaceID)
}

@Test
@DisplayName("ParticipantJoined in other space")
@SuppressWarnings("discouraged_reference")
def runParticipantJoinedInOtherSpace(extension rc : SreRunContext) {
var kern = typeof(SpyAgent2).setupTheSreKernel(true, true)

Thread::sleep(1000)

val id : OutputParameter<UUID> = new OutputParameter

kern.spawnInTest(typeof(JoinerAgent2), [
id.set(it)
])

waitForTheKernel(STANDARD_TIMEOUT)

var evt = getResult(bootAgent, typeof(ParticipantJoined), 0)
evt.assertNotNull

var spcid0 = getResult(bootAgent, typeof(SpaceID), 1)
spcid0.assertNotNull

var spcid1 = getResult(id.get, typeof(SpaceID), 0)
spcid1.assertNotNull

spcid0.assertNotEquals(spcid1)

id.get.assertEquals(evt.source.UUID)
spcid1.assertEquals(evt.spaceID)
}

}
Expand Up @@ -21,11 +21,7 @@

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

import io.sarl.core.Behaviors
import io.sarl.core.DefaultContextInteractions
import io.sarl.core.OpenEventSpaceSpecification
import io.sarl.sre.tests.testutils.agents.TestingAgent
import java.util.UUID

/**
* @author $Author: sgalland$
Expand All @@ -35,13 +31,7 @@ import java.util.UUID
*/
agent JoinerAgent0 extends TestingAgent {

uses DefaultContextInteractions, Behaviors

override runAgentTest : RunPolicy {
var id = UUID::randomUUID
var spc = defaultContext.getOrCreateSpaceWithSpec(typeof(OpenEventSpaceSpecification), id)
spc.register(asEventListener)
addResult(id)
return RunPolicy::DIFFERED_KILLING
}

Expand Down
@@ -0,0 +1,48 @@
/*
* $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.runtime.internal.mocks

import io.sarl.sre.tests.testutils.agents.TestingAgent
import java.util.UUID
import io.sarl.core.DefaultContextInteractions
import io.sarl.core.OpenEventSpaceSpecification
import io.sarl.core.Behaviors

/**
* @author $Author: sgalland$
* @version $FullVersion$
* @mavengroupid $GroupId$
* @mavenartifactid $ArtifactId$
*/
agent JoinerAgent2 extends TestingAgent {

uses DefaultContextInteractions, Behaviors

override runAgentTest : RunPolicy {
var id = UUID::randomUUID
val spc = defaultContext.getOrCreateSpaceWithSpec(typeof(OpenEventSpaceSpecification), id)
spc.register(asEventListener)
addResult(spc.spaceID)
return RunPolicy::DIFFERED_KILLING
}

}
Expand Up @@ -24,6 +24,7 @@ package io.sarl.sre.tests.runtime.internal.mocks
import io.sarl.core.Lifecycle
import io.sarl.core.ParticipantJoined
import io.sarl.sre.tests.testutils.agents.TestingAgent
import io.sarl.core.DefaultContextInteractions

/**
* @author $Author: sgalland$
Expand All @@ -33,14 +34,15 @@ import io.sarl.sre.tests.testutils.agents.TestingAgent
*/
agent SpyAgent0 extends TestingAgent {

uses Lifecycle
uses Lifecycle, DefaultContextInteractions

override runAgentTest : RunPolicy {
return null
}

on ParticipantJoined {
addResult(occurrence)
addResult(defaultSpace.spaceID)
killMe
}

Expand Down
@@ -0,0 +1,52 @@
/*
* $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.runtime.internal.mocks

import io.sarl.core.Lifecycle
import io.sarl.core.ParticipantJoined
import io.sarl.sre.tests.testutils.agents.TestingAgent
import io.sarl.core.DefaultContextInteractions

/**
* @author $Author: sgalland$
* @version $FullVersion$
* @mavengroupid $GroupId$
* @mavenartifactid $ArtifactId$
*/
agent SpyAgent2 extends TestingAgent {

uses Lifecycle, DefaultContextInteractions

override runAgentTest : RunPolicy {
return null
}

on ParticipantJoined {
val spcid = defaultSpace.spaceID
if (occurrence.spaceID != spcid) {
addResult(occurrence)
addResult(spcid)
killMe
}
}

}
Expand Up @@ -46,6 +46,7 @@ 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.
Expand Down Expand Up @@ -469,4 +470,28 @@ class SreRunContext {
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)
}

}
Expand Up @@ -111,7 +111,7 @@ class SpaceParticipantEventEmitterTest {
var ^event = ArgumentCaptor::forClass(typeof(Event))
var scope = ArgumentCaptor::forClass(typeof(Scope))
this.defaultSpace.verify.emit(any, ^event.capture, scope.capture)
scope.value.assertNull
scope.value.assertNotNull
var evt = ^event.value
evt.assertNotNull
typeof(ParticipantJoined).assertInstanceOf(evt)
Expand Down

0 comments on commit 97a982c

Please sign in to comment.