Search in sources :

Example 36 with SettableFuture

use of com.google.common.util.concurrent.SettableFuture in project jackrabbit-oak by apache.

the class UploadStagingCacheTest method testCacheFullAdd.

/**
 * Stage file when cache full.
 * @throws Exception
 */
@Test
public void testCacheFullAdd() throws Exception {
    // initialize cache to have restricted size
    stagingCache = UploadStagingCache.build(root, null, 1, /*threads*/
    4 * 1024, /* bytes */
    uploader, null, /*cache*/
    statsProvider, executor, null, 3000, 6000);
    closer.register(stagingCache);
    // add load
    List<ListenableFuture<Integer>> futures = put(folder);
    // Add another load
    File f2 = copyToFile(randomStream(1, 4 * 1024), folder.newFile());
    Optional<SettableFuture<Integer>> future2 = stagingCache.put(ID_PREFIX + 1, f2);
    assertFalse(future2.isPresent());
    // start
    taskLatch.countDown();
    callbackLatch.countDown();
    assertFuture(futures, 0);
    // Try 2nd upload again
    Optional<SettableFuture<Integer>> future = stagingCache.put(ID_PREFIX + 1, f2);
    futures = Lists.newArrayList();
    if (future.isPresent()) {
        futures.add(future.get());
    }
    assertFuture(futures, 1);
    assertCacheStats(stagingCache, 0, 0, 2, 3);
}
Also used : SettableFuture(com.google.common.util.concurrent.SettableFuture) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) File(java.io.File) Test(org.junit.Test)

Example 37 with SettableFuture

use of com.google.common.util.concurrent.SettableFuture in project jackrabbit-oak by apache.

the class UploadStagingCacheTest method testConcurrentSameAdd.

/**
 * Stage same file concurrently.
 * @throws Exception
 */
@Test
public void testConcurrentSameAdd() throws Exception {
    // Add load
    List<ListenableFuture<Integer>> futures = put(folder);
    File f = copyToFile(randomStream(0, 4 * 1024), folder.newFile());
    Optional<SettableFuture<Integer>> future2 = stagingCache.put(ID_PREFIX + 0, f);
    assertTrue(future2.isPresent());
    assertEquals(future2.get().get().intValue(), 0);
    // start
    taskLatch.countDown();
    callbackLatch.countDown();
    assertFuture(futures, 0);
    assertCacheStats(stagingCache, 0, 0, 1, 2);
}
Also used : SettableFuture(com.google.common.util.concurrent.SettableFuture) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) File(java.io.File) Test(org.junit.Test)

Example 38 with SettableFuture

use of com.google.common.util.concurrent.SettableFuture in project netvirt by opendaylight.

the class L2GatewayConnectionUtils method readAndCopyLocalUcastMacsToCache.

private void readAndCopyLocalUcastMacsToCache(final String elanName, final L2GatewayDevice l2GatewayDevice) {
    final InstanceIdentifier<Node> nodeIid = HwvtepSouthboundUtils.createInstanceIdentifier(new NodeId(l2GatewayDevice.getHwvtepNodeId()));
    jobCoordinator.enqueueJob(elanName + ":" + l2GatewayDevice.getDeviceName(), () -> {
        final SettableFuture settableFuture = SettableFuture.create();
        Futures.addCallback(broker.newReadOnlyTransaction().read(LogicalDatastoreType.OPERATIONAL, nodeIid), new SettableFutureCallback<Optional<Node>>(settableFuture) {

            @Override
            public void onSuccess(@Nonnull Optional<Node> resultNode) {
                LocalUcastMacListener localUcastMacListener = new LocalUcastMacListener(broker, haOpClusteredListener, elanL2GatewayUtils, jobCoordinator, elanInstanceCache);
                settableFuture.set(resultNode);
                Optional<Node> nodeOptional = resultNode;
                if (nodeOptional.isPresent()) {
                    Node node = nodeOptional.get();
                    if (node.getAugmentation(HwvtepGlobalAugmentation.class) != null) {
                        List<LocalUcastMacs> localUcastMacs = node.getAugmentation(HwvtepGlobalAugmentation.class).getLocalUcastMacs();
                        if (localUcastMacs == null) {
                            return;
                        }
                        localUcastMacs.stream().filter((mac) -> {
                            return macBelongsToLogicalSwitch(mac, elanName);
                        }).forEach((mac) -> {
                            InstanceIdentifier<LocalUcastMacs> macIid = getMacIid(nodeIid, mac);
                            localUcastMacListener.added(macIid, mac);
                        });
                    }
                }
            }
        }, MoreExecutors.directExecutor());
        return Lists.newArrayList(settableFuture);
    }, 5);
}
Also used : SettableFuture(com.google.common.util.concurrent.SettableFuture) MoreExecutors(com.google.common.util.concurrent.MoreExecutors) L2gatewayConnection(org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.connections.attributes.l2gatewayconnections.L2gatewayConnection) ElanL2GwCacheUtils(org.opendaylight.netvirt.elanmanager.utils.ElanL2GwCacheUtils) L2gateways(org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateways.attributes.L2gateways) L2gateway(org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateways.attributes.l2gateways.L2gateway) LoggerFactory(org.slf4j.LoggerFactory) AssociateHwvtepToElanJob(org.opendaylight.netvirt.elan.l2gw.jobs.AssociateHwvtepToElanJob) SettableFuture(com.google.common.util.concurrent.SettableFuture) Singleton(javax.inject.Singleton) HwvtepLogicalSwitchListener(org.opendaylight.netvirt.elan.l2gw.listeners.HwvtepLogicalSwitchListener) Uuid(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid) Neutron(org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron) HwvtepUtils(org.opendaylight.genius.utils.hwvtep.HwvtepUtils) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) PreDestroy(javax.annotation.PreDestroy) NodeId(org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId) HAOpClusteredListener(org.opendaylight.netvirt.elan.l2gw.ha.listeners.HAOpClusteredListener) ElanClusterUtils(org.opendaylight.netvirt.elan.utils.ElanClusterUtils) Lists(com.google.common.collect.Lists) Optional(com.google.common.base.Optional) LocalUcastMacs(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalUcastMacs) MDSALUtil(org.opendaylight.genius.mdsalutil.MDSALUtil) LocalUcastMacListener(org.opendaylight.netvirt.elan.l2gw.listeners.LocalUcastMacListener) Nonnull(javax.annotation.Nonnull) HwvtepSouthboundUtils(org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundUtils) Devices(org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.attributes.Devices) LogicalSwitches(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches) Logger(org.slf4j.Logger) Node(org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node) ElanInstanceCache(org.opendaylight.netvirt.elan.cache.ElanInstanceCache) LogicalDatastoreType(org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType) JobCoordinator(org.opendaylight.infrautils.jobcoordinator.JobCoordinator) Set(java.util.Set) DataBroker(org.opendaylight.controller.md.sal.binding.api.DataBroker) ElanInstance(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance) HwvtepGlobalAugmentation(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation) Futures(com.google.common.util.concurrent.Futures) List(java.util.List) InstanceIdentifier(org.opendaylight.yangtools.yang.binding.InstanceIdentifier) L2gatewayConnections(org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.connections.attributes.L2gatewayConnections) L2gatewayKey(org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateways.attributes.l2gateways.L2gatewayKey) DisAssociateHwvtepFromElanJob(org.opendaylight.netvirt.elan.l2gw.jobs.DisAssociateHwvtepFromElanJob) ElanUtils.isVxlanNetworkOrVxlanSegment(org.opendaylight.netvirt.elan.utils.ElanUtils.isVxlanNetworkOrVxlanSegment) Collections(java.util.Collections) L2GatewayCache(org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayCache) L2GatewayDevice(org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Optional(com.google.common.base.Optional) Node(org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node) NodeId(org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId) InstanceIdentifier(org.opendaylight.yangtools.yang.binding.InstanceIdentifier) ArrayList(java.util.ArrayList) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) LocalUcastMacListener(org.opendaylight.netvirt.elan.l2gw.listeners.LocalUcastMacListener)

Example 39 with SettableFuture

use of com.google.common.util.concurrent.SettableFuture in project pdi-dataservice-server-plugin by pentaho.

the class DataServiceClientTest method testInputStreamClosed.

@Test
public void testInputStreamClosed() throws Exception {
    CountDownLatch startQuery = new CountDownLatch(1);
    SettableFuture<Boolean> queryFinished = SettableFuture.create();
    // Create a query that blocks on the countdown latch
    when(queryServiceDelegate.prepareQuery(TEST_SQL_QUERY, MAX_ROWS, ImmutableMap.of())).thenReturn((MockQuery) outputStream -> {
        Random random = new Random();
        try {
            outputStream.write(random.nextInt());
            startQuery.await();
            outputStream.write(random.nextInt());
            queryFinished.set(true);
        } catch (Exception e) {
            queryFinished.setException(e);
        }
    });
    // Start query and close immediately
    client.query(TEST_SQL_QUERY, MAX_ROWS).close();
    // Unleash query stream
    startQuery.countDown();
    // Assert no errors thrown or logged
    assertThat(Futures.get(queryFinished, IOException.class), is(true));
    verify(log, never()).logError(anyString(), any(Throwable.class));
}
Also used : DataInputStream(java.io.DataInputStream) Trans(org.pentaho.di.trans.Trans) Mock(org.mockito.Mock) KettleException(org.pentaho.di.core.exception.KettleException) RunWith(org.junit.runner.RunWith) Random(java.util.Random) SettableFuture(com.google.common.util.concurrent.SettableFuture) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) Matchers.anyString(org.mockito.Matchers.anyString) Matchers.hasProperty(org.hamcrest.Matchers.hasProperty) Assert.assertSame(org.junit.Assert.assertSame) Assert.assertThat(org.junit.Assert.assertThat) SQLException(java.sql.SQLException) ThinServiceInformation(org.pentaho.di.trans.dataservice.jdbc.ThinServiceInformation) ImmutableList(com.google.common.collect.ImmutableList) DataOutputStream(java.io.DataOutputStream) Matchers.anyObject(org.mockito.Matchers.anyObject) Assert.fail(org.junit.Assert.fail) AllOf.allOf(org.hamcrest.core.AllOf.allOf) Before(org.junit.Before) Matchers.empty(org.hamcrest.Matchers.empty) ImmutableMap(com.google.common.collect.ImmutableMap) DataServiceResolver(org.pentaho.di.trans.dataservice.resolvers.DataServiceResolver) Test(org.junit.Test) IOException(java.io.IOException) LogChannelInterface(org.pentaho.di.core.logging.LogChannelInterface) Executors(java.util.concurrent.Executors) Matchers.any(org.mockito.Matchers.any) BaseTest(org.pentaho.di.trans.dataservice.BaseTest) CountDownLatch(java.util.concurrent.CountDownLatch) Mockito(org.mockito.Mockito) Futures(com.google.common.util.concurrent.Futures) List(java.util.List) MockitoJUnitRunner(org.mockito.runners.MockitoJUnitRunner) Matchers.contains(org.hamcrest.Matchers.contains) Matchers.equalTo(org.hamcrest.Matchers.equalTo) KettleStepException(org.pentaho.di.core.exception.KettleStepException) Matchers.is(org.hamcrest.Matchers.is) IDataServiceClientService(org.pentaho.di.trans.dataservice.client.api.IDataServiceClientService) Random(java.util.Random) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) KettleException(org.pentaho.di.core.exception.KettleException) SQLException(java.sql.SQLException) IOException(java.io.IOException) KettleStepException(org.pentaho.di.core.exception.KettleStepException) Test(org.junit.Test) BaseTest(org.pentaho.di.trans.dataservice.BaseTest)

Example 40 with SettableFuture

use of com.google.common.util.concurrent.SettableFuture in project pdi-dataservice-server-plugin by pentaho.

the class DataServiceClientTest method testWindowInputStreamClosed.

@Test
public void testWindowInputStreamClosed() throws Exception {
    CountDownLatch startQuery = new CountDownLatch(1);
    SettableFuture<Boolean> queryFinished = SettableFuture.create();
    // Create a query that blocks on the countdown latch
    when(queryServiceDelegate.prepareQuery(TEST_SQL_QUERY, WINDOW_MODE, WINDOW_SIZE, WINDOW_EVERY, WINDOW_MAX_SIZE, ImmutableMap.of())).thenReturn((MockQuery) outputStream -> {
        Random random = new Random();
        try {
            outputStream.write(random.nextInt());
            startQuery.await();
            outputStream.write(random.nextInt());
            queryFinished.set(true);
        } catch (Exception e) {
            queryFinished.setException(e);
        }
    });
    // Start query and close immediately
    client.query(TEST_SQL_QUERY, WINDOW_MODE, WINDOW_SIZE, WINDOW_EVERY, WINDOW_MAX_SIZE).close();
    // Unleash query stream
    startQuery.countDown();
    // Assert no errors thrown or logged
    assertThat(Futures.get(queryFinished, IOException.class), is(true));
    verify(log, never()).logError(anyString(), any(Throwable.class));
}
Also used : DataInputStream(java.io.DataInputStream) Trans(org.pentaho.di.trans.Trans) Mock(org.mockito.Mock) KettleException(org.pentaho.di.core.exception.KettleException) RunWith(org.junit.runner.RunWith) Random(java.util.Random) SettableFuture(com.google.common.util.concurrent.SettableFuture) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) Matchers.anyString(org.mockito.Matchers.anyString) Matchers.hasProperty(org.hamcrest.Matchers.hasProperty) Assert.assertSame(org.junit.Assert.assertSame) Assert.assertThat(org.junit.Assert.assertThat) SQLException(java.sql.SQLException) ThinServiceInformation(org.pentaho.di.trans.dataservice.jdbc.ThinServiceInformation) ImmutableList(com.google.common.collect.ImmutableList) DataOutputStream(java.io.DataOutputStream) Matchers.anyObject(org.mockito.Matchers.anyObject) Assert.fail(org.junit.Assert.fail) AllOf.allOf(org.hamcrest.core.AllOf.allOf) Before(org.junit.Before) Matchers.empty(org.hamcrest.Matchers.empty) ImmutableMap(com.google.common.collect.ImmutableMap) DataServiceResolver(org.pentaho.di.trans.dataservice.resolvers.DataServiceResolver) Test(org.junit.Test) IOException(java.io.IOException) LogChannelInterface(org.pentaho.di.core.logging.LogChannelInterface) Executors(java.util.concurrent.Executors) Matchers.any(org.mockito.Matchers.any) BaseTest(org.pentaho.di.trans.dataservice.BaseTest) CountDownLatch(java.util.concurrent.CountDownLatch) Mockito(org.mockito.Mockito) Futures(com.google.common.util.concurrent.Futures) List(java.util.List) MockitoJUnitRunner(org.mockito.runners.MockitoJUnitRunner) Matchers.contains(org.hamcrest.Matchers.contains) Matchers.equalTo(org.hamcrest.Matchers.equalTo) KettleStepException(org.pentaho.di.core.exception.KettleStepException) Matchers.is(org.hamcrest.Matchers.is) IDataServiceClientService(org.pentaho.di.trans.dataservice.client.api.IDataServiceClientService) Random(java.util.Random) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) KettleException(org.pentaho.di.core.exception.KettleException) SQLException(java.sql.SQLException) IOException(java.io.IOException) KettleStepException(org.pentaho.di.core.exception.KettleStepException) Test(org.junit.Test) BaseTest(org.pentaho.di.trans.dataservice.BaseTest)

Aggregations

SettableFuture (com.google.common.util.concurrent.SettableFuture)46 Test (org.junit.Test)25 ArrayList (java.util.ArrayList)15 List (java.util.List)15 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)14 File (java.io.File)12 IOException (java.io.IOException)10 TimeUnit (java.util.concurrent.TimeUnit)10 Futures (com.google.common.util.concurrent.Futures)9 ImmutableMap (com.google.common.collect.ImmutableMap)8 ExecutionException (java.util.concurrent.ExecutionException)8 FutureCallback (com.google.common.util.concurrent.FutureCallback)7 Before (org.junit.Before)7 CountDownLatch (java.util.concurrent.CountDownLatch)6 Throwables (com.google.common.base.Throwables)5 ImmutableList (com.google.common.collect.ImmutableList)5 Assert.assertSame (org.junit.Assert.assertSame)5 Mock (org.mockito.Mock)5 Logger (org.slf4j.Logger)5 LoggerFactory (org.slf4j.LoggerFactory)5