You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@brooklyn.apache.org by "Svetoslav Neykov (JIRA)" <ji...@apache.org> on 2017/02/28 12:44:45 UTC
[jira] [Created] (BROOKLYN-446) Constructor ScheduledTask(Map,
Task) is broken
Svetoslav Neykov created BROOKLYN-446:
-----------------------------------------
Summary: Constructor ScheduledTask(Map, Task) is broken
Key: BROOKLYN-446
URL: https://issues.apache.org/jira/browse/BROOKLYN-446
Project: Brooklyn
Issue Type: Bug
Affects Versions: 0.10.0
Reporter: Svetoslav Neykov
The resulting task is executed only once, not rescheduled.
A test case to show the behaviour:
{noformat}
import static org.testng.Assert.assertTrue;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.brooklyn.api.mgmt.Task;
import org.apache.brooklyn.core.test.BrooklynMgmtUnitTestSupport;
import org.apache.brooklyn.test.Asserts;
import org.apache.brooklyn.util.collections.MutableMap;
import org.apache.brooklyn.util.time.Duration;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableMap;
import com.google.common.util.concurrent.Runnables;
public class ScheduledTaskTest extends BrooklynMgmtUnitTestSupport {
/**
* Fails with:
* java.lang.UnsupportedOperationException
* at com.google.common.collect.ImmutableMap.remove(ImmutableMap.java:338)
* at org.apache.brooklyn.util.core.task.BasicTask.<init>(BasicTask.java:115)
* at org.apache.brooklyn.util.core.task.BasicTask.<init>(BasicTask.java:107)
* at org.apache.brooklyn.util.core.task.ScheduledTask.<init>(ScheduledTask.java:96)
* at org.apache.brooklyn.util.core.task.ScheduledTask.<init>(ScheduledTask.java:88)
* at org.apache.brooklyn.util.core.task.ScheduledTaskTest.testImmutableFlags(ScheduledTaskTest.java:43)
*/
@Test
public void testImmutableFlags() {
Map<?, ?> flags = ImmutableMap.of(
"period", Duration.ONE_SECOND,
"delay", Duration.ONE_SECOND);
Task<?> task = new BasicTask<>(Runnables.doNothing());
new ScheduledTask(flags, task);
}
/**
* Fails with:
* java.lang.AssertionError: failed succeeds-eventually, 69 attempts, 30005ms elapsed: AssertionError: expected [true] but found [false]
* at org.apache.brooklyn.test.Asserts.succeedsEventually(Asserts.java:1008)
* at org.apache.brooklyn.test.Asserts.succeedsEventually(Asserts.java:895)
* at org.apache.brooklyn.test.Asserts.succeedsEventually(Asserts.java:888)
* at org.apache.brooklyn.util.core.task.ScheduledTaskTest.testTaskConstructor(ScheduledTaskTest.java:60)
*/
@Test
public void testTaskConstructor() {
final AtomicInteger cnt = new AtomicInteger();
Map<?, ?> flags = MutableMap.of(
"period", Duration.ONE_SECOND,
"delay", Duration.ONE_SECOND);
Task<?> task = new BasicTask<>(new Runnable() {
@Override
public void run() {
cnt.incrementAndGet();
}
});
ScheduledTask scheduledTask = new ScheduledTask(flags, task);
mgmt.getExecutionManager().submit(scheduledTask);
Asserts.succeedsEventually(new Runnable() {
@Override
public void run() {
assertTrue(cnt.get() > 1);
}
});
}
}
{noformat}
The problem is that the task gets reused and on the second schedule attempt it already has a future assigned so is not scheduled any more.
The best solution seems to be to remove the constructor altogether. Optionally providing a Runnable/Callable job constructor, wrapping the job in a factory, returning a new task on each submission.
--
This message was sent by Atlassian JIRA
(v6.3.15#6346)