Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ MODULE.bazel.lock
.gitignore
bin

# VsCode
.vscode

# OS X
.DS_Store

Expand Down
57 changes: 57 additions & 0 deletions core/src/main/java/io/grpc/internal/FailingClientCall.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2025 The gRPC Authors
*
* Licensed 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 io.grpc.internal;

import io.grpc.ClientCall;
import io.grpc.Metadata;
import io.grpc.Status;
import javax.annotation.Nullable;

/**
* A {@link ClientCall} that fails immediately upon starting.
*/
public final class FailingClientCall<ReqT, RespT> extends ClientCall<ReqT, RespT> {

private final Status error;

/**
* Creates a new call that will fail with the given error.
*/
public FailingClientCall(Status error) {
this.error = error;
}

/**
* Immediately fails the call by calling {@link Listener#onClose}.
*/
@Override
public void start(Listener<RespT> responseListener, Metadata headers) {
responseListener.onClose(error, new Metadata());
}

@Override
public void request(int numMessages) {}

@Override
public void cancel(@Nullable String message, @Nullable Throwable cause) {}

@Override
public void halfClose() {}

@Override
public void sendMessage(ReqT message) {}
}
76 changes: 76 additions & 0 deletions core/src/test/java/io/grpc/internal/FailingClientCallTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright 2016 The gRPC Authors
*
* Licensed 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 io.grpc.internal;

import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;

import io.grpc.ClientCall;
import io.grpc.Metadata;
import io.grpc.Status;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;

/** Unit tests for {@link FailingClientCall}. */
@RunWith(JUnit4.class)
public class FailingClientCallTest {

@Rule public final MockitoRule mocks = MockitoJUnit.rule();

@Mock
private ClientCall.Listener<Object> mockListener;

@Test
public void startCallsOnClose() {
Status error = Status.UNAVAILABLE.withDescription("test error");
FailingClientCall<Object, Object> call = new FailingClientCall<>(error);
Metadata metadata = new Metadata();
call.start(mockListener, metadata);

ArgumentCaptor<Metadata> metadataCaptor = ArgumentCaptor.forClass(Metadata.class);
verify(mockListener).onClose(eq(error), metadataCaptor.capture());
assertEquals(0, metadataCaptor.getValue().keys().size());
verifyNoMoreInteractions(mockListener);
}

@Test
public void otherMethodsAreNoOps() {
Status error = Status.UNAVAILABLE.withDescription("test error");
FailingClientCall<Object, Object> call = new FailingClientCall<>(error);
Metadata metadata = new Metadata();

call.start(mockListener, metadata); // Must call start first

call.request(1);
call.cancel("message", new RuntimeException("cause"));
call.halfClose();
call.sendMessage(new Object());

ArgumentCaptor<Metadata> metadataCaptor = ArgumentCaptor.forClass(Metadata.class);
verify(mockListener).onClose(eq(error), metadataCaptor.capture());
assertEquals(0, metadataCaptor.getValue().keys().size());
verifyNoMoreInteractions(mockListener);
}
}
Loading
Loading