From 34e436ef6e95de2e41140b5788002bf5918fb0f1 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Thu, 3 Mar 2022 10:30:54 +0000 Subject: [PATCH] Accommodate SocketException when reading from stopped TunnelClient TunnelClientTests stopTriggersTunnelClose expects that stopping the client will cause an attempt to read from a connected channel to return -1. With Java 17 on Windows the connection has been reset and a SocketException is thrown instead. This seems reasonable as stopping the client closes the ServerSocketChannel to which the test was connected. This commit updates test to expect a SocketException or a return value of -1. Closes gh-30042 --- .../devtools/tunnel/client/TunnelClientTests.java | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/tunnel/client/TunnelClientTests.java b/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/tunnel/client/TunnelClientTests.java index 1ac57b1bdb..cd920e1796 100644 --- a/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/tunnel/client/TunnelClientTests.java +++ b/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/tunnel/client/TunnelClientTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2021 the original author or authors. + * Copyright 2012-2022 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. @@ -20,6 +20,7 @@ import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.IOException; import java.net.InetSocketAddress; +import java.net.SocketException; import java.nio.ByteBuffer; import java.nio.channels.Channels; import java.nio.channels.SocketChannel; @@ -91,7 +92,17 @@ class TunnelClientTests { assertThat(this.tunnelConnection.isOpen()).isTrue(); client.stop(); assertThat(this.tunnelConnection.isOpen()).isFalse(); - assertThat(channel.read(ByteBuffer.allocate(1))).isEqualTo(-1); + assertThat(readWithPossibleFailure(channel)).satisfiesAnyOf((result) -> assertThat(result).isEqualTo(-1), + (result) -> assertThat(result).isInstanceOf(SocketException.class)); + } + + private Object readWithPossibleFailure(SocketChannel channel) { + try { + return channel.read(ByteBuffer.allocate(1)); + } + catch (Exception ex) { + return ex; + } } @Test