下面列出了io.grpc.Status#UNKNOWN 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
synchronized void onComplete(@javax.annotation.Nullable Throwable error) {
if (isCompleted()) {
return;
}
currentConfigObserver = null;
// TODO(songya): add Runnable
Status status;
if (error == null) {
status = Status.OK;
} else if (error instanceof StatusRuntimeException) {
status = ((StatusRuntimeException) error).getStatus();
} else {
status = Status.UNKNOWN;
}
terminateStatus = status;
}
@Test
public void closedTest() {
Status status = Status.UNKNOWN;
Metadata trailers = new Metadata();
forward.closed(status, trailers);
verify(mock).closed(same(status), same(trailers));
}
@Test
public void pickerEmptyList() throws Exception {
SubchannelPicker picker = new EmptyPicker(Status.UNKNOWN);
assertEquals(null, picker.pickSubchannel(mockArgs).getSubchannel());
assertEquals(Status.UNKNOWN,
picker.pickSubchannel(mockArgs).getStatus());
}
/** Ensure that all versions return Optional.empty() when the trailing metadata is not present. */
@Test
public void createGoogleAdsException_fromNullMetadata() {
for (Version version : catalog.getSupportedVersions()) {
ApiException exception =
new ApiException(
new StatusException(Status.UNKNOWN, null), GrpcStatusCode.of(Code.UNKNOWN), false);
Optional<? extends BaseGoogleAdsException> result =
version.getExceptionFactory().createGoogleAdsException(exception);
assertFalse("Null metadata produced result", result.isPresent());
}
}
/** Constructs an ApiException embedding the proto in the trailing metadata for a given key. */
private static ApiException getApiExceptionForVersion(
Metadata.Key<byte[]> failureKey, byte[] data) {
Metadata trailers = new Metadata();
if (data != null) {
trailers.put(failureKey, data);
}
return new ApiException(
new StatusException(Status.UNKNOWN, trailers), GrpcStatusCode.of(Code.UNKNOWN), false);
}
@Test
public void handlesNullTrailers() {
ApiException exception =
new ApiException(
new StatusException(Status.UNKNOWN), GrpcStatusCode.of(Code.UNKNOWN), false);
Throwable result = transformation.transform(exception);
assertEquals(exception, result);
}
private static ApiException getApiExceptionForVersion(
Metadata.Key<byte[]> failureKey, byte[] data) {
Metadata trailers = new Metadata();
if (data != null) {
trailers.put(failureKey, data);
}
return new ApiException(
new StatusException(Status.UNKNOWN, trailers), GrpcStatusCode.of(Code.UNKNOWN), false);
}
/**
* Verifies that the exception transformation behaviour is working for a test example.
*/
@Test
public void exceptionTransformedToGoogleAdsException() {
GoogleAdsClient client =
GoogleAdsClient.newBuilder()
.setCredentials(fakeCredentials)
.setDeveloperToken(DEVELOPER_TOKEN)
.setTransportChannelProvider(localChannelProvider)
.setEnableGeneratedCatalog(enabledGeneratedCatalog)
.build();
Metadata.Key trailerKey = ApiCatalog
.getDefault(enabledGeneratedCatalog)
.getLatestVersion()
.getExceptionFactory()
.getTrailerKey();
Metadata trailers = new Metadata();
GoogleAdsFailure.Builder failure = GoogleAdsFailure.newBuilder();
failure.addErrors(GoogleAdsError.newBuilder().setMessage("Test error message"));
trailers.put(trailerKey, failure.build().toByteArray());
StatusException rootCause = new StatusException(Status.UNKNOWN, trailers);
mockService.addException(new ApiException(rootCause, GrpcStatusCode.of(Code.UNKNOWN), false));
try (GoogleAdsServiceClient googleAdsServiceClient =
client.getLatestVersion().createGoogleAdsServiceClient()) {
googleAdsServiceClient.search("123", "select blah");
} catch (GoogleAdsException ex) {
// Expected
}
}
@Test
public void runExceptionEndOfStreamIsUnavailable() throws IOException, InterruptedException {
RedisClient client = new RedisClient(mock(JedisCluster.class));
Status status = Status.UNKNOWN;
try {
client.run(
jedis -> {
throw new JedisConnectionException("Unexpected end of stream.");
});
} catch (IOException e) {
status = Status.fromThrowable(e);
}
assertThat(status.getCode()).isEqualTo(Code.UNAVAILABLE);
}
@Test
public void runExceptionConnectionResetIsUnavailable() throws IOException, InterruptedException {
RedisClient client = new RedisClient(mock(JedisCluster.class));
Status status = Status.UNKNOWN;
try {
client.run(
jedis -> {
throw new JedisConnectionException(new SocketException("Connection reset"));
});
} catch (IOException e) {
status = Status.fromThrowable(e);
}
assertThat(status.getCode()).isEqualTo(Code.UNAVAILABLE);
}
@Test
public void runExceptionSocketTimeoutExceptionIsDeadlineExceeded()
throws IOException, InterruptedException {
RedisClient client = new RedisClient(mock(JedisCluster.class));
Status status = Status.UNKNOWN;
try {
client.run(
jedis -> {
throw new JedisConnectionException(new SocketTimeoutException());
});
} catch (IOException e) {
status = Status.fromThrowable(e);
}
assertThat(status.getCode()).isEqualTo(Code.DEADLINE_EXCEEDED);
}
synchronized void onComplete(@javax.annotation.Nullable Throwable error) {
if (isCompleted()) {
return;
}
// TODO(songya): add Runnable
Status status;
if (error == null) {
status = Status.OK;
} else if (error instanceof StatusRuntimeException) {
status = ((StatusRuntimeException) error).getStatus();
} else {
status = Status.UNKNOWN;
}
terminateStatus = status;
}
synchronized void onComplete(@javax.annotation.Nullable Throwable error) {
if (isCompleted()) {
return;
}
// TODO(songya): add Runnable
Status status;
if (error == null) {
status = Status.OK;
} else if (error instanceof StatusRuntimeException) {
status = ((StatusRuntimeException) error).getStatus();
} else {
status = Status.UNKNOWN;
}
terminateStatus = status;
}
@SuppressWarnings("checkstyle:ReturnCount")
private Status getStatusFromException(Throwable cause) {
if (cause instanceof StoreException.DataExistsException) {
return Status.ALREADY_EXISTS;
}
if (cause instanceof StoreException.DataNotFoundException) {
return Status.NOT_FOUND;
}
if (cause instanceof StoreException.DataNotEmptyException) {
return Status.FAILED_PRECONDITION;
}
if (cause instanceof StoreException.WriteConflictException) {
return Status.FAILED_PRECONDITION;
}
if (cause instanceof StoreException.IllegalStateException) {
return Status.INTERNAL;
}
if (cause instanceof StoreException.OperationNotAllowedException) {
return Status.PERMISSION_DENIED;
}
if (cause instanceof StoreException.StoreConnectionException) {
return Status.INTERNAL;
}
if (cause instanceof TimeoutException) {
return Status.DEADLINE_EXCEEDED;
}
return Status.UNKNOWN;
}
@Test
public void closedTest() {
Status status = Status.UNKNOWN;
Metadata trailers = new Metadata();
forward.closed(status, trailers);
verify(mock).closed(same(status), same(trailers));
}
@Test
public void pickerEmptyList() throws Exception {
SubchannelPicker picker = new EmptyPicker(Status.UNKNOWN);
assertEquals(null, picker.pickSubchannel(mockArgs).getSubchannel());
assertEquals(Status.UNKNOWN,
picker.pickSubchannel(mockArgs).getStatus());
}
@Test
public void cancelTest() {
Status reason = Status.UNKNOWN;
forward.cancel(reason);
verify(mock).cancel(same(reason));
}
@Test
public void cancelTest() {
Status reason = Status.UNKNOWN;
forward.cancel(reason);
verify(mock).cancel(same(reason));
}