EdColeman commented on code in PR #2569: URL: https://github.com/apache/accumulo/pull/2569#discussion_r858736346
########## test/src/main/java/org/apache/accumulo/test/conf/util/TransformTokenTest.java: ########## @@ -0,0 +1,171 @@ +/* + * 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.accumulo.test.conf.util; + +import static java.nio.charset.StandardCharsets.UTF_8; +import static org.apache.accumulo.harness.AccumuloITBase.ZOOKEEPER_TESTING_SERVER; +import static org.easymock.EasyMock.createMock; +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.replay; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.File; +import java.util.List; +import java.util.UUID; + +import org.apache.accumulo.core.Constants; +import org.apache.accumulo.core.data.InstanceId; +import org.apache.accumulo.fate.zookeeper.ZooReaderWriter; +import org.apache.accumulo.fate.zookeeper.ZooUtil; +import org.apache.accumulo.server.ServerContext; +import org.apache.accumulo.server.conf.store.PropCacheKey; +import org.apache.accumulo.server.conf.store.PropStoreException; +import org.apache.accumulo.server.conf.store.impl.PropStoreWatcher; +import org.apache.accumulo.server.conf.store.impl.ZooPropStore; +import org.apache.accumulo.server.conf.util.TransformToken; +import org.apache.accumulo.test.zookeeper.ZooKeeperTestingServer; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.ZKUtil; +import org.apache.zookeeper.ZooKeeper; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +@Tag(ZOOKEEPER_TESTING_SERVER) +public class TransformTokenTest { + + @TempDir + private static File tempDir; + + private static ZooKeeperTestingServer testZk = null; + private static ZooKeeper zooKeeper; + private static ZooReaderWriter zrw; + private InstanceId instanceId = null; + + @BeforeAll + public static void setupZk() { + + // using default zookeeper port - we don't have a full configuration + testZk = new ZooKeeperTestingServer(tempDir); + zooKeeper = testZk.getZooKeeper(); + zrw = testZk.getZooReaderWriter(); + } + + @AfterAll + public static void shutdownZK() throws Exception { + testZk.close(); + } + + @BeforeEach + public void testSetup() { + instanceId = InstanceId.of(UUID.randomUUID()); + } + + @AfterEach + public void cleanupZnodes() { + try { + ZKUtil.deleteRecursive(zooKeeper, Constants.ZROOT); + } catch (KeeperException | InterruptedException ex) { + throw new IllegalStateException("Failed to clean-up test zooKeeper nodes.", ex); + } + } + + @Test + public void tokenGoPathTest() throws Exception { + + List<LegacyPropData.PropNode> nodes = LegacyPropData.getData(instanceId); + for (LegacyPropData.PropNode node : nodes) { + zrw.putPersistentData(node.getPath(), node.getData(), ZooUtil.NodeExistsPolicy.SKIP); + } + + ZooPropStore propStore = new ZooPropStore.Builder(instanceId, zrw, 30_000).build(); + + ServerContext context = createMock(ServerContext.class); + expect(context.getInstanceID()).andReturn(instanceId).anyTimes(); + expect(context.getZooReaderWriter()).andReturn(zrw).anyTimes(); + expect(context.getPropStore()).andReturn(propStore).anyTimes(); + + PropStoreWatcher watcher = createMock(PropStoreWatcher.class); + + replay(context, watcher); + + var sysPropKey = PropCacheKey.forSystem(instanceId); + + TransformToken token = TransformToken.createToken(sysPropKey, zrw); + + assertTrue(token.haveToken()); + token.releaseToken(); + assertFalse(token.haveToken()); + + // relock by getting a new lock + TransformToken lock2 = TransformToken.createToken(sysPropKey, zrw); + assertTrue(lock2.haveToken()); + + // fail with a current lock node present + TransformToken lock3 = TransformToken.createToken(sysPropKey, zrw); + assertFalse(lock3.haveToken()); + Review Comment: fixed in 79f3e74ea6 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
