Skip to content

Commit

Permalink
add unit test testConnectTimeoutConfigurationReloadWorks.
Browse files Browse the repository at this point in the history
Signed-off-by: qnnn <[email protected]>
  • Loading branch information
qnnn committed Jan 23, 2025
1 parent 8599aee commit 93fa296
Showing 1 changed file with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2020 the original author or authors.
* Copyright 2013-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,25 +16,35 @@

package org.springframework.cloud.gateway.filter;

import java.util.Collections;
import java.util.HashMap;

import io.netty.channel.ChannelOption;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import reactor.core.publisher.Mono;
import reactor.netty.DisposableServer;
import reactor.netty.http.client.HttpClient;
import reactor.netty.http.client.HttpClientConfig;
import reactor.netty.http.server.HttpServer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.context.environment.EnvironmentChangeEvent;
import org.springframework.cloud.gateway.route.Route;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.cloud.gateway.test.BaseWebClientTests;
import org.springframework.cloud.gateway.test.PermitAllSecurityConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.test.util.TestSocketUtils;
import org.springframework.test.web.reactive.server.WebTestClient;

Expand All @@ -48,6 +58,12 @@ class NettyRoutingFilterTests extends BaseWebClientTests {
@Autowired
private ApplicationContext context;

@Autowired
private NettyRoutingFilter nettyRoutingFilter;

@Autowired
private RouteLocator routeLocator;

@BeforeAll
public static void beforeAll() {
port = TestSocketUtils.findAvailableTcpPort();
Expand Down Expand Up @@ -88,6 +104,30 @@ void testCaseInsensitiveScheme() {
}
}

@Test
void testConnectTimeoutConfigurationReloadWorks() {
ConfigurableEnvironment environment = (ConfigurableEnvironment) this.context.getEnvironment();
HashMap<String, Object> map = new HashMap<>();
environment.getPropertySources().addFirst(new MapPropertySource("mock", map));
map.put("spring.cloud.gateway.httpclient.connect-timeout", "1000");
this.context.publishEvent(new EnvironmentChangeEvent(this.context,
Collections.singleton("spring.cloud.gateway.httpclient.connect-timeout")));
Route route = routeLocator.getRoutes()
.filter(r -> r.getId().equals("refreshable_configuration_test")).blockLast();
assertThat(route).isNotNull();
HttpClient httpClient = nettyRoutingFilter.getHttpClient(route, null);
HttpClientConfig configuration = httpClient.configuration();
assertThat(configuration.options().get(ChannelOption.CONNECT_TIMEOUT_MILLIS)).isEqualTo(1000);

map.put("spring.cloud.gateway.httpclient.connect-timeout", "5000");
this.context.publishEvent(new EnvironmentChangeEvent(this.context,
Collections.singleton("spring.cloud.gateway.httpclient.connect-timeout")));
httpClient = nettyRoutingFilter.getHttpClient(route, null);
configuration = httpClient.configuration();
assertThat(configuration.options().get(ChannelOption.CONNECT_TIMEOUT_MILLIS)).isEqualTo(5000);
environment.getPropertySources().remove("mock");
}

@SpringBootConfiguration
@EnableAutoConfiguration
@Import(PermitAllSecurityConfiguration.class)
Expand All @@ -98,6 +138,7 @@ public RouteLocator routes(RouteLocatorBuilder builder) {
return builder.routes()
.route(p -> p.path("/mockexample").filters(f -> f.prefixPath("/httpbin")).uri("http://example.com"))
.route(p -> p.path("/issue").uri("HTTP://127.0.0.1:" + port))
.route("refreshable_configuration_test", p -> p.path("/refresh").uri("http://example.com"))
.build();
}

Expand Down

0 comments on commit 93fa296

Please sign in to comment.