use of org.apache.curator.framework.CuratorFramework in project heron by twitter.
the class CuratorStateManagerTest method testCreateNode.
/**
* test createNode method
* @throws Exception
*/
@Test
public void testCreateNode() throws Exception {
CuratorStateManager spyStateManager = spy(new CuratorStateManager());
CuratorFramework mockClient = mock(CuratorFramework.class);
CreateBuilder mockCreateBuilder = mock(CreateBuilder.class);
// Mockito doesn't support mock type-parametrized class, thus suppress the warning
@SuppressWarnings("rawtypes") ACLBackgroundPathAndBytesable mockPath = spy(ACLBackgroundPathAndBytesable.class);
final byte[] data = new byte[10];
doReturn(mockClient).when(spyStateManager).getCuratorClient();
doReturn(true).when(mockClient).blockUntilConnected(anyInt(), any(TimeUnit.class));
doReturn(mockCreateBuilder).when(mockClient).create();
doReturn(mockPath).when(mockCreateBuilder).withMode(any(CreateMode.class));
spyStateManager.initialize(config);
// Verify the node is created successfully
ListenableFuture<Boolean> result = spyStateManager.createNode(PATH, data, false);
verify(mockCreateBuilder).withMode(any(CreateMode.class));
verify(mockPath).forPath(PATH, data);
assertTrue(result.get());
}
use of org.apache.curator.framework.CuratorFramework in project heron by twitter.
the class CuratorStateManagerTest method testDeleteSchedulerLocation.
/**
* Test deleteSchedulerLocation method
* @throws Exception
*/
@Test
public void testDeleteSchedulerLocation() throws Exception {
CuratorStateManager spyStateManager = spy(new CuratorStateManager());
CuratorFramework mockClient = mock(CuratorFramework.class);
doReturn(mockClient).when(spyStateManager).getCuratorClient();
doReturn(true).when(mockClient).blockUntilConnected(anyInt(), any(TimeUnit.class));
spyStateManager.initialize(config);
final SettableFuture<Boolean> fakeResult = SettableFuture.create();
fakeResult.set(false);
doReturn(fakeResult).when(spyStateManager).deleteNode(anyString(), anyBoolean());
ListenableFuture<Boolean> result = spyStateManager.deleteSchedulerLocation(TOPOLOGY_NAME);
assertTrue(result.get());
}
use of org.apache.curator.framework.CuratorFramework in project heron by twitter.
the class CuratorStateManager method getNodeData.
@Override
protected <M extends Message> ListenableFuture<M> getNodeData(WatchCallback watcher, String path, final Message.Builder builder) {
final SettableFuture<M> future = SettableFuture.create();
Watcher wc = ZkWatcherCallback.makeZkWatcher(watcher);
BackgroundCallback cb = new BackgroundCallback() {
@Override
// we don't know what M is until runtime
@SuppressWarnings("unchecked")
public void processResult(CuratorFramework aClient, CuratorEvent event) throws Exception {
byte[] data;
if (event != null & (data = event.getData()) != null) {
builder.mergeFrom(data);
future.set((M) builder.build());
} else {
future.setException(new RuntimeException("Failed to fetch data from path: " + event.getPath()));
}
}
};
try {
client.getData().usingWatcher(wc).inBackground(cb).forPath(path);
// Suppress it since forPath() throws Exception
// SUPPRESS CHECKSTYLE IllegalCatch
} catch (Exception e) {
future.setException(new RuntimeException("Could not getNodeData", e));
}
return future;
}
use of org.apache.curator.framework.CuratorFramework in project camel by apache.
the class MasterEndpointFailoverTest method beforeRun.
@Before
public void beforeRun() throws Exception {
System.out.println("Starting ZK server!");
serverFactoryBean.setPort(9004);
serverFactoryBean.afterPropertiesSet();
// Create the zkClientBean
zkClientBean.setConnectString("localhost:9004");
CuratorFramework client = zkClientBean.getObject();
// Need to bind the zookeeper client with the name "curator"
SimpleRegistry registry = new SimpleRegistry();
registry.put("curator", client);
producerContext = new DefaultCamelContext(registry);
// Add the vm:start endpoint to avoid the NPE before starting the consumerContext1
producerContext.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:start").to("vm:start");
}
});
template = producerContext.createProducerTemplate();
consumerContext1 = new DefaultCamelContext(registry);
consumerContext1.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("zookeeper-master:MasterEndpointFailoverTest:vm:start").to("log:result1").to("mock:result1");
}
});
consumerContext2 = new DefaultCamelContext(registry);
consumerContext2.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("zookeeper-master:MasterEndpointFailoverTest:vm:start").to("log:result2").to("mock:result2");
}
});
// Need to start at less one consumerContext to enable the vm queue for producerContext
ServiceHelper.startServices(consumerContext1);
ServiceHelper.startServices(producerContext);
result1Endpoint = consumerContext1.getEndpoint("mock:result1", MockEndpoint.class);
result2Endpoint = consumerContext2.getEndpoint("mock:result2", MockEndpoint.class);
}
use of org.apache.curator.framework.CuratorFramework in project camel by apache.
the class GroupTest method testJoinBeforeConnect.
@Test
public void testJoinBeforeConnect() throws Exception {
int port = findFreePort();
CuratorFramework curator = CuratorFrameworkFactory.builder().connectString("localhost:" + port).retryPolicy(new RetryNTimes(10, 100)).build();
curator.start();
Group<NodeState> group = new ZooKeeperGroup<NodeState>(curator, "/singletons/test" + System.currentTimeMillis(), NodeState.class);
group.add(listener);
group.start();
GroupCondition groupCondition = new GroupCondition();
group.add(groupCondition);
assertFalse(group.isConnected());
assertFalse(group.isMaster());
group.update(new NodeState("foo"));
NIOServerCnxnFactory cnxnFactory = startZooKeeper(port);
curator.getZookeeperClient().blockUntilConnectedOrTimedOut();
assertTrue(groupCondition.waitForConnected(5, TimeUnit.SECONDS));
assertTrue(groupCondition.waitForMaster(5, TimeUnit.SECONDS));
group.close();
curator.close();
cnxnFactory.shutdown();
cnxnFactory.join();
}
Aggregations