brooklyn-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ahgittin <...@git.apache.org>
Subject [GitHub] incubator-brooklyn pull request: Rebind read only
Date Wed, 01 Oct 2014 17:21:08 GMT
Github user ahgittin commented on a diff in the pull request:

    https://github.com/apache/incubator-brooklyn/pull/197#discussion_r18293334
  
    --- Diff: core/src/test/java/brooklyn/management/ha/HotStandbyTest.java ---
    @@ -0,0 +1,562 @@
    +/*
    + * 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 brooklyn.management.ha;
    +
    +import static org.testng.Assert.assertEquals;
    +
    +import java.util.ArrayDeque;
    +import java.util.Date;
    +import java.util.Deque;
    +import java.util.List;
    +import java.util.Map;
    +
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +import org.testng.Assert;
    +import org.testng.annotations.AfterMethod;
    +import org.testng.annotations.BeforeMethod;
    +import org.testng.annotations.Test;
    +
    +import brooklyn.entity.Application;
    +import brooklyn.entity.Entity;
    +import brooklyn.entity.basic.Entities;
    +import brooklyn.entity.proxying.EntitySpec;
    +import brooklyn.entity.rebind.PersistenceExceptionHandlerImpl;
    +import brooklyn.entity.rebind.RebindManagerImpl;
    +import brooklyn.entity.rebind.persister.BrooklynMementoPersisterToObjectStore;
    +import brooklyn.entity.rebind.persister.InMemoryObjectStore;
    +import brooklyn.entity.rebind.persister.ListeningObjectStore;
    +import brooklyn.entity.rebind.persister.PersistMode;
    +import brooklyn.entity.rebind.persister.PersistenceObjectStore;
    +import brooklyn.location.Location;
    +import brooklyn.management.internal.AbstractManagementContext;
    +import brooklyn.management.internal.ManagementContextInternal;
    +import brooklyn.test.EntityTestUtils;
    +import brooklyn.test.entity.LocalManagementContextForTests;
    +import brooklyn.test.entity.TestApplication;
    +import brooklyn.test.entity.TestEntity;
    +import brooklyn.util.collections.MutableList;
    +import brooklyn.util.collections.MutableMap;
    +import brooklyn.util.javalang.JavaClassNames;
    +import brooklyn.util.text.ByteSizeStrings;
    +import brooklyn.util.time.Duration;
    +import brooklyn.util.time.Time;
    +
    +import com.google.common.collect.Iterables;
    +
    +public class HotStandbyTest {
    +
    +    private static final Logger log = LoggerFactory.getLogger(HotStandbyTest.class);
    +    
    +    private List<HaMgmtNode> nodes = new MutableList<HotStandbyTest.HaMgmtNode>();
    +    Map<String,String> sharedBackingStore = MutableMap.of();
    +    Map<String,Date> sharedBackingStoreDates = MutableMap.of();
    +    private ClassLoader classLoader = getClass().getClassLoader();
    +    
    +    public class HaMgmtNode {
    +        
    +        private ManagementContextInternal mgmt;
    +        private String ownNodeId;
    +        private String nodeName;
    +        private ListeningObjectStore objectStore;
    +        private ManagementPlaneSyncRecordPersister persister;
    +        private HighAvailabilityManagerImpl ha;
    +        private Duration persistOrRebindPeriod = Duration.ONE_SECOND;
    +
    +        public void setUp() throws Exception {
    +            nodeName = "node "+nodes.size();
    +            mgmt = newLocalManagementContext();
    +            ownNodeId = mgmt.getManagementNodeId();
    +            objectStore = new ListeningObjectStore(newPersistenceObjectStore());
    +            objectStore.injectManagementContext(mgmt);
    +            objectStore.prepareForSharedUse(PersistMode.CLEAN, HighAvailabilityMode.DISABLED);
    +            persister = new ManagementPlaneSyncRecordPersisterToObjectStore(mgmt, objectStore,
classLoader);
    +            ((ManagementPlaneSyncRecordPersisterToObjectStore)persister).allowRemoteTimestampInMemento();
    +            BrooklynMementoPersisterToObjectStore persisterObj = new BrooklynMementoPersisterToObjectStore(objectStore,
mgmt.getBrooklynProperties(), classLoader);
    +            ((RebindManagerImpl)mgmt.getRebindManager()).setPeriodicPersistPeriod(persistOrRebindPeriod);
    +            mgmt.getRebindManager().setPersister(persisterObj, PersistenceExceptionHandlerImpl.builder().build());
    +            ha = ((HighAvailabilityManagerImpl)mgmt.getHighAvailabilityManager())
    +                .setPollPeriod(Duration.PRACTICALLY_FOREVER)
    +                .setHeartbeatTimeout(Duration.THIRTY_SECONDS)
    +                .setPersister(persister);
    +            log.info("Created "+nodeName+" "+ownNodeId);
    +        }
    +        
    +        public void tearDownThisOnly() throws Exception {
    +            if (ha != null) ha.stop();
    +            if (mgmt!=null) mgmt.getRebindManager().stop();
    +            if (mgmt != null) Entities.destroyAll(mgmt);
    +        }
    +        
    +        public void tearDownAll() throws Exception {
    +            tearDownThisOnly();
    +            // can't delete the object store until all being torn down
    +            if (objectStore != null) objectStore.deleteCompletely();
    +        }
    +        
    +        @Override
    +        public String toString() {
    +            return nodeName+" "+ownNodeId;
    +        }
    +
    +        public RebindManagerImpl rebinder() {
    +            return (RebindManagerImpl)mgmt.getRebindManager();
    +        }
    +    }
    +    
    +    @BeforeMethod(alwaysRun=true)
    +    public void setUp() throws Exception {
    +        nodes.clear();
    +        sharedBackingStore.clear();
    +    }
    +    
    +    public HaMgmtNode newNode(Duration persistOrRebindPeriod) throws Exception {
    +        HaMgmtNode node = new HaMgmtNode();
    +        node.persistOrRebindPeriod = persistOrRebindPeriod;
    +        node.setUp();
    +        nodes.add(node);
    +        return node;
    +    }
    +
    +    @AfterMethod(alwaysRun=true)
    +    public void tearDown() throws Exception {
    +        for (HaMgmtNode n: nodes)
    +            n.tearDownAll();
    +    }
    +
    +    protected ManagementContextInternal newLocalManagementContext() {
    +        return new LocalManagementContextForTests();
    +    }
    +
    +    protected PersistenceObjectStore newPersistenceObjectStore() {
    +        return new InMemoryObjectStore(sharedBackingStore, sharedBackingStoreDates);
    +    }
    +
    +    private HaMgmtNode createMaster(Duration persistOrRebindPeriod) throws Exception
{
    +        HaMgmtNode n1 = newNode(persistOrRebindPeriod);
    +        n1.ha.start(HighAvailabilityMode.AUTO);
    +        assertEquals(n1.ha.getNodeState(), ManagementNodeState.MASTER);
    +        return n1;
    +    }
    +    
    +    private HaMgmtNode createHotStandby(Duration rebindPeriod) throws Exception {
    +        HaMgmtNode n2 = newNode(rebindPeriod);
    +        n2.ha.start(HighAvailabilityMode.HOT_STANDBY);
    +        assertEquals(n2.ha.getNodeState(), ManagementNodeState.HOT_STANDBY);
    +        return n2;
    +    }
    +
    +    private TestApplication createFirstAppAndPersist(HaMgmtNode n1) throws Exception
{
    +        TestApplication app = TestApplication.Factory.newManagedInstanceForTests(n1.mgmt);
    +        // for testing without enrichers, if desired:
    +//        TestApplication app = ApplicationBuilder.newManagedApp(EntitySpec.create(TestApplication.class).impl(TestApplicationNoEnrichersImpl.class),
n1.mgmt);
    +        app.setDisplayName("First App");
    +        app.start(MutableList.<Location>of());
    +        app.setConfig(TestEntity.CONF_NAME, "first-app");
    +        app.setAttribute(TestEntity.SEQUENCE, 3);
    +        
    +        forcePersistNow(n1);
    +        return app;
    +    }
    +
    +    protected void forcePersistNow(HaMgmtNode n1) {
    +        n1.mgmt.getRebindManager().forcePersistNow();
    +    }
    +    
    +    private Application expectRebindSequenceNumber(HaMgmtNode master, HaMgmtNode hotStandby,
Application app, int expectedSensorSequenceValue, boolean immediate) {
    +        Application appRO = hotStandby.mgmt.lookup(app.getId(), Application.class);
    +
    +        if (immediate) {
    +            forcePersistNow(master);
    +            forceRebindNow(hotStandby);
    +            EntityTestUtils.assertAttributeEquals(appRO, TestEntity.SEQUENCE, expectedSensorSequenceValue);
    +        } else {
    +            EntityTestUtils.assertAttributeEqualsEventually(appRO, TestEntity.SEQUENCE,
expectedSensorSequenceValue);
    +        }
    +        
    +        log.info("got sequence number "+expectedSensorSequenceValue+" from "+appRO);
    +        
    +        // make sure the instance (proxy) is unchanged
    +        Application appRO2 = hotStandby.mgmt.lookup(app.getId(), Application.class);
    +        Assert.assertTrue(appRO2==appRO);
    +        
    +        return appRO;
    +    }
    +
    +    private void forceRebindNow(HaMgmtNode hotStandby) {
    +        hotStandby.mgmt.getRebindManager().rebind(null, null, ManagementNodeState.HOT_STANDBY);
    +    }
    +    
    +    @Test
    +    public void testHotStandbySeesChangedNameConfigAndSensorValueButDoesntAllowChange()
throws Exception {
    +        HaMgmtNode n1 = createMaster(Duration.PRACTICALLY_FOREVER);
    +        TestApplication app = createFirstAppAndPersist(n1);
    +        HaMgmtNode n2 = createHotStandby(Duration.PRACTICALLY_FOREVER);
    +
    +        assertEquals(n2.mgmt.getApplications().size(), 1);
    +        Application appRO = n2.mgmt.lookup(app.getId(), Application.class);
    +        Assert.assertNotNull(appRO);
    +        Assert.assertTrue(appRO instanceof TestApplication);
    +        assertEquals(appRO.getDisplayName(), "First App");
    +        assertEquals(appRO.getConfig(TestEntity.CONF_NAME), "first-app");
    +        assertEquals(appRO.getAttribute(TestEntity.SEQUENCE), (Integer)3);
    +
    +        try {
    +            ((TestApplication)appRO).setAttribute(TestEntity.SEQUENCE, 4);
    +            Assert.fail("Should not have allowed sensor to be set");
    +        } catch (Exception e) {
    +            Assert.assertTrue(e.toString().toLowerCase().contains("read-only"), "Error
message did not contain expected text: "+e);
    --- End diff --
    
    good add'l assertion.  i've added it.
    
    this test just tests the initial custom value. the following test asserts subsequent changes
and more complex ones.  i've renamed both to make that clearer, thx.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message