Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit test coverage for PendingAuthListener. #10806

Merged
merged 3 commits into from
Jan 22, 2024
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package io.grpc.binder.internal;

import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;

import org.junit.Before;
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.Captor;
import org.mockito.InOrder;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;

import io.grpc.Metadata;
import io.grpc.ServerCall;
import io.grpc.ServerCallHandler;
import io.grpc.Status;

@RunWith(JUnit4.class)
public final class PendingAuthListenerTest {

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

@Mock ServerCallHandler<Object, Object> next;
@Mock ServerCall<Object, Object> call;
@Mock ServerCall.Listener<Object> delegate;
@Captor ArgumentCaptor<Status> statusCaptor;

private final Metadata headers = new Metadata();
private final PendingAuthListener<Object, Object> listener = new PendingAuthListener<>();

@Before
public void setUp() {
when(next.startCall(call, headers)).thenReturn(delegate);
}

@Test
public void onCallbacks_noOpBeforeStartCall() {
listener.onReady();
listener.onMessage("foo");
listener.onHalfClose();
listener.onComplete();

verifyNoInteractions(delegate);
}

@Test
public void onCallbacks_runsPendingCallbacksAfterStartCall() {
String message = "foo";

// Act 1
listener.onReady();
listener.onMessage(message);
listener.startCall(call, headers, next);

// Assert 1
InOrder order = Mockito.inOrder(delegate);
order.verify(delegate).onReady();
order.verify(delegate).onMessage(message);

// Act 2
listener.onHalfClose();
listener.onComplete();

// Assert 2
order.verify(delegate).onHalfClose();
order.verify(delegate).onComplete();
}

@Test
public void onCallbacks_withCancellation_runsPendingCallbacksAfterStartCall() {
listener.onReady();
listener.onCancel();
listener.startCall(call, headers, next);

InOrder order = Mockito.inOrder(delegate);
order.verify(delegate).onReady();
order.verify(delegate).onCancel();
}

@Test
public void whenStartCallFails_closesTheCallWithInternalStatus() {
IllegalStateException exception = new IllegalStateException("oops");
when(next.startCall(any(), any())).thenThrow(exception);

listener.onReady();
listener.startCall(call, headers, next);

verify(call).close(statusCaptor.capture(), any());
Status status = statusCaptor.getValue();
assertThat(status.getCode()).isEqualTo(Status.Code.INTERNAL);
assertThat(status.getCause()).isSameInstanceAs(exception);
}
}