From 7e7a1814978a9f7a648f87ab58c1914425ac4551 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Galland?= Date: Sun, 1 Mar 2020 23:10:46 +0100 Subject: [PATCH] [sre] Fixing the invalid emitting of ParticipantLeft. 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 | 8 ++- .../internal/OnParticipantLeftTest.sarl | 70 +++++++++++++++---- .../runtime/internal/mocks/JoinerAgent1.sarl | 23 ++---- .../runtime/internal/mocks/JoinerAgent3.sarl | 59 ++++++++++++++++ .../runtime/internal/mocks/SpyAgent1.sarl | 4 +- .../runtime/internal/mocks/SpyAgent3.sarl | 52 ++++++++++++++ ...ceParticipantPlatformEventEmitterTest.sarl | 2 +- 8 files changed, 186 insertions(+), 34 deletions(-) create mode 100644 sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/runtime/internal/mocks/JoinerAgent3.sarl create mode 100644 sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/runtime/internal/mocks/SpyAgent3.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 ef57f51137..5089cc362f 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 @@ -505,7 +505,7 @@ final event ParticipantJoined { } -/** Notifies other participants the member with agentID has effectively +/** Notifies other participants that the member has effectively * left the space. * The event is fired within the default space of the context in which the agent left the space. * The leaving 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 02b4d35193..e39dc868d0 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 @@ -159,9 +159,13 @@ class SpaceParticipantEventEmitter implements SpaceParticipantListener { // Send the event in the default space of the context. if (this.defaultSpace !== null) { var participantAddr = participant.address + val participantId = participantAddr.UUID 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 ParticipantLeft(addr, participantAddr.spaceID)) + // 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 ParticipantLeft(addr, participantAddr.spaceID) + ) [participantId != it.UUID] } } diff --git a/sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/runtime/internal/OnParticipantLeftTest.sarl b/sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/runtime/internal/OnParticipantLeftTest.sarl index 9eb91b6fdb..f9b3a4cfc0 100644 --- a/sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/runtime/internal/OnParticipantLeftTest.sarl +++ b/sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/runtime/internal/OnParticipantLeftTest.sarl @@ -41,7 +41,11 @@ 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 +import static extension org.junit.jupiter.api.Assertions.assertNull +import static extension org.junit.jupiter.api.Assertions.assertNotSame +import io.sarl.lang.core.SpaceID +import io.sarl.sre.tests.runtime.internal.mocks.SpyAgent3 +import io.sarl.sre.tests.runtime.internal.mocks.JoinerAgent3 /** * @author $Author: sgalland$ @@ -60,30 +64,72 @@ import org.junit.jupiter.api.Disabled class OnParticipantLeftTest { @Test - @Disabled("tmp") + @DisplayName("ParticipantLeft in default space") @SuppressWarnings("discouraged_reference") - def run(extension rc : SreRunContext) { - var kern = typeof(SpyAgent1).setupTheSreKernel(null, true, true) + def runParticipantLeftInDefaultSpace(extension rc : SreRunContext) { + var kern = typeof(SpyAgent1).setupTheSreKernel(true, true) Thread::sleep(1000) val id : OutputParameter = new OutputParameter - kern.spawn(typeof(JoinerAgent1)) [ + kern.spawnInTest(typeof(JoinerAgent1)) [ id.set(it) ] waitForTheKernel(STANDARD_TIMEOUT) - var evt = getResult(bootAgent, typeof(ParticipantLeft), 0) - evt.assertNotNull + var evt0 = getResult(bootAgent, typeof(ParticipantLeft), 0) + evt0.assertNotNull - var spaceId = getResult(id.get, typeof(UUID), 0) - spaceId.assertNotNull + var spcid0 = getResult(bootAgent, typeof(SpaceID), 1) + spcid0.assertNotNull - bootAgent.assertEquals(evt.source.UUID) - spaceId.assertNotEquals(evt.source.spaceID.ID) - spaceId.assertEquals(evt.spaceID.ID) + var evt1 = getResult(id.get, typeof(ParticipantLeft), 0) + evt1.assertNull + + id.get.assertEquals(evt0.source.UUID) + spcid0.assertEquals(evt0.spaceID) + } + + @Test + @DisplayName("ParticipantLeft in other space") + @SuppressWarnings("discouraged_reference") + def runParticipantLeftInOtherSpace(extension rc : SreRunContext) { + var kern = typeof(SpyAgent3).setupTheSreKernel(true, true) + + Thread::sleep(1000) + + val id : OutputParameter = new OutputParameter + + kern.spawnInTest(typeof(JoinerAgent3)) [ + id.set(it) + ] + + waitForTheKernel(NO_TIMEOUT) + + var evt0 = getResult(bootAgent, typeof(ParticipantLeft), 0) + evt0.assertNotNull + + var spcid0 = getResult(bootAgent, typeof(SpaceID), 1) + spcid0.assertNotNull + + var spcid1 = getResult(id.get, typeof(SpaceID), 0) + spcid1.assertNotNull + + var evt1 = getResult(id.get, typeof(ParticipantLeft), 1) + evt1.assertNotNull + + spcid0.assertNotEquals(spcid1) + evt0.assertNotSame(evt1) + + spcid0.assertEquals(evt0.source.spaceID) + id.get.assertEquals(evt0.source.UUID) + spcid1.assertEquals(evt0.spaceID) + + spcid0.assertEquals(evt1.source.spaceID) + bootAgent.assertEquals(evt1.source.UUID) + spcid0.assertEquals(evt1.spaceID) } } diff --git a/sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/runtime/internal/mocks/JoinerAgent1.sarl b/sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/runtime/internal/mocks/JoinerAgent1.sarl index 19ff253350..281017d45f 100644 --- a/sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/runtime/internal/mocks/JoinerAgent1.sarl +++ b/sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/runtime/internal/mocks/JoinerAgent1.sarl @@ -21,13 +21,8 @@ package io.sarl.sre.tests.runtime.internal.mocks -import io.sarl.core.Behaviors -import io.sarl.core.DefaultContextInteractions -import io.sarl.core.Lifecycle -import io.sarl.core.OpenEventSpaceSpecification -import io.sarl.core.Schedules +import io.sarl.core.ParticipantLeft import io.sarl.sre.tests.testutils.agents.TestingAgent -import java.util.UUID /** * @author $Author: sgalland$ @@ -37,18 +32,12 @@ import java.util.UUID */ agent JoinerAgent1 extends TestingAgent { - uses DefaultContextInteractions, Schedules, Behaviors, Lifecycle - override runAgentTest : RunPolicy { - var id = UUID::randomUUID - val spc = defaultContext.getOrCreateSpaceWithSpec(typeof(OpenEventSpaceSpecification), id) - spc.register(asEventListener) - addResult(id) - in(1000) [ - spc.unregister(asEventListener) - killMe - ] - return RunPolicy::STANDARD + return RunPolicy::DIFFERED_KILLING + } + + on ParticipantLeft { + addResult(occurrence) } } diff --git a/sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/runtime/internal/mocks/JoinerAgent3.sarl b/sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/runtime/internal/mocks/JoinerAgent3.sarl new file mode 100644 index 0000000000..695f1f582d --- /dev/null +++ b/sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/runtime/internal/mocks/JoinerAgent3.sarl @@ -0,0 +1,59 @@ +/* + * $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.Behaviors +import io.sarl.core.DefaultContextInteractions +import io.sarl.core.Lifecycle +import io.sarl.core.OpenEventSpaceSpecification +import io.sarl.core.Schedules +import io.sarl.sre.tests.testutils.agents.TestingAgent +import java.util.UUID +import io.sarl.core.ParticipantLeft + +/** + * @author $Author: sgalland$ + * @version $FullVersion$ + * @mavengroupid $GroupId$ + * @mavenartifactid $ArtifactId$ + */ +agent JoinerAgent3 extends TestingAgent { + + uses DefaultContextInteractions, Schedules, Behaviors, Lifecycle + + override runAgentTest : RunPolicy { + var id = UUID::randomUUID + val spc = defaultContext.getOrCreateSpaceWithSpec(typeof(OpenEventSpaceSpecification), id) + spc.register(asEventListener) + addResult(spc.spaceID) + in(500) [ + spc.unregister(asEventListener) + ] + return RunPolicy::STANDARD + } + + on ParticipantLeft [it.source.UUID != ID] { + addResult(occurrence) + killMe + } + +} diff --git a/sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/runtime/internal/mocks/SpyAgent1.sarl b/sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/runtime/internal/mocks/SpyAgent1.sarl index de1f0d1b69..801fc78173 100644 --- a/sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/runtime/internal/mocks/SpyAgent1.sarl +++ b/sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/runtime/internal/mocks/SpyAgent1.sarl @@ -24,6 +24,7 @@ package io.sarl.sre.tests.runtime.internal.mocks import io.sarl.core.Lifecycle import io.sarl.core.ParticipantLeft 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 SpyAgent1 extends TestingAgent { - uses Lifecycle + uses Lifecycle, DefaultContextInteractions override runAgentTest : RunPolicy { return null @@ -41,6 +42,7 @@ agent SpyAgent1 extends TestingAgent { on ParticipantLeft { 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/SpyAgent3.sarl b/sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/runtime/internal/mocks/SpyAgent3.sarl new file mode 100644 index 0000000000..f8c8001ef0 --- /dev/null +++ b/sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/runtime/internal/mocks/SpyAgent3.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.ParticipantLeft +import io.sarl.sre.tests.testutils.agents.TestingAgent +import io.sarl.core.DefaultContextInteractions + +/** + * @author $Author: sgalland$ + * @version $FullVersion$ + * @mavengroupid $GroupId$ + * @mavenartifactid $ArtifactId$ + */ +agent SpyAgent3 extends TestingAgent { + + uses Lifecycle, DefaultContextInteractions + + override runAgentTest : RunPolicy { + return null + } + + on ParticipantLeft { + 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/units/internal/SpaceParticipantPlatformEventEmitterTest.sarl b/sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/units/internal/SpaceParticipantPlatformEventEmitterTest.sarl index 5426b74065..af7ceacac8 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 @@ -137,7 +137,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(ParticipantLeft).assertInstanceOf(evt)