|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.exporter.sender.okhttp.internal; |
| 7 | + |
| 8 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 9 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 10 | + |
| 11 | +import io.opentelemetry.exporter.internal.RetryUtil; |
| 12 | +import io.opentelemetry.exporter.internal.grpc.GrpcExporterUtil; |
| 13 | +import java.util.Set; |
| 14 | +import okhttp3.MediaType; |
| 15 | +import okhttp3.Protocol; |
| 16 | +import okhttp3.Request; |
| 17 | +import okhttp3.Response; |
| 18 | +import okhttp3.ResponseBody; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | +import org.junit.jupiter.params.ParameterizedTest; |
| 21 | +import org.junit.jupiter.params.provider.MethodSource; |
| 22 | + |
| 23 | +class OkHttpGrpcSenderTest { |
| 24 | + |
| 25 | + private static final String GRPC_STATUS = "grpc-status"; |
| 26 | + private static final MediaType TEXT_PLAIN = MediaType.get("text/plain"); |
| 27 | + |
| 28 | + static Set<String> provideRetryableGrpcStatusCodes() { |
| 29 | + return RetryUtil.retryableGrpcStatusCodes(); |
| 30 | + } |
| 31 | + |
| 32 | + @ParameterizedTest(name = "isRetryable should return true for GRPC status code: {0}") |
| 33 | + @MethodSource("provideRetryableGrpcStatusCodes") |
| 34 | + void isRetryable_RetryableGrpcStatus(String retryableGrpcStatus) { |
| 35 | + Response response = createResponse(503, retryableGrpcStatus, "Retryable"); |
| 36 | + boolean isRetryable = OkHttpGrpcSender.isRetryable(response); |
| 37 | + assertTrue(isRetryable); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + void isRetryable_NonRetryableGrpcStatus() { |
| 42 | + String nonRetryableGrpcStatus = |
| 43 | + Integer.valueOf(GrpcExporterUtil.GRPC_STATUS_UNKNOWN).toString(); // INVALID_ARGUMENT |
| 44 | + Response response = createResponse(503, nonRetryableGrpcStatus, "Non-retryable"); |
| 45 | + boolean isRetryable = OkHttpGrpcSender.isRetryable(response); |
| 46 | + assertFalse(isRetryable); |
| 47 | + } |
| 48 | + |
| 49 | + private static Response createResponse(int httpCode, String grpcStatus, String message) { |
| 50 | + return new Response.Builder() |
| 51 | + .request(new Request.Builder().url("http://localhost/").build()) |
| 52 | + .protocol(Protocol.HTTP_2) |
| 53 | + .code(httpCode) |
| 54 | + .body(ResponseBody.create("body", TEXT_PLAIN)) |
| 55 | + .message(message) |
| 56 | + .header(GRPC_STATUS, grpcStatus) |
| 57 | + .build(); |
| 58 | + } |
| 59 | +} |
0 commit comments