|
| 1 | +/* |
| 2 | + * Copyright (c) 2016-2024 The gRPC-Spring Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package net.devh.boot.grpc.test.setup; |
| 18 | + |
| 19 | +import static io.grpc.Status.DEADLINE_EXCEEDED; |
| 20 | +import static net.devh.boot.grpc.test.util.GrpcAssertions.assertStatus; |
| 21 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 22 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 23 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 24 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 25 | + |
| 26 | +import java.util.concurrent.ExecutionException; |
| 27 | + |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | +import org.springframework.boot.test.context.SpringBootTest; |
| 30 | +import org.springframework.test.annotation.DirtiesContext; |
| 31 | +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; |
| 32 | + |
| 33 | +import io.grpc.StatusRuntimeException; |
| 34 | +import io.grpc.internal.testing.StreamRecorder; |
| 35 | +import io.grpc.stub.StreamObserver; |
| 36 | +import lombok.SneakyThrows; |
| 37 | +import lombok.extern.slf4j.Slf4j; |
| 38 | +import net.devh.boot.grpc.client.config.GrpcChannelProperties; |
| 39 | +import net.devh.boot.grpc.test.config.BaseAutoConfiguration; |
| 40 | +import net.devh.boot.grpc.test.config.ServiceConfiguration; |
| 41 | +import net.devh.boot.grpc.test.proto.SomeType; |
| 42 | + |
| 43 | +/** |
| 44 | + * These tests check the property {@link GrpcChannelProperties#getDeadline()}. |
| 45 | + */ |
| 46 | +@Slf4j |
| 47 | +@SpringBootTest(properties = { |
| 48 | + "grpc.client.GLOBAL.address=localhost:9090", |
| 49 | + "grpc.client.GLOBAL.deadline=1s", |
| 50 | + "grpc.client.GLOBAL.negotiationType=PLAINTEXT", |
| 51 | +}) |
| 52 | +@SpringJUnitConfig(classes = {ServiceConfiguration.class, BaseAutoConfiguration.class}) |
| 53 | +public class DeadlineTests extends AbstractSimpleServerClientTest { |
| 54 | + |
| 55 | + @Test |
| 56 | + @SneakyThrows |
| 57 | + @DirtiesContext |
| 58 | + void testServiceStubDeadlineEnabledAndSuccessful() { |
| 59 | + log.info("--- Starting test with unsuccessful and than successful call ---"); |
| 60 | + final StreamRecorder<SomeType> streamRecorder1 = StreamRecorder.create(); |
| 61 | + StreamObserver<SomeType> echo1 = this.testServiceStub.echo(streamRecorder1); |
| 62 | + assertThrows(ExecutionException.class, () -> streamRecorder1.firstValue().get()); |
| 63 | + |
| 64 | + final StreamRecorder<SomeType> streamRecorder2 = StreamRecorder.create(); |
| 65 | + StreamObserver<SomeType> echo2 = testServiceStub.echo(streamRecorder2); |
| 66 | + echo2.onNext(SomeType.getDefaultInstance()); |
| 67 | + assertNull(streamRecorder2.getError()); |
| 68 | + assertNotNull(streamRecorder2.firstValue().get().getVersion()); |
| 69 | + log.info("--- Test completed --- "); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + @SneakyThrows |
| 74 | + @DirtiesContext |
| 75 | + void testServiceStubDeadlineEnabledAndUnsuccessful() { |
| 76 | + log.info("--- Starting test with unsuccessful call ---"); |
| 77 | + final StreamRecorder<SomeType> streamRecorder = StreamRecorder.create(); |
| 78 | + this.testServiceStub.echo(streamRecorder); |
| 79 | + assertThrows(ExecutionException.class, () -> streamRecorder.firstValue().get()); |
| 80 | + assertNotNull(streamRecorder.getError()); |
| 81 | + assertEquals(StatusRuntimeException.class, streamRecorder.getError().getClass()); |
| 82 | + assertStatus(DEADLINE_EXCEEDED.getCode(), (StatusRuntimeException) streamRecorder.getError()); |
| 83 | + log.info("--- Test completed --- "); |
| 84 | + } |
| 85 | + |
| 86 | +} |
0 commit comments