Skip to content

Commit 25b384b

Browse files
Dave Syerdsyer
Dave Syer
authored andcommitted
Add a test for autroconfig of servlet
Signed-off-by: Dave Syer <dsvid.syer@broadcom.com>
1 parent d5fb8ea commit 25b384b

File tree

3 files changed

+112
-11
lines changed

3 files changed

+112
-11
lines changed

spring-grpc-docs/src/main/antora/modules/ROOT/partials/_configprops.adoc

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,23 @@
1212
|spring.grpc.client.default-channel.max-inbound-message-size | |
1313
|spring.grpc.client.default-channel.max-inbound-metadata-size | |
1414
|spring.grpc.client.default-channel.negotiation-type | | The negotiation type for the channel. Default is {@link NegotiationType#PLAINTEXT}.
15-
|spring.grpc.client.default-channel.secure | `+++true+++` | Flag to say that strict SSL checks are not enabled (so the remote certificate could be anonymous).
15+
|spring.grpc.client.default-channel.secure | | Flag to say that strict SSL checks are not enabled (so the remote certificate could be anonymous).
1616
|spring.grpc.client.default-channel.ssl.bundle | | SSL bundle name.
1717
|spring.grpc.client.default-channel.ssl.enabled | | Whether to enable SSL support. Enabled automatically if "bundle" is provided unless specified otherwise.
1818
|spring.grpc.client.default-channel.user-agent | |
1919
|spring.grpc.server.address | | The address to bind to. could be a host:port combination or a pseudo URL like static://host:port. Can not be set if host or port are set independently.
20-
|spring.grpc.server.host | `+++*+++` | Server address to bind to. The default is any IP address ('*').
20+
|spring.grpc.server.host | | Server address to bind to. The default is any IP address ('*').
2121
|spring.grpc.server.keep-alive.max-age | | Maximum time a connection may exist before being gracefully terminated (default infinite).
2222
|spring.grpc.server.keep-alive.max-age-grace | | Maximum time for graceful connection termination (default infinite).
2323
|spring.grpc.server.keep-alive.max-idle | | Maximum time a connection can remain idle before being gracefully terminated (default infinite).
24-
|spring.grpc.server.keep-alive.permit-time | `+++5m+++` | Maximum keep-alive time clients are permitted to configure (default 5m).
25-
|spring.grpc.server.keep-alive.permit-without-calls | `+++false+++` | Whether clients are permitted to send keep alive pings when there are no outstanding RPCs on the connection (default false).
26-
|spring.grpc.server.keep-alive.time | `+++2h+++` | Duration without read activity before sending a keep alive ping (default 2h).
27-
|spring.grpc.server.keep-alive.timeout | `+++20s+++` | Maximum time to wait for read activity after sending a keep alive ping. If sender does not receive an acknowledgment within this time, it will close the connection (default 20s).
28-
|spring.grpc.server.max-inbound-message-size | `+++4194304B+++` | Maximum message size allowed to be received by the server (default 4MiB).
29-
|spring.grpc.server.max-inbound-metadata-size | `+++8192B+++` | Maximum metadata size allowed to be received by the server (default 8KiB).
30-
|spring.grpc.server.port | `+++9090+++` | Server port to listen on. When the value is 0, a random available port is selected. The default is 9090.
31-
|spring.grpc.server.shutdown-grace-period | `+++30s+++` | Maximum time to wait for the server to gracefully shutdown. When the value is negative, the server waits forever. When the value is 0, the server will force shutdown immediately. The default is 30 seconds.
24+
|spring.grpc.server.keep-alive.permit-time | | Maximum keep-alive time clients are permitted to configure (default 5m).
25+
|spring.grpc.server.keep-alive.permit-without-calls | | Whether clients are permitted to send keep alive pings when there are no outstanding RPCs on the connection (default false).
26+
|spring.grpc.server.keep-alive.time | | Duration without read activity before sending a keep alive ping (default 2h).
27+
|spring.grpc.server.keep-alive.timeout | | Maximum time to wait for read activity after sending a keep alive ping. If sender does not receive an acknowledgment within this time, it will close the connection (default 20s).
28+
|spring.grpc.server.max-inbound-message-size | | Maximum message size allowed to be received by the server (default 4MiB).
29+
|spring.grpc.server.max-inbound-metadata-size | | Maximum metadata size allowed to be received by the server (default 8KiB).
30+
|spring.grpc.server.port | | Server port to listen on. When the value is 0, a random available port is selected. The default is 9090.
31+
|spring.grpc.server.shutdown-grace-period | | Maximum time to wait for the server to gracefully shutdown. When the value is negative, the server waits forever. When the value is 0, the server will force shutdown immediately. The default is 30 seconds.
3232
|spring.grpc.server.ssl.bundle | | SSL bundle name.
3333
|spring.grpc.server.ssl.enabled | | Whether to enable SSL support. Enabled automatically if "bundle" is provided unless specified otherwise.
3434

spring-grpc-spring-boot-autoconfigure/pom.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@
4545
<version>${project.version}</version>
4646
<optional>true</optional>
4747
</dependency>
48+
<dependency>
49+
<groupId>org.springframework</groupId>
50+
<artifactId>spring-web</artifactId>
51+
<optional>true</optional>
52+
</dependency>
4853
<dependency>
4954
<groupId>org.apache.tomcat.embed</groupId>
5055
<artifactId>tomcat-embed-core</artifactId>
@@ -86,4 +91,4 @@
8691

8792
</dependencies>
8893

89-
</project>
94+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright 2023-2024 the original author or 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+
* https://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 org.springframework.grpc.autoconfigure.server;
18+
19+
import static org.assertj.core.api.Assertions.assertThat;
20+
import static org.mockito.ArgumentMatchers.any;
21+
import static org.mockito.ArgumentMatchers.anyInt;
22+
import static org.mockito.Mockito.inOrder;
23+
import static org.mockito.Mockito.mock;
24+
import static org.mockito.Mockito.when;
25+
26+
import java.time.Duration;
27+
import java.util.List;
28+
import java.util.concurrent.TimeUnit;
29+
30+
import org.assertj.core.api.InstanceOfAssertFactories;
31+
import org.junit.jupiter.api.Test;
32+
import org.mockito.InOrder;
33+
import org.mockito.MockedStatic;
34+
import org.mockito.Mockito;
35+
import org.mockito.stubbing.Answer;
36+
import org.springframework.boot.autoconfigure.AutoConfigurations;
37+
import org.springframework.boot.autoconfigure.ssl.SslAutoConfiguration;
38+
import org.springframework.boot.test.context.FilteredClassLoader;
39+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
40+
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
41+
import org.springframework.boot.web.servlet.ServletRegistrationBean;
42+
import org.springframework.context.annotation.Bean;
43+
import org.springframework.context.annotation.Configuration;
44+
import org.springframework.core.annotation.Order;
45+
import org.springframework.grpc.server.GrpcServerFactory;
46+
import org.springframework.grpc.server.NettyGrpcServerFactory;
47+
import org.springframework.grpc.server.ServerBuilderCustomizer;
48+
import org.springframework.grpc.server.ShadedNettyGrpcServerFactory;
49+
import org.springframework.grpc.server.lifecycle.GrpcServerLifecycle;
50+
51+
import io.grpc.BindableService;
52+
import io.grpc.Grpc;
53+
import io.grpc.ServerBuilder;
54+
import io.grpc.ServerServiceDefinition;
55+
import io.grpc.ServiceDescriptor;
56+
import io.grpc.netty.NettyServerBuilder;
57+
58+
/**
59+
* Tests for {@link GrpcServerAutoConfiguration}.
60+
*
61+
* @author Chris Bono
62+
*/
63+
class GrpcServletAutoConfigurationTests {
64+
65+
private WebApplicationContextRunner contextRunner() {
66+
BindableService service = mock();
67+
ServerServiceDefinition serviceDefinition = ServerServiceDefinition.builder("my-service").build();
68+
when(service.bindService()).thenReturn(serviceDefinition);
69+
// NOTE: we use noop server lifecycle to avoid startup
70+
return new WebApplicationContextRunner()
71+
.withConfiguration(
72+
AutoConfigurations.of(GrpcServerAutoConfiguration.class, GrpcServerFactoryAutoConfiguration.class))
73+
.withBean(BindableService.class, () -> service);
74+
}
75+
76+
@Test
77+
void whenGrpcNotOnClasspathAutoConfigurationIsSkipped() {
78+
this.contextRunner()
79+
.withClassLoader(new FilteredClassLoader(BindableService.class))
80+
.run((context) -> assertThat(context).doesNotHaveBean(GrpcServerAutoConfiguration.class)
81+
.doesNotHaveBean(ServletRegistrationBean.class));
82+
}
83+
84+
@Test
85+
void whenNoBindableServicesRegisteredAutoConfigurationIsSkipped() {
86+
new ApplicationContextRunner().withConfiguration(AutoConfigurations.of(GrpcServerAutoConfiguration.class))
87+
.run((context) -> assertThat(context).doesNotHaveBean(GrpcServerAutoConfiguration.class)
88+
.doesNotHaveBean(ServletRegistrationBean.class));
89+
}
90+
91+
@Test
92+
void whenWebApplicationServletIsAutoConfigured() {
93+
this.contextRunner().run((context) -> assertThat(context).getBean(ServletRegistrationBean.class).isNotNull());
94+
}
95+
96+
}

0 commit comments

Comments
 (0)