This commit is contained in:
2026-08-25 14:46:06 +08:00
parent 5abc7543ae
commit f325d57fc1
106 changed files with 3682 additions and 450 deletions
@@ -0,0 +1,84 @@
package com.baiyue.lastleg.config;
import com.sun.net.httpserver.HttpServer;
import org.junit.jupiter.api.Test;
import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.boot.context.properties.source.MapConfigurationPropertySource;
import org.springframework.web.client.ResourceAccessException;
import org.springframework.web.client.RestClient;
import java.net.InetSocketAddress;
import java.time.Duration;
import java.util.Map;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
class LastlegRestClientFactoryTest {
private final LastlegRestClientFactory factory = new LastlegRestClientFactory();
@Test
void defaultsArePositiveAndSeparateFileUploadsFromWarehouseCalls() {
LastlegRestClientProperties properties = new LastlegRestClientProperties();
assertThat(properties.getUnloadPhotoFile().getConnectTimeout()).isEqualTo(Duration.ofSeconds(3));
assertThat(properties.getUnloadPhotoFile().getReadTimeout()).isEqualTo(Duration.ofSeconds(30));
assertThat(properties.getRelabelPhotoFile().getReadTimeout()).isEqualTo(Duration.ofSeconds(30));
assertThat(properties.getWarehouseResource().getReadTimeout()).isEqualTo(Duration.ofSeconds(5));
assertThat(properties.getPalletPhysical().getReadTimeout()).isEqualTo(Duration.ofSeconds(5));
}
@Test
void rejectsUnboundedOrNonPositiveTimeouts() {
assertThatThrownBy(() -> new LastlegRestClientProperties.ClientTimeouts(null, Duration.ofSeconds(1)))
.isInstanceOf(IllegalArgumentException.class);
assertThatThrownBy(() -> new LastlegRestClientProperties.ClientTimeouts(Duration.ZERO, Duration.ofSeconds(1)))
.isInstanceOf(IllegalArgumentException.class);
assertThatThrownBy(() -> new LastlegRestClientProperties.ClientTimeouts(Duration.ofSeconds(1), Duration.ZERO))
.isInstanceOf(IllegalArgumentException.class);
}
@Test
void bindsPerClientTimeoutOverridesFromConfiguration() {
var source = new MapConfigurationPropertySource(Map.of(
"baiyue.http.clients.unload-photo-file.connect-timeout", "125ms",
"baiyue.http.clients.unload-photo-file.read-timeout", "9s",
"baiyue.http.clients.pallet-physical.connect-timeout", "250ms",
"baiyue.http.clients.pallet-physical.read-timeout", "2s"));
LastlegRestClientProperties properties = new Binder(source)
.bind("baiyue.http.clients", Bindable.of(LastlegRestClientProperties.class))
.orElseThrow(() -> new AssertionError("Lastleg HTTP client properties were not bound"));
assertThat(properties.getUnloadPhotoFile().getConnectTimeout()).isEqualTo(Duration.ofMillis(125));
assertThat(properties.getUnloadPhotoFile().getReadTimeout()).isEqualTo(Duration.ofSeconds(9));
assertThat(properties.getPalletPhysical().getConnectTimeout()).isEqualTo(Duration.ofMillis(250));
assertThat(properties.getPalletPhysical().getReadTimeout()).isEqualTo(Duration.ofSeconds(2));
assertThat(properties.getWarehouseResource().getReadTimeout()).isEqualTo(Duration.ofSeconds(5));
}
@Test
void appliesConfiguredConnectAndReadTimeoutsWithoutRetryOrFallback() throws Exception {
var timeouts = new LastlegRestClientProperties.ClientTimeouts(
Duration.ofMillis(250), Duration.ofMillis(75));
assertThat(factory.httpClient(timeouts).connectTimeout()).contains(Duration.ofMillis(250));
HttpServer server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
server.createContext("/slow", exchange -> {
try { Thread.sleep(500); }
catch (InterruptedException interrupted) { Thread.currentThread().interrupt(); }
exchange.sendResponseHeaders(200, 0);
exchange.close();
});
server.start();
try {
RestClient client = factory.build(RestClient.builder(),
"http://127.0.0.1:" + server.getAddress().getPort(), timeouts);
assertThatThrownBy(() -> client.get().uri("/slow").retrieve().toBodilessEntity())
.isInstanceOf(ResourceAccessException.class);
} finally {
server.stop(0);
}
}
}