Java Code Examples for io.vavr.concurrent.Future#of()

The following examples show how to use io.vavr.concurrent.Future#of() . 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: AriEventProcessingTest.java    From ari-proxy with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
void verifyGetCallContextWorksAsExpected() {
	new TestKit(system)
	{
		{
			final Future<Try<String>> callContext = Future.of(() -> AriEventProcessing.getCallContext("RESOURCE_ID", getRef(),
					ProviderPolicy.CREATE_IF_MISSING));

			final ProvideCallContext provideCallContext = expectMsgClass(ProvideCallContext.class);

			assertThat(provideCallContext.policy(), is(ProviderPolicy.CREATE_IF_MISSING));
			assertThat(provideCallContext.resourceId(), is("RESOURCE_ID"));
			reply(new CallContextProvided("CALL_CONTEXT"));

			assertThat(callContext.await().get().get(), is("CALL_CONTEXT"));
		}
	};
}
 
Example 2
Source File: RedisPersistenceStore.java    From ari-proxy with GNU Affero General Public License v3.0 5 votes vote down vote up
private <T> Future<T> executeRedisCommand(Function<RedisCommands<String, String>, T> f) {
	return Future.of(() -> {
		try (StatefulRedisConnection<String, String> connection = redisClient.connect()) {
			return f.apply(connection.sync());
		}
	});
}
 
Example 3
Source File: AriEventProcessingTest.java    From ari-proxy with GNU Affero General Public License v3.0 5 votes vote down vote up
@DisplayName("Verify processing of both channel and playback events results in the expected kafka producer record")
@ParameterizedTest
@ValueSource(strings = { stasisStartEvent, playbackFinishedEvent, recordingFinishedEvent })
void generateProducerRecordFromAllAriMessageTypes(String ariEvent) {
	new TestKit(system)
	{
		{
			final Future<Source<ProducerRecord<String, String>, NotUsed>> wsToKafkaProcessor = Future.of(
					() -> AriEventProcessing
							.generateProducerRecordFromEvent(fakeCommandsTopic, fakeEventsAndResponsesTopic, new Strict(ariEvent), getRef(), system.log(), genApplicationReplacedHandler.apply(getRef()))
			);

			expectMsgClass(ProvideCallContext.class);
			reply(new CallContextProvided("CALL_CONTEXT"));

			final ProducerRecord<String, String> record = wsToKafkaProcessor
					.flatMap(source -> Future.fromCompletableFuture(source.runWith(
							Sink.last(),
							ActorMaterializer.create(ActorMaterializerSettings.create(system), system))
							.toCompletableFuture())
					)
					.await()
					.get();

			assertThat(record.key(), is("CALL_CONTEXT"));
			assertThat(record.topic(), is(fakeEventsAndResponsesTopic));
		}
	};
}
 
Example 4
Source File: AriEventProcessingTest.java    From ari-proxy with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
void checkApplicationReplacedHandlerIsTriggered() {
	new TestKit(system)
	{
		{
			final Future<Source<ProducerRecord<String, String>, NotUsed>> wsToKafkaProcessor = Future.of(
					() -> AriEventProcessing
							.generateProducerRecordFromEvent(fakeCommandsTopic, fakeEventsAndResponsesTopic, new Strict(applicationReplacedEvent), getRef(), system.log(), genApplicationReplacedHandler.apply(getRef()))
			);
			assertThat(expectMsgClass(String.class), is("Shutdown triggered!"));
			assertThat(wsToKafkaProcessor.await().get(), is(Source.empty()));
		}
	};
}
 
Example 5
Source File: FutureUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenDivideByZero_thenGetThrowable1() {
    Future<Integer> resultFuture = Future.of(() -> 10 / 0);

    assertThatThrownBy(resultFuture::get)
      .isInstanceOf(ArithmeticException.class);
}
 
Example 6
Source File: FutureUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenCallZip_thenCorrect() {
    Future<String> f1 = Future.of(() -> "hello1");
    Future<String> f2 = Future.of(() -> "hello2");

    assertThat(f1.zip(f2).get())
      .isEqualTo(Tuple.of("hello1", "hello2"));
}
 
Example 7
Source File: FutureUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenBothFuturesFail_thenGetErrorMessage() {
    Future<String> f1 = Future.of(() -> "Hello".substring(-1));
    Future<String> f2 = Future.of(() -> "Hello".substring(-2));

    Future<String> errorMessageFuture = f1.fallbackTo(f2);
    Future<Throwable> errorMessage = errorMessageFuture.failed();

    assertThat(
      errorMessage.get().getMessage())
      .isEqualTo("String index out of range: -1");
}