GWphua commented on code in PR #17482:
URL: https://github.com/apache/druid/pull/17482#discussion_r1972925211


##########
server/src/test/java/org/apache/druid/curator/announcement/NodeAnnouncerTest.java:
##########
@@ -0,0 +1,391 @@
+/*
+ * 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.druid.curator.announcement;
+
+import org.apache.curator.framework.CuratorFramework;
+import org.apache.curator.framework.api.CuratorEvent;
+import org.apache.curator.framework.api.CuratorEventType;
+import org.apache.curator.framework.api.CuratorListener;
+import org.apache.curator.framework.api.transaction.CuratorOp;
+import org.apache.curator.framework.api.transaction.CuratorTransactionResult;
+import org.apache.curator.test.KillSession;
+import org.apache.druid.curator.CuratorTestBase;
+import org.apache.druid.java.util.common.IAE;
+import org.apache.druid.java.util.common.ISE;
+import org.apache.druid.java.util.common.StringUtils;
+import org.apache.druid.java.util.common.concurrent.Execs;
+import org.apache.druid.java.util.common.logger.Logger;
+import org.apache.druid.server.ZKPathsUtils;
+import org.apache.zookeeper.KeeperException.Code;
+import org.apache.zookeeper.data.Stat;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutorService;
+
+/**
+ *
+ */
+public class NodeAnnouncerTest extends CuratorTestBase
+{
+  private static final Logger log = new Logger(NodeAnnouncerTest.class);
+  private ExecutorService exec;
+
+  @Before
+  public void setUp() throws Exception
+  {
+    setupServerAndCurator();
+    exec = Execs.singleThreaded("test-node-announcer-sanity-%s");
+    curator.start();
+    curator.blockUntilConnected();
+  }
+
+  @After
+  public void tearDown()
+  {
+    tearDownServerAndCurator();
+  }
+
+  @Test(timeout = 60_000L)
+  public void testCreateParentPath() throws Exception
+  {
+    NodeAnnouncer announcer = new NodeAnnouncer(curator, exec);
+    final byte[] billy = StringUtils.toUtf8("billy");
+    final String testPath = "/newParent/testPath";
+    final String parentPath = ZKPathsUtils.getParentPath(testPath);
+
+    announcer.start();
+    Assert.assertNull("Parent path should not exist before announcement", 
curator.checkExists().forPath(parentPath));
+    announcer.announce(testPath, billy);
+
+    // Wait for the announcement to be processed
+    while (curator.checkExists().forPath(testPath) == null) {
+      Thread.sleep(100);
+    }
+
+    Assert.assertNotNull("Parent path should be created", 
curator.checkExists().forPath(parentPath));
+    Assert.assertArrayEquals(billy, 
curator.getData().decompressed().forPath(testPath));
+    announcer.stop();
+  }
+
+  @Test(timeout = 60_000L)
+  public void testAnnounceSamePathWithDifferentPayloadThrowsIAE() throws 
Exception
+  {
+    NodeAnnouncer announcer = new NodeAnnouncer(curator, exec);
+    final byte[] billy = StringUtils.toUtf8("billy");
+    final byte[] tilly = StringUtils.toUtf8("tilly");
+    final String testPath = "/testPath";
+
+    announcer.start();
+    announcer.announce(testPath, billy);
+    while (curator.checkExists().forPath(testPath) == null) {
+      Thread.sleep(100);
+    }
+    Assert.assertArrayEquals(billy, 
curator.getData().decompressed().forPath(testPath));
+
+    // Nothing wrong when we announce same path.
+    announcer.announce(testPath, billy);
+
+    // Something wrong when we announce different path.
+    Exception exception = Assert.assertThrows(IAE.class, () -> 
announcer.announce(testPath, tilly));
+    Assert.assertEquals(exception.getMessage(), "Cannot reannounce different 
values under the same path.");
+
+    // Confirm that the new announcement is invalidated, and we still have 
payload from previous announcement.
+    Assert.assertArrayEquals(billy, 
curator.getData().decompressed().forPath(testPath));
+    announcer.stop();
+  }
+
+  @Test
+  public void testUpdateBeforeStartingNodeAnnouncer() throws Exception
+  {
+    NodeAnnouncer announcer = new NodeAnnouncer(curator, exec);
+    final byte[] billy = StringUtils.toUtf8("billy");
+    final byte[] tilly = StringUtils.toUtf8("tilly");
+    final String testPath = "/testAnnounce";
+
+    announcer.update(testPath, tilly);
+    announcer.announce(testPath, billy);
+    announcer.start();
+
+    // Verify that the path was announced
+    Assert.assertArrayEquals(tilly, 
curator.getData().decompressed().forPath(testPath));
+    announcer.stop();
+  }
+
+  @Test
+  public void testUpdateSuccessfully() throws Exception
+  {
+    NodeAnnouncer announcer = new NodeAnnouncer(curator, exec);
+    final byte[] billy = StringUtils.toUtf8("billy");
+    final byte[] tilly = StringUtils.toUtf8("tilly");
+    final String testPath = "/testUpdate";
+
+    announcer.start();
+    announcer.announce(testPath, billy);
+    Assert.assertArrayEquals(billy, 
curator.getData().decompressed().forPath(testPath));
+
+    announcer.update(testPath, billy);
+    Assert.assertArrayEquals(billy, 
curator.getData().decompressed().forPath(testPath));
+
+    announcer.update(testPath, tilly);
+    Assert.assertArrayEquals(tilly, 
curator.getData().decompressed().forPath(testPath));
+    announcer.stop();
+  }
+
+  @Test
+  public void testUpdateNonExistentPath()
+  {
+    NodeAnnouncer announcer = new NodeAnnouncer(curator, exec);
+    final byte[] billy = StringUtils.toUtf8("billy");
+    final String testPath = "/testUpdate";
+
+    announcer.start();
+
+    Exception exception = Assert.assertThrows(ISE.class, () -> 
announcer.update(testPath, billy));
+    Assert.assertEquals(exception.getMessage(), "Cannot update 
path[/testUpdate] that hasn't been announced!");
+    announcer.stop();
+  }
+
+  @Test(timeout = 60_000L)
+  public void testSanity() throws Exception
+  {
+    NodeAnnouncer announcer = new NodeAnnouncer(curator, exec);
+
+    final byte[] billy = StringUtils.toUtf8("billy");
+    final String testPath1 = "/test1";
+    final String testPath2 = "/somewhere/test2";
+    announcer.announce(testPath1, billy);
+
+    Assert.assertNull("/test1 does not exists", 
curator.checkExists().forPath(testPath1));
+    Assert.assertNull("/somewhere/test2 does not exists", 
curator.checkExists().forPath(testPath2));
+
+    announcer.start();
+    while (!announcer.getAddedPaths().contains("/test1")) {
+      Thread.sleep(100);

Review Comment:
   Using @test(timeout = 60_000L) at L171 will be able to handle this. 👍
   
   



-- 
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]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to