org.apache.curator.framework.recipes.cache.TreeCacheEvent.Type Java Examples

The following examples show how to use org.apache.curator.framework.recipes.cache.TreeCacheEvent.Type. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestTreeCache.java    From curator with Apache License 2.0 6 votes vote down vote up
@Test
public void testBasicsWithNoZkWatches() throws Exception
{
    client.create().forPath("/test");
    client.create().forPath("/test/one", "hey there".getBytes());

    cache = buildWithListeners(TreeCache.newBuilder(client, "/test").disableZkWatches(true));

    cache.start();
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test");
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/one");

    assertEvent(TreeCacheEvent.Type.INITIALIZED);
    Assert.assertEquals(cache.getCurrentChildren("/test").keySet(), ImmutableSet.of("one"));
    Assert.assertEquals(new String(cache.getCurrentData("/test/one").getData()), "hey there");
    Assert.assertEquals(cache.getCurrentChildren("/test/one").keySet(), ImmutableSet.of());
    Assert.assertNull(cache.getCurrentChildren("/test/o"));
    Assert.assertNull(cache.getCurrentChildren("/test/onely"));
    Assert.assertNull(cache.getCurrentChildren("/t"));
    Assert.assertNull(cache.getCurrentChildren("/testing"));

    assertNoMoreEvents();
}
 
Example #2
Source File: TestTreeCache.java    From curator with Apache License 2.0 6 votes vote down vote up
@Test
public void testDepth1Deeper() throws Exception
{
    client.create().forPath("/test");
    client.create().forPath("/test/foo");
    client.create().forPath("/test/foo/bar");
    client.create().forPath("/test/foo/bar/1", "one".getBytes());
    client.create().forPath("/test/foo/bar/2", "two".getBytes());
    client.create().forPath("/test/foo/bar/3", "three".getBytes());
    client.create().forPath("/test/foo/bar/2/sub", "two-sub".getBytes());

    cache = buildWithListeners(TreeCache.newBuilder(client, "/test/foo/bar").setMaxDepth(1));
    cache.start();
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/foo/bar");
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/foo/bar/1", "one".getBytes());
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/foo/bar/2", "two".getBytes());
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/foo/bar/3", "three".getBytes());
    assertEvent(TreeCacheEvent.Type.INITIALIZED);
    assertNoMoreEvents();
}
 
Example #3
Source File: TestTreeCache.java    From xian with Apache License 2.0 6 votes vote down vote up
@Test
public void testStartup() throws Exception
{
    client.create().forPath("/test");
    client.create().forPath("/test/1", "one".getBytes());
    client.create().forPath("/test/2", "two".getBytes());
    client.create().forPath("/test/3", "three".getBytes());
    client.create().forPath("/test/2/sub", "two-sub".getBytes());

    cache = newTreeCacheWithListeners(client, "/test");
    cache.start();
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test");
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/1", "one".getBytes());
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/2", "two".getBytes());
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/3", "three".getBytes());
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/2/sub", "two-sub".getBytes());
    assertEvent(TreeCacheEvent.Type.INITIALIZED);
    assertNoMoreEvents();

    Assert.assertEquals(cache.getCurrentChildren("/test").keySet(), ImmutableSet.of("1", "2", "3"));
    Assert.assertEquals(cache.getCurrentChildren("/test/1").keySet(), ImmutableSet.of());
    Assert.assertEquals(cache.getCurrentChildren("/test/2").keySet(), ImmutableSet.of("sub"));
    Assert.assertNull(cache.getCurrentChildren("/test/non_exist"));
}
 
Example #4
Source File: TestTreeCache.java    From curator with Apache License 2.0 6 votes vote down vote up
@Test
public void testFromRootWithDepth() throws Exception
{
    client.create().forPath("/test");
    client.create().forPath("/test/one", "hey there".getBytes());

    cache = buildWithListeners(TreeCache.newBuilder(client, "/").setMaxDepth(1));
    cache.start();
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/");
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test");
    assertEvent(TreeCacheEvent.Type.INITIALIZED);
    assertNoMoreEvents();

    Assert.assertTrue(cache.getCurrentChildren("/").keySet().contains("test"));
    Assert.assertEquals(cache.getCurrentChildren("/test").keySet(), ImmutableSet.of());
    Assert.assertNull(cache.getCurrentData("/test/one"));
    Assert.assertNull(cache.getCurrentChildren("/test/one"));
}
 
Example #5
Source File: TestTreeCache.java    From xian with Apache License 2.0 6 votes vote down vote up
@Test
public void testDepth1() throws Exception
{
    client.create().forPath("/test");
    client.create().forPath("/test/1", "one".getBytes());
    client.create().forPath("/test/2", "two".getBytes());
    client.create().forPath("/test/3", "three".getBytes());
    client.create().forPath("/test/2/sub", "two-sub".getBytes());

    cache = buildWithListeners(TreeCache.newBuilder(client, "/test").setMaxDepth(1));
    cache.start();
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test");
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/1", "one".getBytes());
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/2", "two".getBytes());
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/3", "three".getBytes());
    assertEvent(TreeCacheEvent.Type.INITIALIZED);
    assertNoMoreEvents();

    Assert.assertEquals(cache.getCurrentChildren("/test").keySet(), ImmutableSet.of("1", "2", "3"));
    Assert.assertEquals(cache.getCurrentChildren("/test/1").keySet(), ImmutableSet.of());
    Assert.assertEquals(cache.getCurrentChildren("/test/2").keySet(), ImmutableSet.of());
    Assert.assertNull(cache.getCurrentData("/test/2/sub"));
    Assert.assertNull(cache.getCurrentChildren("/test/2/sub"));
    Assert.assertNull(cache.getCurrentChildren("/test/non_exist"));
}
 
Example #6
Source File: TestTreeCache.java    From curator with Apache License 2.0 6 votes vote down vote up
@Test
public void testStartup() throws Exception
{
    client.create().forPath("/test");
    client.create().forPath("/test/1", "one".getBytes());
    client.create().forPath("/test/2", "two".getBytes());
    client.create().forPath("/test/3", "three".getBytes());
    client.create().forPath("/test/2/sub", "two-sub".getBytes());

    cache = newTreeCacheWithListeners(client, "/test");
    cache.start();
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test");
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/1", "one".getBytes());
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/2", "two".getBytes());
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/3", "three".getBytes());
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/2/sub", "two-sub".getBytes());
    assertEvent(TreeCacheEvent.Type.INITIALIZED);
    assertNoMoreEvents();

    Assert.assertEquals(cache.getCurrentChildren("/test").keySet(), ImmutableSet.of("1", "2", "3"));
    Assert.assertEquals(cache.getCurrentChildren("/test/1").keySet(), ImmutableSet.of());
    Assert.assertEquals(cache.getCurrentChildren("/test/2").keySet(), ImmutableSet.of("sub"));
    Assert.assertNull(cache.getCurrentChildren("/test/non_exist"));
}
 
Example #7
Source File: TestTreeCache.java    From curator with Apache License 2.0 6 votes vote down vote up
@Test
public void testDeleteThenCreate() throws Exception
{
    client.create().forPath("/test");
    client.create().forPath("/test/foo", "one".getBytes());

    cache = newTreeCacheWithListeners(client, "/test");
    cache.start();
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test");
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/foo");
    assertEvent(TreeCacheEvent.Type.INITIALIZED);

    client.delete().forPath("/test/foo");
    assertEvent(TreeCacheEvent.Type.NODE_REMOVED, "/test/foo", "one".getBytes());
    client.create().forPath("/test/foo", "two".getBytes());
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/foo");

    client.delete().forPath("/test/foo");
    assertEvent(TreeCacheEvent.Type.NODE_REMOVED, "/test/foo", "two".getBytes());
    client.create().forPath("/test/foo", "two".getBytes());
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/foo");

    assertNoMoreEvents();
}
 
Example #8
Source File: TestTreeCache.java    From xian with Apache License 2.0 6 votes vote down vote up
@Test
public void testFromRoot() throws Exception
{
    client.create().forPath("/test");
    client.create().forPath("/test/one", "hey there".getBytes());

    cache = newTreeCacheWithListeners(client, "/");
    cache.start();
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/");
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test");
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/one");
    assertEvent(TreeCacheEvent.Type.INITIALIZED);
    assertNoMoreEvents();

    Assert.assertTrue(cache.getCurrentChildren("/").keySet().contains("test"));
    Assert.assertEquals(cache.getCurrentChildren("/test").keySet(), ImmutableSet.of("one"));
    Assert.assertEquals(cache.getCurrentChildren("/test/one").keySet(), ImmutableSet.of());
    Assert.assertEquals(new String(cache.getCurrentData("/test/one").getData()), "hey there");
}
 
Example #9
Source File: TestTreeCache.java    From xian with Apache License 2.0 6 votes vote down vote up
@Test
public void testFromRootWithDepth() throws Exception
{
    client.create().forPath("/test");
    client.create().forPath("/test/one", "hey there".getBytes());

    cache = buildWithListeners(TreeCache.newBuilder(client, "/").setMaxDepth(1));
    cache.start();
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/");
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test");
    assertEvent(TreeCacheEvent.Type.INITIALIZED);
    assertNoMoreEvents();

    Assert.assertTrue(cache.getCurrentChildren("/").keySet().contains("test"));
    Assert.assertEquals(cache.getCurrentChildren("/test").keySet(), ImmutableSet.of());
    Assert.assertNull(cache.getCurrentData("/test/one"));
    Assert.assertNull(cache.getCurrentChildren("/test/one"));
}
 
Example #10
Source File: TestTreeCache.java    From xian with Apache License 2.0 6 votes vote down vote up
@Test
public void testWithNamespace() throws Exception
{
    client.create().forPath("/outer");
    client.create().forPath("/outer/foo");
    client.create().forPath("/outer/test");
    client.create().forPath("/outer/test/one", "hey there".getBytes());

    cache = newTreeCacheWithListeners(client.usingNamespace("outer"), "/test");
    cache.start();
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test");
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/one");
    assertEvent(TreeCacheEvent.Type.INITIALIZED);
    assertNoMoreEvents();

    Assert.assertEquals(cache.getCurrentChildren("/test").keySet(), ImmutableSet.of("one"));
    Assert.assertEquals(cache.getCurrentChildren("/test/one").keySet(), ImmutableSet.of());
    Assert.assertEquals(new String(cache.getCurrentData("/test/one").getData()), "hey there");
}
 
Example #11
Source File: TestTreeCache.java    From xian with Apache License 2.0 6 votes vote down vote up
@Test
public void testWithNamespaceAtRoot() throws Exception
{
    client.create().forPath("/outer");
    client.create().forPath("/outer/foo");
    client.create().forPath("/outer/test");
    client.create().forPath("/outer/test/one", "hey there".getBytes());

    cache = newTreeCacheWithListeners(client.usingNamespace("outer"), "/");
    cache.start();
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/");
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/foo");
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test");
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/one");
    assertEvent(TreeCacheEvent.Type.INITIALIZED);
    assertNoMoreEvents();
    Assert.assertEquals(cache.getCurrentChildren("/").keySet(), ImmutableSet.of("foo", "test"));
    Assert.assertEquals(cache.getCurrentChildren("/foo").keySet(), ImmutableSet.of());
    Assert.assertEquals(cache.getCurrentChildren("/test").keySet(), ImmutableSet.of("one"));
    Assert.assertEquals(cache.getCurrentChildren("/test/one").keySet(), ImmutableSet.of());
    Assert.assertEquals(new String(cache.getCurrentData("/test/one").getData()), "hey there");
}
 
Example #12
Source File: TestTreeCache.java    From xian with Apache License 2.0 6 votes vote down vote up
@Test
public void testChildrenInitialized() throws Exception
{
    client.create().forPath("/test", "".getBytes());
    client.create().forPath("/test/1", "1".getBytes());
    client.create().forPath("/test/2", "2".getBytes());
    client.create().forPath("/test/3", "3".getBytes());

    cache = newTreeCacheWithListeners(client, "/test");
    cache.start();
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test");
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/1");
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/2");
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/3");
    assertEvent(TreeCacheEvent.Type.INITIALIZED);
    assertNoMoreEvents();
}
 
Example #13
Source File: TestTreeCache.java    From curator with Apache License 2.0 6 votes vote down vote up
@Test
public void testChildrenInitialized() throws Exception
{
    client.create().forPath("/test", "".getBytes());
    client.create().forPath("/test/1", "1".getBytes());
    client.create().forPath("/test/2", "2".getBytes());
    client.create().forPath("/test/3", "3".getBytes());

    cache = newTreeCacheWithListeners(client, "/test");
    cache.start();
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test");
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/1");
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/2");
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/3");
    assertEvent(TreeCacheEvent.Type.INITIALIZED);
    assertNoMoreEvents();
}
 
Example #14
Source File: TestTreeCache.java    From xian with Apache License 2.0 6 votes vote down vote up
@Test
public void testDeleteThenCreate() throws Exception
{
    client.create().forPath("/test");
    client.create().forPath("/test/foo", "one".getBytes());

    cache = newTreeCacheWithListeners(client, "/test");
    cache.start();
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test");
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/foo");
    assertEvent(TreeCacheEvent.Type.INITIALIZED);

    client.delete().forPath("/test/foo");
    assertEvent(TreeCacheEvent.Type.NODE_REMOVED, "/test/foo", "one".getBytes());
    client.create().forPath("/test/foo", "two".getBytes());
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/foo");

    client.delete().forPath("/test/foo");
    assertEvent(TreeCacheEvent.Type.NODE_REMOVED, "/test/foo", "two".getBytes());
    client.create().forPath("/test/foo", "two".getBytes());
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/foo");

    assertNoMoreEvents();
}
 
Example #15
Source File: TestTreeCache.java    From xian with Apache License 2.0 6 votes vote down vote up
@Test
public void testDeleteThenCreateRoot() throws Exception
{
    client.create().forPath("/test");
    client.create().forPath("/test/foo", "one".getBytes());

    cache = newTreeCacheWithListeners(client, "/test/foo");
    cache.start();
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/foo");
    assertEvent(TreeCacheEvent.Type.INITIALIZED);

    client.delete().forPath("/test/foo");
    assertEvent(TreeCacheEvent.Type.NODE_REMOVED, "/test/foo");
    client.create().forPath("/test/foo", "two".getBytes());
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/foo");

    client.delete().forPath("/test/foo");
    assertEvent(TreeCacheEvent.Type.NODE_REMOVED, "/test/foo");
    client.create().forPath("/test/foo", "two".getBytes());
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/foo");

    assertNoMoreEvents();
}
 
Example #16
Source File: TestTreeCache.java    From xian with Apache License 2.0 6 votes vote down vote up
@Test
public void testKilledSession() throws Exception
{
    client.create().forPath("/test");

    cache = newTreeCacheWithListeners(client, "/test");
    cache.start();
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test");
    assertEvent(TreeCacheEvent.Type.INITIALIZED);

    client.create().forPath("/test/foo", "foo".getBytes());
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/foo");
    client.create().withMode(CreateMode.EPHEMERAL).forPath("/test/me", "data".getBytes());
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/me");

    KillSession.kill(client.getZookeeperClient().getZooKeeper(), server.getConnectString());
    assertEvent(TreeCacheEvent.Type.CONNECTION_SUSPENDED);
    assertEvent(TreeCacheEvent.Type.CONNECTION_LOST);
    assertEvent(TreeCacheEvent.Type.CONNECTION_RECONNECTED);
    assertEvent(TreeCacheEvent.Type.NODE_REMOVED, "/test/me", "data".getBytes());
    assertEvent(TreeCacheEvent.Type.INITIALIZED);

    assertNoMoreEvents();
}
 
Example #17
Source File: TestTreeCache.java    From curator with Apache License 2.0 6 votes vote down vote up
@Test
public void testKilledSession() throws Exception
{
    client.create().forPath("/test");

    cache = newTreeCacheWithListeners(client, "/test");
    cache.start();
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test");
    assertEvent(TreeCacheEvent.Type.INITIALIZED);

    client.create().forPath("/test/foo", "foo".getBytes());
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/foo");
    client.create().withMode(CreateMode.EPHEMERAL).forPath("/test/me", "data".getBytes());
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/me");

    client.getZookeeperClient().getZooKeeper().getTestable().injectSessionExpiration();
    assertEvent(TreeCacheEvent.Type.INITIALIZED, null, null, true);
    assertEvent(TreeCacheEvent.Type.NODE_REMOVED, "/test/me", "data".getBytes(), true);

    assertNoMoreEvents();
}
 
Example #18
Source File: TestTreeCache.java    From curator with Apache License 2.0 6 votes vote down vote up
@Test
public void testDeleteThenCreateRoot() throws Exception
{
    client.create().forPath("/test");
    client.create().forPath("/test/foo", "one".getBytes());

    cache = newTreeCacheWithListeners(client, "/test/foo");
    cache.start();
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/foo");
    assertEvent(TreeCacheEvent.Type.INITIALIZED);

    client.delete().forPath("/test/foo");
    assertEvent(TreeCacheEvent.Type.NODE_REMOVED, "/test/foo");
    client.create().forPath("/test/foo", "two".getBytes());
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/foo");

    client.delete().forPath("/test/foo");
    assertEvent(TreeCacheEvent.Type.NODE_REMOVED, "/test/foo");
    client.create().forPath("/test/foo", "two".getBytes());
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/foo");

    assertNoMoreEvents();
}
 
Example #19
Source File: TestTreeCache.java    From curator with Apache License 2.0 6 votes vote down vote up
@Test
public void testUpdateWhenNotCachingData() throws Exception
{
    client.create().forPath("/test");

    cache = buildWithListeners(TreeCache.newBuilder(client, "/test").setCacheData(false));
    cache.start();
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test");
    assertEvent(TreeCacheEvent.Type.INITIALIZED);

    client.create().forPath("/test/foo", "first".getBytes());
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/foo");

    client.setData().forPath("/test/foo", "something new".getBytes());
    assertEvent(TreeCacheEvent.Type.NODE_UPDATED, "/test/foo");
    assertNoMoreEvents();

    Assert.assertNotNull(cache.getCurrentData("/test/foo"));
    // No byte data querying the tree because we're not caching data.
    Assert.assertNull(cache.getCurrentData("/test/foo").getData());
}
 
Example #20
Source File: TestTreeCache.java    From curator with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateParents() throws Exception
{
    cache = newTreeCacheWithListeners(client, "/one/two/three");
    cache.start();
    assertEvent(TreeCacheEvent.Type.INITIALIZED);
    assertNoMoreEvents();
    Assert.assertNull(client.checkExists().forPath("/one/two/three"));
    cache.close();

    cache = buildWithListeners(TreeCache.newBuilder(client, "/one/two/three").setCreateParentNodes(true));
    cache.start();
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/one/two/three");
    assertEvent(TreeCacheEvent.Type.INITIALIZED);
    assertNoMoreEvents();
    Assert.assertNotNull(client.checkExists().forPath("/one/two/three"));
}
 
Example #21
Source File: TestTreeCache.java    From curator with Apache License 2.0 6 votes vote down vote up
@Test
public void testDepth0() throws Exception
{
    client.create().forPath("/test");
    client.create().forPath("/test/1", "one".getBytes());
    client.create().forPath("/test/2", "two".getBytes());
    client.create().forPath("/test/3", "three".getBytes());
    client.create().forPath("/test/2/sub", "two-sub".getBytes());

    cache = buildWithListeners(TreeCache.newBuilder(client, "/test").setMaxDepth(0));
    cache.start();
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test");
    assertEvent(TreeCacheEvent.Type.INITIALIZED);
    assertNoMoreEvents();

    Assert.assertEquals(cache.getCurrentChildren("/test").keySet(), ImmutableSet.of());
    Assert.assertNull(cache.getCurrentData("/test/1"));
    Assert.assertNull(cache.getCurrentChildren("/test/1"));
    Assert.assertNull(cache.getCurrentData("/test/non_exist"));
}
 
Example #22
Source File: TestTreeCache.java    From curator with Apache License 2.0 6 votes vote down vote up
@Test
public void testFromRoot() throws Exception
{
    client.create().forPath("/test");
    client.create().forPath("/test/one", "hey there".getBytes());

    cache = newTreeCacheWithListeners(client, "/");
    cache.start();
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/");
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test");
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/one");
    assertEvent(TreeCacheEvent.Type.INITIALIZED);
    assertNoMoreEvents();

    Assert.assertTrue(cache.getCurrentChildren("/").keySet().contains("test"));
    Assert.assertEquals(cache.getCurrentChildren("/test").keySet(), ImmutableSet.of("one"));
    Assert.assertEquals(cache.getCurrentChildren("/test/one").keySet(), ImmutableSet.of());
    Assert.assertEquals(new String(cache.getCurrentData("/test/one").getData()), "hey there");
}
 
Example #23
Source File: TestTreeCache.java    From curator with Apache License 2.0 6 votes vote down vote up
/**
 * Make sure TreeCache gets to a sane state when we can't initially connect to server.
 */
@Test
public void testServerNotStartedYet() throws Exception
{
    // Stop the existing server.
    server.stop();

    // Shutdown the existing client and re-create it started.
    client.close();
    initCuratorFramework();

    // Start the client disconnected.
    cache = newTreeCacheWithListeners(client, "/test");
    cache.start();
    assertNoMoreEvents();

    // Now restart the server.
    server.restart();
    assertEvent(TreeCacheEvent.Type.INITIALIZED);

    client.create().forPath("/test");

    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test");
    assertNoMoreEvents();
}
 
Example #24
Source File: ElectionListenerManagerTest.java    From shardingsphere-elasticjob-lite with Apache License 2.0 5 votes vote down vote up
@Test
public void assertLeaderElectionWhenRemoveLeaderInstancePathWithAvailableServer() {
    JobRegistry.getInstance().registerJob("test_job", jobScheduleController, regCenter);
    when(serverService.isAvailableServer("127.0.0.1")).thenReturn(true);
    electionListenerManager.new LeaderElectionJobListener().dataChanged("/test_job/leader/election/instance", Type.NODE_REMOVED, "127.0.0.1");
    verify(leaderService).electLeader();
    JobRegistry.getInstance().shutdown("test_job");
}
 
Example #25
Source File: TestTreeCache.java    From curator with Apache License 2.0 5 votes vote down vote up
@Test
public void testSyncInitialPopulation() throws Exception
{
    cache = newTreeCacheWithListeners(client, "/test");
    cache.start();
    assertEvent(TreeCacheEvent.Type.INITIALIZED);

    client.create().forPath("/test");
    client.create().forPath("/test/one", "hey there".getBytes());
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test");
    assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/one");
    assertNoMoreEvents();
}
 
Example #26
Source File: ShardingListenerManagerTest.java    From shardingsphere-elasticjob-lite with Apache License 2.0 5 votes vote down vote up
@Test
public void assertShardingTotalCountChangedJobListenerWhenIsConfigPathAndCurrentShardingTotalCountIsNotEqualToNewShardingTotalCount() {
    JobRegistry.getInstance().setCurrentShardingTotalCount("test_job", 5);
    shardingListenerManager.new ShardingTotalCountChangedJobListener().dataChanged("/test_job/config", Type.NODE_UPDATED, LiteYamlConstants.getJobYaml());
    verify(shardingService).setReshardingFlag();
    JobRegistry.getInstance().setCurrentShardingTotalCount("test_job", 0);
}
 
Example #27
Source File: ShardingListenerManagerTest.java    From shardingsphere-elasticjob-lite with Apache License 2.0 5 votes vote down vote up
@Test
public void assertListenServersChangedJobListenerWhenIsInstanceChange() {
    JobRegistry.getInstance().registerJob("test_job", jobScheduleController, regCenter);
    shardingListenerManager.new ListenServersChangedJobListener().dataChanged("/test_job/instances/xxx", Type.NODE_ADDED, "");
    verify(shardingService).setReshardingFlag();
    JobRegistry.getInstance().shutdown("test_job");
}
 
Example #28
Source File: FailoverListenerManagerTest.java    From shardingsphere-elasticjob-lite with Apache License 2.0 5 votes vote down vote up
@Test
public void assertJobCrashedJobListenerWhenIsSameInstance() {
    JobRegistry.getInstance().addJobInstance("test_job", new JobInstance("127.0.0.1@-@0"));
    when(configService.load(true)).thenReturn(JobConfiguration.newBuilder("test_job", JobType.SIMPLE, "0/1 * * * * ?", 3).failover(true).build());
    failoverListenerManager.new JobCrashedJobListener().dataChanged("/test_job/instances/127.0.0.1@-@0", Type.NODE_REMOVED, "");
    verify(failoverService, times(0)).failoverIfNecessary();
    JobRegistry.getInstance().shutdown("test_job");
}
 
Example #29
Source File: FailoverListenerManagerTest.java    From shardingsphere-elasticjob-lite with Apache License 2.0 5 votes vote down vote up
@Test
public void assertJobCrashedJobListenerWhenIsOtherInstanceCrashed() {
    JobRegistry.getInstance().addJobInstance("test_job", new JobInstance("127.0.0.1@-@0"));
    when(configService.load(true)).thenReturn(JobConfiguration.newBuilder("test_job", JobType.SIMPLE, "0/1 * * * * ?", 3).failover(true).build());
    when(shardingService.getShardingItems("127.0.0.1@-@1")).thenReturn(Arrays.asList(0, 2));
    failoverListenerManager.new JobCrashedJobListener().dataChanged("/test_job/instances/127.0.0.1@-@1", Type.NODE_REMOVED, "");
    verify(failoverService).setCrashedFailoverFlag(0);
    verify(failoverService).setCrashedFailoverFlag(2);
    verify(failoverService, times(2)).failoverIfNecessary();
    JobRegistry.getInstance().shutdown("test_job");
}
 
Example #30
Source File: ShardingListenerManager.java    From shardingsphere-elasticjob-lite with Apache License 2.0 5 votes vote down vote up
@Override
protected void dataChanged(final String path, final Type eventType, final String data) {
    if (configNode.isConfigPath(path) && 0 != JobRegistry.getInstance().getCurrentShardingTotalCount(jobName)) {
        int newShardingTotalCount = YamlEngine.unmarshal(data, YamlJobConfiguration.class).toJobConfiguration().getShardingTotalCount();
        if (newShardingTotalCount != JobRegistry.getInstance().getCurrentShardingTotalCount(jobName)) {
            shardingService.setReshardingFlag();
            JobRegistry.getInstance().setCurrentShardingTotalCount(jobName, newShardingTotalCount);
        }
    }
}