Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[sre] Allow cancellation of tasks when the future is not yet provided…
… by the thread manager.

close #900

Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Apr 9, 2019
1 parent 07b4993 commit bb9ad7a
Show file tree
Hide file tree
Showing 2 changed files with 214 additions and 5 deletions.
Expand Up @@ -28,9 +28,12 @@
import java.util.Objects;
import java.util.TreeMap;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;

import com.google.common.base.MoreObjects;
import com.google.common.base.Strings;
Expand Down Expand Up @@ -659,12 +662,16 @@ private static class TaskDescription {
private Future<?> future;

TaskDescription(AgentTask task) {
this.task = task;
this(task, null);
}

TaskDescription(AgentTask task, Future<?> future) {
this.task = task;
this.future = future;
if (future == null) {
this.future = new FutureReceiver();
} else {
this.future = future;
}
}

@Override
Expand All @@ -680,12 +687,82 @@ public void setTask(AgentTask task) {
this.task = task;
}

public Future<?> getFuture() {
public synchronized Future<?> getFuture() {
return this.future;
}

public void setFuture(Future<?> future) {
this.future = future;
public synchronized void setFuture(Future<?> future) {
if (future != null) {
final FutureReceiver receiver;
if (this.future instanceof FutureReceiver) {
receiver = (FutureReceiver) this.future;
} else {
receiver = null;
}
this.future = future;
if (receiver != null) {
receiver.apply(this.future);
}
}
}

/**
* A future definition that enables to interact with the future
* object's even if it is not already provided by the thread manager.
* This receiver will be replaced by the real future object as soon
* as it is provided by the thread manager. Then, any interaction with
* the receiver will be propagated to the real future.
*
* @author $Author: sgalland$
* @version $Name$ $Revision$ $Date$
* @mavengroupid $GroupId$
* @mavenartifactid $ArtifactId$
* @since 0.9
*/
private static class FutureReceiver implements Future<Object> {

private final AtomicBoolean cancelFlag = new AtomicBoolean();

private final AtomicBoolean mayInterruptIfRunningFlag = new AtomicBoolean();

FutureReceiver() {
//
}

void apply(Future<?> future) {
if (future != null && !future.isCancelled() && !future.isDone() && this.cancelFlag.get()) {
future.cancel(this.mayInterruptIfRunningFlag.get());
}
}

@Override
public boolean cancel(boolean mayInterruptIfRunning) {
this.mayInterruptIfRunningFlag.set(mayInterruptIfRunning);
this.cancelFlag.set(true);
return true;
}

@Override
public boolean isCancelled() {
return this.cancelFlag.get();
}

@Override
public boolean isDone() {
return false;
}

@Override
public Object get() throws InterruptedException, ExecutionException {
throw new ExecutionException(new UnsupportedOperationException());
}

@Override
public Object get(long timeout, TimeUnit unit)
throws InterruptedException, ExecutionException, TimeoutException {
throw new ExecutionException(new UnsupportedOperationException());
}

}

}
Expand Down
@@ -0,0 +1,132 @@
/*
* $Id$
*
* SARL is an general-purpose agent programming language.
* More details on http://www.sarl.io
*
* Copyright (C) 2014-2019 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.janusproject.tests.bugs;

import java.util.Collection;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;

import org.junit.Rule;
import org.junit.Test;

import io.janusproject.tests.testutils.AbstractJanusRunTest;

import io.sarl.core.AgentKilled;
import io.sarl.core.AgentTask;
import io.sarl.core.DefaultContextInteractions;
import io.sarl.core.InnerContextAccess;
import io.sarl.core.Lifecycle;
import io.sarl.core.Schedules;
import io.sarl.lang.SARLVersion;
import io.sarl.lang.annotation.PerceptGuardEvaluator;
import io.sarl.lang.annotation.SarlElementType;
import io.sarl.lang.annotation.SarlSpecification;
import io.sarl.lang.core.Event;
import io.sarl.lang.sarl.SarlPackage;
import io.sarl.tests.api.Repeat;
import io.sarl.tests.api.RepeatRule;

/** Tests for issue #900: Run-time inconstancy between Schedules and DefaultContextInteractions.
*
* <p>See: https://github.com/sarl/sarl/issues/900
*
* @author $Author: sgalland$
* @version $FullVersion$
* @mavengroupid $GroupId$
* @mavenartifactid $ArtifactId$
* @see "https://github.com/sarl/sarl/issues/900"
*/
@SuppressWarnings("all")
public class Bug900 extends AbstractJanusRunTest {

@Test
public void run_01() throws Exception {
startJanusWithDefaultProcess(PongAgent.class, false, true, getDefaultJanusModule());
Thread.sleep(1000);
this.janusKernel.spawn(PingAgent.class, getAgentInitializationParameters());
waitForTheKernel(NO_TIMEOUT);
assertEquals(1, getNumberOfResults(getBootAgent()));
assertEquals("RECEIVE 159", getResult(getBootAgent(), String.class, 0));
forgetTheKernel();
}

@SarlSpecification(SARLVersion.SPECIFICATION_RELEASE_VERSION_STRING)
@SarlElementType(SarlPackage.SARL_EVENT)
static class Ping extends Event {
public final int index;
public Ping(int index) {
this.index = index;
}
}

@SarlSpecification(SARLVersion.SPECIFICATION_RELEASE_VERSION_STRING)
@SarlElementType(SarlPackage.SARL_AGENT)
static class PingAgent extends TestingAgent {

private final AtomicInteger index = new AtomicInteger(159);

public PingAgent(UUID parentID, UUID agentID) {
super(parentID, agentID);
}

@Override
protected boolean runAgentTest() {
AgentTask task = getSkill(Schedules.class).task("hello");
getSkill(Schedules.class).every(task, 1000, (it) -> {
if (getSkill(DefaultContextInteractions.class).getDefaultSpace().getParticipants().size() > 1) {
getSkill(DefaultContextInteractions.class).emit(new Ping(index.getAndIncrement()));
getSkill(Schedules.class).cancel(task);
}
});
getSkill(Schedules.class).in(3000, (it) -> {
forceKillMe();
});
return false;
}

}

@SarlSpecification(SARLVersion.SPECIFICATION_RELEASE_VERSION_STRING)
@SarlElementType(SarlPackage.SARL_AGENT)
static class PongAgent extends TestingAgent {

public PongAgent(UUID parentID, UUID agentID) {
super(parentID, agentID);
}

@Override
protected boolean runAgentTest() {
return false;
}

@PerceptGuardEvaluator
private void guardPing(Ping occurrence, Collection<Runnable> handlers) {
handlers.add(() -> onPing(occurrence));
}

private void onPing(Ping occurrence) {
addResult("RECEIVE " + occurrence.index);
forceKillMe();
}

}

}

0 comments on commit bb9ad7a

Please sign in to comment.