From 97a982c32846211684540cddaf27066441cae4c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Galland?= Date: Sun, 1 Mar 2020 22:30:42 +0100 Subject: [PATCH] [sre] Fixing the invalid emitting of ParticipantJoined. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Stéphane Galland --- .../src/main/sarl/io/sarl/core/events.sarl | 2 +- .../sre/internal/PlatformEventEmitters.sarl | 10 +++- .../internal/OnParticipantJoinedTest.sarl | 58 ++++++++++++++----- .../runtime/internal/mocks/JoinerAgent0.sarl | 10 ---- .../runtime/internal/mocks/JoinerAgent2.sarl | 48 +++++++++++++++ .../runtime/internal/mocks/SpyAgent0.sarl | 4 +- .../runtime/internal/mocks/SpyAgent2.sarl | 52 +++++++++++++++++ .../testutils/extension/SreRunContext.sarl | 25 ++++++++ ...ceParticipantPlatformEventEmitterTest.sarl | 2 +- 9 files changed, 182 insertions(+), 29 deletions(-) create mode 100644 sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/runtime/internal/mocks/JoinerAgent2.sarl create mode 100644 sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/runtime/internal/mocks/SpyAgent2.sarl diff --git a/main/apiplugins/io.sarl.core/src/main/sarl/io/sarl/core/events.sarl b/main/apiplugins/io.sarl.core/src/main/sarl/io/sarl/core/events.sarl index 18004e9e16..ef57f51137 100644 --- a/main/apiplugins/io.sarl.core/src/main/sarl/io/sarl/core/events.sarl +++ b/main/apiplugins/io.sarl.core/src/main/sarl/io/sarl/core/events.sarl @@ -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. diff --git a/sre/io.janusproject/io.janusproject.plugin/src/main/sarl/io/sarl/sre/internal/PlatformEventEmitters.sarl b/sre/io.janusproject/io.janusproject.plugin/src/main/sarl/io/sarl/sre/internal/PlatformEventEmitters.sarl index dee8d4778b..02b4d35193 100644 --- a/sre/io.janusproject/io.janusproject.plugin/src/main/sarl/io/sarl/sre/internal/PlatformEventEmitters.sarl +++ b/sre/io.janusproject/io.janusproject.plugin/src/main/sarl/io/sarl/sre/internal/PlatformEventEmitters.sarl @@ -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] } } diff --git a/sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/runtime/internal/OnParticipantJoinedTest.sarl b/sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/runtime/internal/OnParticipantJoinedTest.sarl index 0f902a6c40..d2b2e1b1ff 100644 --- a/sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/runtime/internal/OnParticipantJoinedTest.sarl +++ b/sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/runtime/internal/OnParticipantJoinedTest.sarl @@ -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 @@ -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$ @@ -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 = 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 = 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) } } diff --git a/sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/runtime/internal/mocks/JoinerAgent0.sarl b/sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/runtime/internal/mocks/JoinerAgent0.sarl index f7c2353c84..8c33b152ac 100644 --- a/sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/runtime/internal/mocks/JoinerAgent0.sarl +++ b/sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/runtime/internal/mocks/JoinerAgent0.sarl @@ -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$ @@ -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 } diff --git a/sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/runtime/internal/mocks/JoinerAgent2.sarl b/sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/runtime/internal/mocks/JoinerAgent2.sarl new file mode 100644 index 0000000000..744bdd8280 --- /dev/null +++ b/sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/runtime/internal/mocks/JoinerAgent2.sarl @@ -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 + } + +} diff --git a/sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/runtime/internal/mocks/SpyAgent0.sarl b/sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/runtime/internal/mocks/SpyAgent0.sarl index a46cc34d91..e5ab87d315 100644 --- a/sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/runtime/internal/mocks/SpyAgent0.sarl +++ b/sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/runtime/internal/mocks/SpyAgent0.sarl @@ -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$ @@ -33,7 +34,7 @@ import io.sarl.sre.tests.testutils.agents.TestingAgent */ agent SpyAgent0 extends TestingAgent { - uses Lifecycle + uses Lifecycle, DefaultContextInteractions override runAgentTest : RunPolicy { return null @@ -41,6 +42,7 @@ agent SpyAgent0 extends TestingAgent { on ParticipantJoined { addResult(occurrence) + addResult(defaultSpace.spaceID) killMe } diff --git a/sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/runtime/internal/mocks/SpyAgent2.sarl b/sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/runtime/internal/mocks/SpyAgent2.sarl new file mode 100644 index 0000000000..c9b7c78a31 --- /dev/null +++ b/sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/runtime/internal/mocks/SpyAgent2.sarl @@ -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 + } + } + +} diff --git a/sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/testutils/extension/SreRunContext.sarl b/sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/testutils/extension/SreRunContext.sarl index a1343f594b..d48fe42348 100644 --- a/sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/testutils/extension/SreRunContext.sarl +++ b/sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/testutils/extension/SreRunContext.sarl @@ -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. @@ -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, 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) + } + } diff --git a/sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/units/internal/SpaceParticipantPlatformEventEmitterTest.sarl b/sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/units/internal/SpaceParticipantPlatformEventEmitterTest.sarl index a07bd918fb..5426b74065 100644 --- a/sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/units/internal/SpaceParticipantPlatformEventEmitterTest.sarl +++ b/sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/units/internal/SpaceParticipantPlatformEventEmitterTest.sarl @@ -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)