Skip to content

Commit

Permalink
[sre] Fixing the invalid emitting of ParticipantLeft.
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 c732e55 commit 7e7a181
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 34 deletions.
Expand Up @@ -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.
Expand Down
Expand Up @@ -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]
}
}

Expand Down
Expand Up @@ -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$
Expand All @@ -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<UUID> = 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<UUID> = 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)
}

}
Expand Up @@ -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$
Expand All @@ -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)
}

}
@@ -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
}

}
Expand Up @@ -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$
Expand All @@ -33,14 +34,15 @@ import io.sarl.sre.tests.testutils.agents.TestingAgent
*/
agent SpyAgent1 extends TestingAgent {

uses Lifecycle
uses Lifecycle, DefaultContextInteractions

override runAgentTest : RunPolicy {
return null
}

on ParticipantLeft {
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.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
}
}

}
Expand Up @@ -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)
Expand Down

0 comments on commit 7e7a181

Please sign in to comment.