You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@brooklyn.apache.org by al...@apache.org on 2016/08/03 19:49:32 UTC
[1/3] brooklyn-server git commit: code tidy: SoftwareProcess tests
Repository: brooklyn-server
Updated Branches:
refs/heads/master 636851100 -> e1c310f5f
code tidy: SoftwareProcess tests
Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo
Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/9d32882b
Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/9d32882b
Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/9d32882b
Branch: refs/heads/master
Commit: 9d32882badebe9fe5f3002c33a91d088c7161c91
Parents: 6368511
Author: Aled Sage <al...@gmail.com>
Authored: Wed Aug 3 14:26:06 2016 +0100
Committer: Aled Sage <al...@gmail.com>
Committed: Wed Aug 3 16:08:28 2016 +0100
----------------------------------------------------------------------
.../org/apache/brooklyn/core/feed/Poller.java | 2 +-
.../core/mgmt/rebind/RebindTestFixture.java | 8 +++
.../core/mgmt/rebind/RebindTestUtils.java | 10 +++
.../base/SoftwareProcessEntityRebindTest.java | 68 ++++++--------------
4 files changed, 39 insertions(+), 49 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/9d32882b/core/src/main/java/org/apache/brooklyn/core/feed/Poller.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/core/feed/Poller.java b/core/src/main/java/org/apache/brooklyn/core/feed/Poller.java
index a9a34d9..eed8f63 100644
--- a/core/src/main/java/org/apache/brooklyn/core/feed/Poller.java
+++ b/core/src/main/java/org/apache/brooklyn/core/feed/Poller.java
@@ -148,7 +148,7 @@ public class Poller<V> {
DynamicSequentialTask<Void> task = new DynamicSequentialTask<Void>(MutableMap.of("displayName", scheduleName, "entity", entity),
new Callable<Void>() { public Void call() {
if (onlyIfServiceUp && !Boolean.TRUE.equals(entity.getAttribute(Attributes.SERVICE_UP))) {
- return null;
+ return null;
}
pollJob.wrappedJob.run();
return null;
http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/9d32882b/core/src/test/java/org/apache/brooklyn/core/mgmt/rebind/RebindTestFixture.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/brooklyn/core/mgmt/rebind/RebindTestFixture.java b/core/src/test/java/org/apache/brooklyn/core/mgmt/rebind/RebindTestFixture.java
index 4744708..819b8f0 100644
--- a/core/src/test/java/org/apache/brooklyn/core/mgmt/rebind/RebindTestFixture.java
+++ b/core/src/test/java/org/apache/brooklyn/core/mgmt/rebind/RebindTestFixture.java
@@ -45,6 +45,7 @@ import org.apache.brooklyn.core.mgmt.internal.ManagementContextInternal;
import org.apache.brooklyn.core.mgmt.persist.BrooklynMementoPersisterToObjectStore;
import org.apache.brooklyn.core.mgmt.persist.FileBasedObjectStore;
import org.apache.brooklyn.core.mgmt.persist.PersistMode;
+import org.apache.brooklyn.core.test.entity.LocalManagementContextForTests;
import org.apache.brooklyn.util.core.task.BasicExecutionManager;
import org.apache.brooklyn.util.os.Os;
import org.apache.brooklyn.util.repeat.Repeater;
@@ -97,15 +98,22 @@ public abstract class RebindTestFixture<T extends StartableApplication> {
/** @return A started management context */
protected LocalManagementContext createOrigManagementContext() {
+ LocalManagementContextForTests.newInstance();
+
return RebindTestUtils.managementContextBuilder(mementoDir, classLoader)
.persistPeriodMillis(getPersistPeriodMillis())
.forLive(useLiveManagementContext())
+ .enablePersistenceBackups(enablePersistenceBackups())
.emptyCatalog(useEmptyCatalog())
.properties(createBrooklynProperties())
.enableOsgi(useOsgi())
.buildStarted();
}
+ protected boolean enablePersistenceBackups() {
+ return true;
+ }
+
/** As {@link #createNewManagementContext(File)} using the default memento dir */
protected LocalManagementContext createNewManagementContext() {
return createNewManagementContext(mementoDir);
http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/9d32882b/core/src/test/java/org/apache/brooklyn/core/mgmt/rebind/RebindTestUtils.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/brooklyn/core/mgmt/rebind/RebindTestUtils.java b/core/src/test/java/org/apache/brooklyn/core/mgmt/rebind/RebindTestUtils.java
index 24c8024..ad2bdf8 100644
--- a/core/src/test/java/org/apache/brooklyn/core/mgmt/rebind/RebindTestUtils.java
+++ b/core/src/test/java/org/apache/brooklyn/core/mgmt/rebind/RebindTestUtils.java
@@ -153,6 +153,7 @@ public class RebindTestUtils {
boolean forLive;
boolean enableOsgi = false;
boolean emptyCatalog;
+ private boolean enablePersistenceBackups = true;
ManagementContextBuilder(File mementoDir, ClassLoader classLoader) {
this(classLoader, new FileBasedObjectStore(mementoDir));
@@ -185,6 +186,10 @@ public class RebindTestUtils {
return this;
}
+ public ManagementContextBuilder enablePersistenceBackups(boolean val) {
+ this.enablePersistenceBackups = val;
+ return this;
+ }
public ManagementContextBuilder enableOsgi(boolean val) {
this.enableOsgi = val;
return this;
@@ -208,6 +213,11 @@ public class RebindTestUtils {
if (this.emptyCatalog) {
properties.putIfAbsent(BrooklynServerConfig.BROOKLYN_CATALOG_URL, ManagementContextInternal.EMPTY_CATALOG_URL);
}
+ if (!enablePersistenceBackups) {
+ properties.putIfAbsent(BrooklynServerConfig.PERSISTENCE_BACKUPS_REQUIRED_ON_DEMOTION, false);
+ properties.putIfAbsent(BrooklynServerConfig.PERSISTENCE_BACKUPS_REQUIRED_ON_PROMOTION, false);
+ properties.putIfAbsent(BrooklynServerConfig.PERSISTENCE_BACKUPS_REQUIRED, false);
+ }
if (forLive) {
unstarted = new LocalManagementContext(properties);
} else {
http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/9d32882b/software/base/src/test/java/org/apache/brooklyn/entity/software/base/SoftwareProcessEntityRebindTest.java
----------------------------------------------------------------------
diff --git a/software/base/src/test/java/org/apache/brooklyn/entity/software/base/SoftwareProcessEntityRebindTest.java b/software/base/src/test/java/org/apache/brooklyn/entity/software/base/SoftwareProcessEntityRebindTest.java
index 3592e3c..8c568c3 100644
--- a/software/base/src/test/java/org/apache/brooklyn/entity/software/base/SoftwareProcessEntityRebindTest.java
+++ b/software/base/src/test/java/org/apache/brooklyn/entity/software/base/SoftwareProcessEntityRebindTest.java
@@ -22,7 +22,6 @@ import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue;
-import java.io.File;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
@@ -33,60 +32,38 @@ import org.apache.brooklyn.api.entity.EntitySpec;
import org.apache.brooklyn.api.location.LocationSpec;
import org.apache.brooklyn.api.location.MachineProvisioningLocation;
import org.apache.brooklyn.api.location.NoMachinesAvailableException;
-import org.apache.brooklyn.api.mgmt.ManagementContext;
import org.apache.brooklyn.core.entity.Attributes;
-import org.apache.brooklyn.core.entity.Entities;
+import org.apache.brooklyn.core.entity.BrooklynConfigKeys;
import org.apache.brooklyn.core.entity.EntityAsserts;
import org.apache.brooklyn.core.entity.lifecycle.Lifecycle;
import org.apache.brooklyn.core.entity.lifecycle.ServiceStateLogic;
import org.apache.brooklyn.core.entity.lifecycle.ServiceStateLogic.ServiceProblemsLogic;
import org.apache.brooklyn.core.location.AbstractLocation;
-import org.apache.brooklyn.core.mgmt.rebind.RebindTestUtils;
-import org.apache.brooklyn.core.test.BrooklynAppUnitTestSupport;
+import org.apache.brooklyn.core.mgmt.rebind.RebindTestFixtureWithApp;
import org.apache.brooklyn.core.test.entity.TestApplication;
import org.apache.brooklyn.entity.software.base.SoftwareProcessEntityTest.MyService;
+import org.apache.brooklyn.location.ssh.SshMachineLocation;
import org.apache.brooklyn.util.core.flags.SetFromFlag;
-import org.testng.annotations.AfterMethod;
-import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
-import org.apache.brooklyn.location.ssh.SshMachineLocation;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
-import com.google.common.io.Files;
-public class SoftwareProcessEntityRebindTest extends BrooklynAppUnitTestSupport {
+public class SoftwareProcessEntityRebindTest extends RebindTestFixtureWithApp {
- private ClassLoader classLoader = getClass().getClassLoader();
- private TestApplication newApp;
- private ManagementContext newManagementContext;
- private MyService origE;
- private File mementoDir;
-
- @BeforeMethod(alwaysRun=true)
@Override
- public void setUp() throws Exception {
- mementoDir = Files.createTempDir();
- mgmt = RebindTestUtils.newPersistingManagementContext(mementoDir, classLoader);
- super.setUp();
+ protected boolean enablePersistenceBackups() {
+ return false;
}
- @AfterMethod(alwaysRun=true)
- @Override
- public void tearDown() throws Exception {
- super.tearDown();
- if (newApp != null) Entities.destroyAll(newApp.getManagementContext());
- if (newManagementContext != null) Entities.destroyAll(newManagementContext);
- if (mementoDir != null) RebindTestUtils.deleteMementoDir(mementoDir);
- }
-
@Test
public void testReleasesLocationOnStopAfterRebinding() throws Exception {
- origE = app.createAndManageChild(EntitySpec.create(MyService.class));
+ MyService origE = origApp.createAndManageChild(EntitySpec.create(MyService.class)
+ .configure(BrooklynConfigKeys.SKIP_ON_BOX_BASE_DIR_RESOLUTION, true));
- MyProvisioningLocation origLoc = mgmt.getLocationManager().createLocation(LocationSpec.create(MyProvisioningLocation.class)
+ MyProvisioningLocation origLoc = mgmt().getLocationManager().createLocation(LocationSpec.create(MyProvisioningLocation.class)
.displayName("mylocname"));
- app.start(ImmutableList.of(origLoc));
+ origApp.start(ImmutableList.of(origLoc));
assertEquals(origLoc.inUseCount.get(), 1);
newApp = (TestApplication) rebind();
@@ -99,13 +76,14 @@ public class SoftwareProcessEntityRebindTest extends BrooklynAppUnitTestSupport
@Test
public void testCreatesDriverAfterRebind() throws Exception {
- origE = app.createAndManageChild(EntitySpec.create(MyService.class));
- //the entity skips enricher initialization, do it explicitly
+ MyService origE = origApp.createAndManageChild(EntitySpec.create(MyService.class)
+ .configure(BrooklynConfigKeys.SKIP_ON_BOX_BASE_DIR_RESOLUTION, true));
+ //the entity skips enricher initialization, do it explicitly
origE.enrichers().add(ServiceStateLogic.newEnricherForServiceStateFromProblemsAndUp());
- MyProvisioningLocation origLoc = mgmt.getLocationManager().createLocation(LocationSpec.create(MyProvisioningLocation.class)
+ MyProvisioningLocation origLoc = mgmt().getLocationManager().createLocation(LocationSpec.create(MyProvisioningLocation.class)
.displayName("mylocname"));
- app.start(ImmutableList.of(origLoc));
+ origApp.start(ImmutableList.of(origLoc));
assertEquals(origE.getAttribute(Attributes.SERVICE_STATE_EXPECTED).getState(), Lifecycle.RUNNING);
EntityAsserts.assertAttributeEqualsEventually(origE, Attributes.SERVICE_STATE_ACTUAL, Lifecycle.RUNNING);
@@ -119,13 +97,14 @@ public class SoftwareProcessEntityRebindTest extends BrooklynAppUnitTestSupport
@Test
public void testDoesNotCreateDriverAfterRebind() throws Exception {
- origE = app.createAndManageChild(EntitySpec.create(MyService.class));
- //the entity skips enricher initialization, do it explicitly
+ MyService origE = origApp.createAndManageChild(EntitySpec.create(MyService.class)
+ .configure(BrooklynConfigKeys.SKIP_ON_BOX_BASE_DIR_RESOLUTION, true));
+ //the entity skips enricher initialization, do it explicitly
origE.enrichers().add(ServiceStateLogic.newEnricherForServiceStateFromProblemsAndUp());
- MyProvisioningLocation origLoc = mgmt.getLocationManager().createLocation(LocationSpec.create(MyProvisioningLocation.class)
+ MyProvisioningLocation origLoc = mgmt().getLocationManager().createLocation(LocationSpec.create(MyProvisioningLocation.class)
.displayName("mylocname"));
- app.start(ImmutableList.of(origLoc));
+ origApp.start(ImmutableList.of(origLoc));
assertEquals(origE.getAttribute(Attributes.SERVICE_STATE_EXPECTED).getState(), Lifecycle.RUNNING);
EntityAsserts.assertAttributeEqualsEventually(origE, Attributes.SERVICE_STATE_ACTUAL, Lifecycle.RUNNING);
@@ -136,13 +115,6 @@ public class SoftwareProcessEntityRebindTest extends BrooklynAppUnitTestSupport
MyService newE = (MyService) Iterables.getOnlyElement(newApp.getChildren());
assertNull(newE.getDriver(), "driver should not be initialized because entity is in a permanent failure");
}
-
- private TestApplication rebind() throws Exception {
- RebindTestUtils.waitForPersisted(app);
- TestApplication result = (TestApplication) RebindTestUtils.rebind(mementoDir, getClass().getClassLoader());
- newManagementContext = result.getManagementContext();
- return result;
- }
public static class MyProvisioningLocation extends AbstractLocation implements MachineProvisioningLocation<SshMachineLocation> {
@SetFromFlag(defaultVal="0")
[3/3] brooklyn-server git commit: This closes #286
Posted by al...@apache.org.
This closes #286
Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo
Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/e1c310f5
Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/e1c310f5
Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/e1c310f5
Branch: refs/heads/master
Commit: e1c310f5f1dd3b7e2bb72cb7487f91a2f1017697
Parents: 6368511 b54814d
Author: Aled Sage <al...@gmail.com>
Authored: Wed Aug 3 20:49:15 2016 +0100
Committer: Aled Sage <al...@gmail.com>
Committed: Wed Aug 3 20:49:15 2016 +0100
----------------------------------------------------------------------
.../org/apache/brooklyn/core/feed/Poller.java | 5 +-
.../core/mgmt/rebind/RebindTestFixture.java | 8 +
.../core/mgmt/rebind/RebindTestUtils.java | 10 ++
.../SoftwareProcessEntityFeedRebindTest.java | 175 +++++++++++++++++++
.../base/SoftwareProcessEntityRebindTest.java | 68 +++----
5 files changed, 217 insertions(+), 49 deletions(-)
----------------------------------------------------------------------
[2/3] brooklyn-server git commit: BROOKLYN-322: feeds don’t poll if entity unmanaged
Posted by al...@apache.org.
BROOKLYN-322: feeds don\u2019t poll if entity unmanaged
Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo
Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/b54814d3
Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/b54814d3
Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/b54814d3
Branch: refs/heads/master
Commit: b54814d39bc3e9cd4a97c77bdfbcd2fd7b59516b
Parents: 9d32882
Author: Aled Sage <al...@gmail.com>
Authored: Wed Aug 3 14:26:38 2016 +0100
Committer: Aled Sage <al...@gmail.com>
Committed: Wed Aug 3 18:01:23 2016 +0100
----------------------------------------------------------------------
.../org/apache/brooklyn/core/feed/Poller.java | 3 +
.../SoftwareProcessEntityFeedRebindTest.java | 175 +++++++++++++++++++
2 files changed, 178 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/b54814d3/core/src/main/java/org/apache/brooklyn/core/feed/Poller.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/core/feed/Poller.java b/core/src/main/java/org/apache/brooklyn/core/feed/Poller.java
index eed8f63..9d8e0f8 100644
--- a/core/src/main/java/org/apache/brooklyn/core/feed/Poller.java
+++ b/core/src/main/java/org/apache/brooklyn/core/feed/Poller.java
@@ -147,6 +147,9 @@ public class Poller<V> {
public Task<?> call() {
DynamicSequentialTask<Void> task = new DynamicSequentialTask<Void>(MutableMap.of("displayName", scheduleName, "entity", entity),
new Callable<Void>() { public Void call() {
+ if (!Entities.isManaged(entity)) {
+ return null;
+ }
if (onlyIfServiceUp && !Boolean.TRUE.equals(entity.getAttribute(Attributes.SERVICE_UP))) {
return null;
}
http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/b54814d3/software/base/src/test/java/org/apache/brooklyn/entity/software/base/SoftwareProcessEntityFeedRebindTest.java
----------------------------------------------------------------------
diff --git a/software/base/src/test/java/org/apache/brooklyn/entity/software/base/SoftwareProcessEntityFeedRebindTest.java b/software/base/src/test/java/org/apache/brooklyn/entity/software/base/SoftwareProcessEntityFeedRebindTest.java
new file mode 100644
index 0000000..506ff39
--- /dev/null
+++ b/software/base/src/test/java/org/apache/brooklyn/entity/software/base/SoftwareProcessEntityFeedRebindTest.java
@@ -0,0 +1,175 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.brooklyn.entity.software.base;
+
+import static org.testng.Assert.assertFalse;
+
+import java.util.List;
+import java.util.concurrent.Callable;
+
+import org.apache.brooklyn.api.entity.Entity;
+import org.apache.brooklyn.api.entity.EntityLocal;
+import org.apache.brooklyn.api.entity.EntitySpec;
+import org.apache.brooklyn.api.location.LocationSpec;
+import org.apache.brooklyn.api.sensor.AttributeSensor;
+import org.apache.brooklyn.core.entity.BrooklynConfigKeys;
+import org.apache.brooklyn.core.entity.Entities;
+import org.apache.brooklyn.core.entity.EntityAsserts;
+import org.apache.brooklyn.core.mgmt.rebind.RebindTestFixtureWithApp;
+import org.apache.brooklyn.core.sensor.Sensors;
+import org.apache.brooklyn.core.test.entity.TestApplication;
+import org.apache.brooklyn.entity.software.base.SoftwareProcessEntityRebindTest.MyProvisioningLocation;
+import org.apache.brooklyn.entity.software.base.SoftwareProcessEntityTest.MyService;
+import org.apache.brooklyn.entity.software.base.SoftwareProcessEntityTest.MyServiceImpl;
+import org.apache.brooklyn.entity.software.base.SoftwareProcessEntityTest.SimulatedDriver;
+import org.apache.brooklyn.feed.function.FunctionFeed;
+import org.apache.brooklyn.feed.function.FunctionPollConfig;
+import org.apache.brooklyn.location.ssh.SshMachineLocation;
+import org.apache.brooklyn.util.time.Duration;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.testng.annotations.Test;
+
+import com.google.common.base.Functions;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Lists;
+
+public class SoftwareProcessEntityFeedRebindTest extends RebindTestFixtureWithApp {
+
+ private static final Logger LOG = LoggerFactory.getLogger(SoftwareProcessEntityFeedRebindTest.class);
+
+ @Override
+ protected boolean enablePersistenceBackups() {
+ return false;
+ }
+
+ @Test
+ public void testFeedsDoNotPollUntilManaged() throws Exception {
+ runFeedsDoNotPollUntilManaged(1);
+ }
+
+ @Test(groups="Integeration")
+ public void testFeedsDoNotPollUntilManagedManyEntities() throws Exception {
+ runFeedsDoNotPollUntilManaged(100);
+ }
+
+ protected void runFeedsDoNotPollUntilManaged(int numEntities) throws Exception {
+ List<MyService> origEs = Lists.newArrayList();
+
+ LOG.info("Creating "+numEntities+" entities");
+ for (int i = 0; i < numEntities; i++) {
+ origEs.add(origApp.createAndManageChild(EntitySpec.create(MyService.class)
+ .impl(MyServiceWithFeedsImpl.class)
+ .configure(BrooklynConfigKeys.SKIP_ON_BOX_BASE_DIR_RESOLUTION, true)));
+ }
+
+ LOG.info("Starting "+numEntities+" entities");
+ MyProvisioningLocation origLoc = mgmt().getLocationManager().createLocation(LocationSpec.create(MyProvisioningLocation.class)
+ .displayName("mylocname"));
+ origApp.start(ImmutableList.of(origLoc));
+
+ LOG.info("Rebinding "+numEntities+" entities");
+ newApp = (TestApplication) rebind();
+ List<Entity> newEs = ImmutableList.copyOf(newApp.getChildren());
+
+ LOG.info("Checking state of "+numEntities+" entities");
+ for (Entity newE : newEs) {
+ EntityAsserts.assertAttributeChangesEventually(newE, MyServiceWithFeeds.COUNTER);
+ assertFalse(((MyServiceWithFeeds)newE).isFeedCalledWhenNotManaged());
+ SimulatedDriverWithFeeds driver = (SimulatedDriverWithFeeds) ((MyServiceWithFeeds)newE).getDriver();
+ assertFalse(driver.isRunningCalledWhenNotManaged);
+ }
+ }
+
+ public static interface MyServiceWithFeeds extends MyService {
+ AttributeSensor<Integer> COUNTER = Sensors.newIntegerSensor("counter");
+
+ boolean isFeedCalledWhenNotManaged();
+ }
+
+ public static class MyServiceWithFeedsImpl extends MyServiceImpl implements MyServiceWithFeeds {
+ protected FunctionFeed functionFeed;
+ protected boolean feedCalledWhenNotManaged;
+
+ @Override
+ public boolean isFeedCalledWhenNotManaged() {
+ return feedCalledWhenNotManaged;
+ }
+
+ @Override
+ public void init() {
+ super.init();
+
+ // By calling feeds().add(...), it will persist the feed, and rebind it
+ functionFeed = feeds().add(FunctionFeed.builder()
+ .entity(this)
+ .period(Duration.millis(10))
+ .uniqueTag("MyserviceWithFeeds-functionFeed")
+ .poll(new FunctionPollConfig<Integer, Integer>(COUNTER)
+ .suppressDuplicates(true)
+ .onException(Functions.constant(-1))
+ .callable(new Callable<Integer>() {
+ public Integer call() {
+ if (!Entities.isManaged(MyServiceWithFeedsImpl.this)) {
+ feedCalledWhenNotManaged = true;
+ throw new IllegalStateException("Entity "+MyServiceWithFeedsImpl.this+" is not managed in feed.call");
+ }
+ Integer oldVal = sensors().get(COUNTER);
+ return (oldVal == null ? 0 : oldVal) + 1;
+ }
+ }))
+ .build());
+ }
+
+ @Override
+ protected void connectSensors() {
+ // connectSensors is called on rebind; it will re-register the feed
+ super.connectSensors();
+ super.connectServiceUpIsRunning();
+ }
+
+ @Override
+ protected void disconnectSensors() {
+ super.disconnectSensors();
+ super.disconnectServiceUpIsRunning();
+ }
+
+ @Override
+ public Class<?> getDriverInterface() {
+ return SimulatedDriverWithFeeds.class;
+ }
+ }
+
+ public static class SimulatedDriverWithFeeds extends SimulatedDriver {
+ protected boolean isRunningCalledWhenNotManaged = false;
+
+ public SimulatedDriverWithFeeds(EntityLocal entity, SshMachineLocation machine) {
+ super(entity, machine);
+ }
+
+ @Override
+ public boolean isRunning() {
+ if (!Entities.isManaged(entity)) {
+ isRunningCalledWhenNotManaged = true;
+ throw new IllegalStateException("Entity "+entity+" is not managed in driver.isRunning");
+ }
+ return super.isRunning();
+ }
+ }
+}