com.google.common.util.concurrent.SettableFuture

Here are the examples of the java api class com.google.common.util.concurrent.SettableFuture taken from open source projects.

1. AbstractConfiguredObjectTest#testCreateAwaitsAttainState()

View license
public void testCreateAwaitsAttainState() {
    SettableFuture stateChangeFuture = SettableFuture.create();
    TestCar car = _model.getObjectFactory().create(TestCar.class, Collections.<String, Object>singletonMap(ConfiguredObject.NAME, "myCar"));
    Map<String, Object> engineAttributes = new HashMap<>();
    engineAttributes.put(ConfiguredObject.NAME, "myEngine");
    engineAttributes.put(TestEngine.STATE_CHANGE_FUTURE, stateChangeFuture);
    ListenableFuture engine = car.createChildAsync(TestEngine.class, engineAttributes);
    assertFalse("create child has completed before state change completes", engine.isDone());
    stateChangeFuture.set(null);
    assertTrue("create child has not completed", engine.isDone());
}

2. DefaultOvsdbClient#processResult()

Project: onos
Source File: DefaultOvsdbClient.java
View license
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public void processResult(JsonNode response) {
    log.debug("Handle result");
    String requestId = response.get("id").asText();
    SettableFuture sf = requestResult.get(requestId);
    if (sf == null) {
        log.debug("No such future to process");
        return;
    }
    String methodName = requestMethod.get(requestId);
    sf.set(FromJsonUtil.jsonResultParser(response, methodName));
}

3. AbstractConfiguredObjectTest#testCreateAwaitsAttainState_StateChangeAsyncErrors()

View license
public void testCreateAwaitsAttainState_StateChangeAsyncErrors() throws Exception {
    SettableFuture stateChangeFuture = SettableFuture.create();
    RuntimeException stateChangeException = new RuntimeException("state change error");
    TestCar car = _model.getObjectFactory().create(TestCar.class, Collections.<String, Object>singletonMap(ConfiguredObject.NAME, "myCar"));
    Map<String, Object> engineAttributes = new HashMap<>();
    engineAttributes.put(ConfiguredObject.NAME, "myEngine");
    engineAttributes.put(TestEngine.STATE_CHANGE_FUTURE, stateChangeFuture);
    ListenableFuture engine = car.createChildAsync(TestEngine.class, engineAttributes);
    assertFalse("create child has completed before state change completes", engine.isDone());
    stateChangeFuture.setException(stateChangeException);
    assertTrue("create child has not completed", engine.isDone());
    try {
        engine.get();
        fail("Exception not thrown");
    } catch (ExecutionException ee) {
        assertSame(stateChangeException, ee.getCause());
    }
}

4. DirectDruidClientTest#testRun()

Project: druid
Source File: DirectDruidClientTest.java
View license
@Test
public void testRun() throws Exception {
    HttpClient httpClient = EasyMock.createMock(HttpClient.class);
    final URL url = new URL("http://foo/druid/v2/");
    SettableFuture<InputStream> futureResult = SettableFuture.create();
    Capture<Request> capturedRequest = EasyMock.newCapture();
    EasyMock.expect(httpClient.go(EasyMock.capture(capturedRequest), EasyMock.<HttpResponseHandler>anyObject())).andReturn(futureResult).times(1);
    SettableFuture futureException = SettableFuture.create();
    EasyMock.expect(httpClient.go(EasyMock.capture(capturedRequest), EasyMock.<HttpResponseHandler>anyObject())).andReturn(futureException).times(1);
    EasyMock.expect(httpClient.go(EasyMock.capture(capturedRequest), EasyMock.<HttpResponseHandler>anyObject())).andReturn(SettableFuture.create()).atLeastOnce();
    EasyMock.replay(httpClient);
    final ServerSelector serverSelector = new ServerSelector(new DataSegment("test", new Interval("2013-01-01/2013-01-02"), new DateTime("2013-01-01").toString(), Maps.<String, Object>newHashMap(), Lists.<String>newArrayList(), Lists.<String>newArrayList(), NoneShardSpec.instance(), 0, 0L), new HighestPriorityTierSelectorStrategy(new ConnectionCountServerSelectorStrategy()));
    DirectDruidClient client1 = new DirectDruidClient(new ReflectionQueryToolChestWarehouse(), QueryRunnerTestHelper.NOOP_QUERYWATCHER, new DefaultObjectMapper(), httpClient, "foo", new NoopServiceEmitter());
    DirectDruidClient client2 = new DirectDruidClient(new ReflectionQueryToolChestWarehouse(), QueryRunnerTestHelper.NOOP_QUERYWATCHER, new DefaultObjectMapper(), httpClient, "foo2", new NoopServiceEmitter());
    QueryableDruidServer queryableDruidServer1 = new QueryableDruidServer(new DruidServer("test1", "localhost", 0, "historical", DruidServer.DEFAULT_TIER, 0), client1);
    serverSelector.addServerAndUpdateSegment(queryableDruidServer1, serverSelector.getSegment());
    QueryableDruidServer queryableDruidServer2 = new QueryableDruidServer(new DruidServer("test1", "localhost", 0, "historical", DruidServer.DEFAULT_TIER, 0), client2);
    serverSelector.addServerAndUpdateSegment(queryableDruidServer2, serverSelector.getSegment());
    TimeBoundaryQuery query = Druids.newTimeBoundaryQueryBuilder().dataSource("test").build();
    HashMap<String, List> context = Maps.newHashMap();
    Sequence s1 = client1.run(query, context);
    Assert.assertTrue(capturedRequest.hasCaptured());
    Assert.assertEquals(url, capturedRequest.getValue().getUrl());
    Assert.assertEquals(HttpMethod.POST, capturedRequest.getValue().getMethod());
    Assert.assertEquals(1, client1.getNumOpenConnections());
    // simulate read timeout
    Sequence s2 = client1.run(query, context);
    Assert.assertEquals(2, client1.getNumOpenConnections());
    futureException.setException(new ReadTimeoutException());
    Assert.assertEquals(1, client1.getNumOpenConnections());
    // subsequent connections should work
    Sequence s3 = client1.run(query, context);
    Sequence s4 = client1.run(query, context);
    Sequence s5 = client1.run(query, context);
    Assert.assertTrue(client1.getNumOpenConnections() == 4);
    // produce result for first connection
    futureResult.set(new ByteArrayInputStream("[{\"timestamp\":\"2014-01-01T01:02:03Z\", \"result\": 42.0}]".getBytes()));
    List<Result> results = Sequences.toList(s1, Lists.<Result>newArrayList());
    Assert.assertEquals(1, results.size());
    Assert.assertEquals(new DateTime("2014-01-01T01:02:03Z"), results.get(0).getTimestamp());
    Assert.assertEquals(3, client1.getNumOpenConnections());
    client2.run(query, context);
    client2.run(query, context);
    Assert.assertTrue(client2.getNumOpenConnections() == 2);
    Assert.assertTrue(serverSelector.pick() == queryableDruidServer2);
    EasyMock.verify(httpClient);
}

5. TradeClient#trade()

Project: mercury
Source File: TradeClient.java
View license
public SettableFuture trade(AtomicSwapTrade trade) {
    SettableFuture future = queueTrade(trade);
    requestSignal.release();
    return future;
}