Search in sources :

Example 76 with FutureCallback

use of com.linkedin.common.callback.FutureCallback in project rest.li by linkedin.

the class SymlinkAwareZooKeeperTest method testSymlinkWithChildrenWatcher3.

// test children2Callback watcher
@Test
public void testSymlinkWithChildrenWatcher3() throws ExecutionException, InterruptedException, KeeperException {
    final CountDownLatch latch1 = new CountDownLatch(1);
    final CountDownLatch latch2 = new CountDownLatch(1);
    Stat expectedStat1 = _zkClient.getZooKeeper().exists("/foo/bar", false);
    Stat expectedStat2 = _zkClient.getZooKeeper().exists("/bar/foo", false);
    final AsyncCallback.Children2Callback callback2 = new AsyncCallback.Children2Callback() {

        @Override
        public void processResult(int rc, String path, Object ctx, List<String> children, Stat stat) {
            Assert.assertEquals(path, "/foo/$link");
            Assert.assertEquals(children.size(), 5);
            Assert.assertEquals(stat, expectedStat2);
            latch2.countDown();
        }
    };
    Watcher watcher = new Watcher() {

        @Override
        public void process(WatchedEvent event) {
            Assert.assertEquals(event.getType(), Event.EventType.NodeChildrenChanged);
            _zkClient.getZooKeeper().getChildren(event.getPath(), null, callback2, null);
        }
    };
    AsyncCallback.Children2Callback callback = new AsyncCallback.Children2Callback() {

        @Override
        public void processResult(int rc, String path, Object ctx, List<String> children, Stat stat) {
            Assert.assertEquals(stat, expectedStat1);
            latch1.countDown();
        }
    };
    // set watcher to /foo/$link
    _zkClient.getZooKeeper().getChildren("/foo/$link", watcher, callback, null);
    latch1.await(30, TimeUnit.SECONDS);
    // update symlink
    _zkClient.setSymlinkData("/foo/$link", "/bar/foo", new FutureCallback<None>());
    latch2.await(30, TimeUnit.SECONDS);
    FutureCallback<None> fcb = new FutureCallback<None>();
    // restore symlink
    _zkClient.setSymlinkData("/foo/$link", "/foo/bar", fcb);
    fcb.get();
}
Also used : AsyncCallback(org.apache.zookeeper.AsyncCallback) Watcher(org.apache.zookeeper.Watcher) CountDownLatch(java.util.concurrent.CountDownLatch) WatchedEvent(org.apache.zookeeper.WatchedEvent) Stat(org.apache.zookeeper.data.Stat) List(java.util.List) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Example 77 with FutureCallback

use of com.linkedin.common.callback.FutureCallback in project rest.li by linkedin.

the class ZKFSTest method testServiceDirectory.

@Test
public void testServiceDirectory() throws Exception {
    final String TEST_SERVICE_NAME = "testingService";
    startServer();
    try {
        ZKFSLoadBalancer balancer = getBalancer();
        FutureCallback<None> callback = new FutureCallback<None>();
        balancer.start(callback);
        callback.get(30, TimeUnit.SECONDS);
        Directory dir = balancer.getDirectory();
        ZKConnection conn = new ZKConnection("localhost:" + PORT, 30000);
        conn.start();
        ZooKeeperPermanentStore<ServiceProperties> store = new ZooKeeperPermanentStore<ServiceProperties>(conn, new ServicePropertiesJsonSerializer(), ZKFSUtil.servicePath(BASE_PATH));
        callback = new FutureCallback<None>();
        store.start(callback);
        callback.get(30, TimeUnit.SECONDS);
        ServiceProperties props = new ServiceProperties(TEST_SERVICE_NAME, "someCluster", "/somePath", Arrays.asList("someStrategy"));
        store.put(TEST_SERVICE_NAME, props);
        FutureCallback<List<String>> serviceCallback = new FutureCallback<List<String>>();
        dir.getServiceNames(serviceCallback);
        Assert.assertEquals(serviceCallback.get(30, TimeUnit.SECONDS), Collections.singletonList(TEST_SERVICE_NAME));
    } finally {
        stopServer();
    }
}
Also used : ZKConnection(com.linkedin.d2.discovery.stores.zk.ZKConnection) ServicePropertiesJsonSerializer(com.linkedin.d2.balancer.properties.ServicePropertiesJsonSerializer) ZooKeeperPermanentStore(com.linkedin.d2.discovery.stores.zk.ZooKeeperPermanentStore) ServiceProperties(com.linkedin.d2.balancer.properties.ServiceProperties) List(java.util.List) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) Directory(com.linkedin.d2.balancer.Directory) Test(org.testng.annotations.Test)

Example 78 with FutureCallback

use of com.linkedin.common.callback.FutureCallback in project rest.li by linkedin.

the class ZKFSTest method testClusterDirectory.

@Test
public void testClusterDirectory() throws Exception {
    final String TEST_CLUSTER_NAME = "testingService";
    startServer();
    try {
        ZKFSLoadBalancer balancer = getBalancer();
        FutureCallback<None> callback = new FutureCallback<None>();
        balancer.start(callback);
        callback.get(30, TimeUnit.SECONDS);
        Directory dir = balancer.getDirectory();
        ZKConnection conn = new ZKConnection("localhost:" + PORT, 30000);
        conn.start();
        ZooKeeperPermanentStore<ClusterProperties> store = new ZooKeeperPermanentStore<ClusterProperties>(conn, new ClusterPropertiesJsonSerializer(), ZKFSUtil.clusterPath(BASE_PATH));
        callback = new FutureCallback<None>();
        store.start(callback);
        callback.get(30, TimeUnit.SECONDS);
        ClusterProperties props = new ClusterProperties(TEST_CLUSTER_NAME);
        store.put(TEST_CLUSTER_NAME, props);
        FutureCallback<List<String>> clusterCallback = new FutureCallback<List<String>>();
        dir.getClusterNames(clusterCallback);
        Assert.assertEquals(clusterCallback.get(30, TimeUnit.SECONDS), Collections.singletonList(TEST_CLUSTER_NAME));
    } finally {
        stopServer();
    }
}
Also used : ZKConnection(com.linkedin.d2.discovery.stores.zk.ZKConnection) ZooKeeperPermanentStore(com.linkedin.d2.discovery.stores.zk.ZooKeeperPermanentStore) ClusterPropertiesJsonSerializer(com.linkedin.d2.balancer.properties.ClusterPropertiesJsonSerializer) ClusterProperties(com.linkedin.d2.balancer.properties.ClusterProperties) List(java.util.List) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) Directory(com.linkedin.d2.balancer.Directory) Test(org.testng.annotations.Test)

Example 79 with FutureCallback

use of com.linkedin.common.callback.FutureCallback in project rest.li by linkedin.

the class TestMIMEWriter method testPrependDataSources.

@Test(dataProvider = "prependDataSources")
public void testPrependDataSources(final MultiPartMIMEWriter.Builder builder, final List<MultiPartMIMEDataSourceWriter> prependDataSources, final List<MIMEDataPart> expectedParts, final String expectedPreamble, final int expectedSize) throws Exception {
    for (final MultiPartMIMEDataSourceWriter dataSourceWriter : prependDataSources) {
        builder.prependDataSource(dataSourceWriter);
    }
    Assert.assertEquals(builder.getCurrentSize(), expectedSize);
    final MultiPartMIMEWriter writer = builder.build();
    final FutureCallback<ByteString> futureCallback = new FutureCallback<ByteString>();
    final FullEntityReader fullEntityReader = new FullEntityReader(futureCallback);
    writer.getEntityStream().setReader(fullEntityReader);
    futureCallback.get(_testTimeout, TimeUnit.MILLISECONDS);
    final StreamRequest multiPartMIMEStreamRequest = MultiPartMIMEStreamRequestFactory.generateMultiPartMIMEStreamRequest(URI.create("localhost"), "mixed", writer, Collections.<String, String>emptyMap());
    final JavaxMailMultiPartMIMEReader javaxMailMultiPartMIMEReader = new JavaxMailMultiPartMIMEReader(multiPartMIMEStreamRequest.getHeader(MultiPartMIMEUtils.CONTENT_TYPE_HEADER), futureCallback.get());
    javaxMailMultiPartMIMEReader.parseRequestIntoParts();
    List<MIMEDataPart> dataSourceList = javaxMailMultiPartMIMEReader._dataSourceList;
    Assert.assertEquals(dataSourceList.size(), 3);
    for (int i = 0; i < dataSourceList.size(); i++) {
        Assert.assertEquals(dataSourceList.get(i), expectedParts.get(i));
    }
    //Javax mail incorrectly adds the CRLF for the first boundary to the end of the preamble, so we trim
    Assert.assertEquals(javaxMailMultiPartMIMEReader._preamble != null ? javaxMailMultiPartMIMEReader._preamble.trim() : null, expectedPreamble);
}
Also used : FullEntityReader(com.linkedin.r2.message.stream.entitystream.FullEntityReader) ByteString(com.linkedin.data.ByteString) MIMEDataPart(com.linkedin.multipart.utils.MIMEDataPart) FutureCallback(com.linkedin.common.callback.FutureCallback) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 80 with FutureCallback

use of com.linkedin.common.callback.FutureCallback in project rest.li by linkedin.

the class TestMIMEWriter method testSingleDataSource.

@Test(dataProvider = "singleDataSources")
public void testSingleDataSource(final ByteString body, final Map<String, String> headers) throws Exception {
    final MIMEDataPart expectedMultiPartMIMEDataPart = new MIMEDataPart(body, headers);
    final MultiPartMIMEInputStream singleDataSource = new MultiPartMIMEInputStream.Builder(new ByteArrayInputStream(body.copyBytes()), _scheduledExecutorService, headers).build();
    final MultiPartMIMEWriter multiPartMIMEWriter = new MultiPartMIMEWriter.Builder("preamble", "epilogue").appendDataSource(singleDataSource).build();
    final FutureCallback<ByteString> futureCallback = new FutureCallback<ByteString>();
    final FullEntityReader fullEntityReader = new FullEntityReader(futureCallback);
    multiPartMIMEWriter.getEntityStream().setReader(fullEntityReader);
    futureCallback.get(_testTimeout, TimeUnit.MILLISECONDS);
    final StreamRequest multiPartMIMEStreamRequest = MultiPartMIMEStreamRequestFactory.generateMultiPartMIMEStreamRequest(URI.create("localhost"), "mixed", multiPartMIMEWriter, Collections.<String, String>emptyMap());
    JavaxMailMultiPartMIMEReader javaxMailMultiPartMIMEReader = new JavaxMailMultiPartMIMEReader(multiPartMIMEStreamRequest.getHeader(MultiPartMIMEUtils.CONTENT_TYPE_HEADER), futureCallback.get());
    javaxMailMultiPartMIMEReader.parseRequestIntoParts();
    List<MIMEDataPart> dataSourceList = javaxMailMultiPartMIMEReader._dataSourceList;
    Assert.assertEquals(dataSourceList.size(), 1);
    Assert.assertEquals(dataSourceList.get(0), expectedMultiPartMIMEDataPart);
    //Javax mail incorrectly adds the CRLF for the first boundary to the end of the preamble, so we trim
    Assert.assertEquals(javaxMailMultiPartMIMEReader._preamble.trim(), "preamble");
}
Also used : ByteString(com.linkedin.data.ByteString) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) FullEntityReader(com.linkedin.r2.message.stream.entitystream.FullEntityReader) ByteArrayInputStream(java.io.ByteArrayInputStream) MIMEDataPart(com.linkedin.multipart.utils.MIMEDataPart) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Aggregations

FutureCallback (com.linkedin.common.callback.FutureCallback)152 Test (org.testng.annotations.Test)116 None (com.linkedin.common.util.None)77 RestRequest (com.linkedin.r2.message.rest.RestRequest)50 ExecutionException (java.util.concurrent.ExecutionException)50 RequestContext (com.linkedin.r2.message.RequestContext)47 RestResponse (com.linkedin.r2.message.rest.RestResponse)43 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)36 HashMap (java.util.HashMap)26 CountDownLatch (java.util.concurrent.CountDownLatch)25 TransportCallbackAdapter (com.linkedin.r2.transport.common.bridge.client.TransportCallbackAdapter)22 ByteString (com.linkedin.data.ByteString)21 RemoteInvocationException (com.linkedin.r2.RemoteInvocationException)21 URI (java.net.URI)21 TimeoutException (java.util.concurrent.TimeoutException)21 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)20 UriProperties (com.linkedin.d2.balancer.properties.UriProperties)16 ArrayList (java.util.ArrayList)16 RestException (com.linkedin.r2.message.rest.RestException)15 AsyncSharedPoolImpl (com.linkedin.r2.transport.http.client.AsyncSharedPoolImpl)14