Skip to content

Commit

Permalink
Add ThriftCompletableFuture and ThriftListenableFuture (#347)
Browse files Browse the repository at this point in the history
  • Loading branch information
imasahiro authored and trustin committed Dec 7, 2016
1 parent 820b5b8 commit fa737db
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 0 deletions.
@@ -0,0 +1,37 @@
/*
* Copyright 2016 LINE Corporation
*
* LINE Corporation licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

package com.linecorp.armeria.common.thrift;

import java.util.concurrent.CompletableFuture;

import org.apache.thrift.async.AsyncMethodCallback;

/**
* A {@link CompletableFuture} that can be passed in as an {@link AsyncMethodCallback}
* when making an asynchronous client-side Thrift RPC.
*/
public class ThriftCompletableFuture<T> extends CompletableFuture<T> implements AsyncMethodCallback<T> {
@Override
public void onComplete(T t) {
complete(t);
}

@Override
public void onError(Exception e) {
completeExceptionally(e);
}
}
@@ -0,0 +1,59 @@
/*
* Copyright 2016 LINE Corporation
*
* LINE Corporation licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package com.linecorp.armeria.common.thrift;

/**
* Static factory methods pertaining to the {@link ThriftCompletableFuture} and {@link ThriftListenableFuture}.
*/
public final class ThriftFutures {
/**
* Returns a new {@link ThriftCompletableFuture} instance that has its value set immediately.
*/
public static <T> ThriftCompletableFuture<T> successfulCompletedFuture(T value) {
ThriftCompletableFuture<T> future = new ThriftCompletableFuture<T>();
future.onComplete(value);
return future;
}

/**
* Returns a new {@link ThriftCompletableFuture} instance that has an exception set immediately.
*/
public static <T> ThriftCompletableFuture<T> failedCompletedFuture(Exception e) {
ThriftCompletableFuture<T> future = new ThriftCompletableFuture<>();
future.onError(e);
return future;
}

/**
* Returns a new {@link ThriftListenableFuture} instance that has its value set immediately.
*/
public static <T> ThriftListenableFuture<T> successfulListenableFuture(T value) {
ThriftListenableFuture<T> future = new ThriftListenableFuture<>();
future.onComplete(value);
return future;
}

/**
* Returns a new {@link ThriftListenableFuture} instance that has an exception set immediately.
*/
public static <T> ThriftListenableFuture<T> failedListenableFuture(Exception e) {
ThriftListenableFuture<T> future = new ThriftListenableFuture<>();
future.onError(e);
return future;
}

private ThriftFutures() {}
}
@@ -0,0 +1,38 @@
/*
* Copyright 2016 LINE Corporation
*
* LINE Corporation licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

package com.linecorp.armeria.common.thrift;

import org.apache.thrift.async.AsyncMethodCallback;

import com.google.common.util.concurrent.AbstractFuture;
import com.google.common.util.concurrent.ListenableFuture;

/**
* A {@link ListenableFuture} that can be passed in as an {@link AsyncMethodCallback}
* when making an asynchronous client-side Thrift RPC.
*/
public class ThriftListenableFuture<T> extends AbstractFuture<T> implements AsyncMethodCallback<T> {
@Override
public void onComplete(T t) {
set(t);
}

@Override
public void onError(Exception e) {
setException(e);
}
}
@@ -0,0 +1,53 @@
/*
* Copyright 2016 LINE Corporation
*
* LINE Corporation licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

package com.linecorp.armeria.common.thrift;

import static com.linecorp.armeria.common.thrift.ThriftFutures.failedCompletedFuture;
import static com.linecorp.armeria.common.thrift.ThriftFutures.failedListenableFuture;
import static com.linecorp.armeria.common.thrift.ThriftFutures.successfulCompletedFuture;
import static com.linecorp.armeria.common.thrift.ThriftFutures.successfulListenableFuture;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.catchThrowable;

import org.junit.Test;

public class ThriftFuturesTest {

@Test
public void testSuccessfulCompletedFuture() throws Exception {
ThriftCompletableFuture<String> future = successfulCompletedFuture("success");
assertThat(future.get()).isEqualTo("success");
}

@Test
public void testFailedCompletedFuture() throws Exception {
ThriftCompletableFuture<String> future = failedCompletedFuture(new IllegalStateException());
assertThat(catchThrowable(future::get)).hasCauseInstanceOf(IllegalStateException.class);
}

@Test
public void testSuccessfulListenableFuture() throws Exception {
ThriftListenableFuture<String> future = successfulListenableFuture("success");
assertThat(future.get()).isEqualTo("success");
}

@Test
public void testFailedListenableFuture() throws Exception {
ThriftListenableFuture<String> future = failedListenableFuture(new IllegalStateException());
assertThat(catchThrowable(future::get)).hasCauseInstanceOf(IllegalStateException.class);
}
}

0 comments on commit fa737db

Please sign in to comment.