Skip to content

Commit d49fdaf

Browse files
authored
Merge pull request #31 from Blazemeter/FIX_NPE_ON_TIMEOUT
Fixed NPE on TimeOut exceptions
2 parents fce3a23 + 002a66e commit d49fdaf

File tree

2 files changed

+62
-72
lines changed

2 files changed

+62
-72
lines changed

src/main/java/com/blazemeter/jmeter/http2/core/HTTP2JettyClient.java

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,26 @@
11
package com.blazemeter.jmeter.http2.core;
22

33
import com.blazemeter.jmeter.http2.sampler.HTTP2Sampler;
4+
import java.io.ByteArrayInputStream;
5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
import java.io.UnsupportedEncodingException;
8+
import java.net.URI;
9+
import java.net.URISyntaxException;
10+
import java.net.URL;
11+
import java.net.URLDecoder;
12+
import java.nio.charset.Charset;
13+
import java.nio.charset.StandardCharsets;
14+
import java.nio.file.Path;
15+
import java.nio.file.Paths;
16+
import java.util.Arrays;
17+
import java.util.HashSet;
18+
import java.util.Set;
19+
import java.util.concurrent.ExecutionException;
20+
import java.util.concurrent.TimeUnit;
21+
import java.util.concurrent.TimeoutException;
22+
import java.util.regex.Pattern;
23+
import java.util.stream.StreamSupport;
424
import org.apache.commons.lang3.StringUtils;
525
import org.apache.jmeter.protocol.http.control.AuthManager;
626
import org.apache.jmeter.protocol.http.control.Authorization;
@@ -46,27 +66,6 @@
4666
import org.slf4j.Logger;
4767
import org.slf4j.LoggerFactory;
4868

49-
import java.io.ByteArrayInputStream;
50-
import java.io.IOException;
51-
import java.io.InputStream;
52-
import java.io.UnsupportedEncodingException;
53-
import java.net.URI;
54-
import java.net.URISyntaxException;
55-
import java.net.URL;
56-
import java.net.URLDecoder;
57-
import java.nio.charset.Charset;
58-
import java.nio.charset.StandardCharsets;
59-
import java.nio.file.Path;
60-
import java.nio.file.Paths;
61-
import java.util.Arrays;
62-
import java.util.HashSet;
63-
import java.util.Set;
64-
import java.util.concurrent.ExecutionException;
65-
import java.util.concurrent.TimeUnit;
66-
import java.util.concurrent.TimeoutException;
67-
import java.util.regex.Pattern;
68-
import java.util.stream.StreamSupport;
69-
7069
public class HTTP2JettyClient {
7170

7271
private static final Logger LOG = LoggerFactory.getLogger(HTTP2JettyClient.class);
@@ -169,24 +168,25 @@ public ContentResponse send(HttpRequest request) throws InterruptedException,
169168
String maxBufferSizeString = JMeterUtils.getPropDefault("httpJettyClient.maxBufferSize",
170169
String.valueOf(2 * 1024 * 1024));
171170

172-
LOG.debug("Setting max buffer size to {}", maxBufferSizeString);
171+
if (LOG.isDebugEnabled()) {
172+
LOG.debug("Sending request: {}", request);
173+
LOG.debug("Setting max buffer size to {}", maxBufferSizeString);
174+
}
173175
FutureResponseListener listener =
174176
new FutureResponseListener(request, Integer.parseInt(maxBufferSizeString));
175177
request.send(listener);
178+
int timeout = JMeterUtils.getPropDefault("HTTPSampler.response_timeout", 2000);
176179

177180
try {
178-
return listener.get(JMeterUtils.getPropDefault(
179-
"HTTPSampler.response_timeout", 2000), TimeUnit.MILLISECONDS);
180-
} catch (TimeoutException | ExecutionException e) {
181-
if (e instanceof TimeoutException || e.getCause() instanceof TimeoutException) {
182-
throw (TimeoutException) e.getCause();
183-
} else if (e.getMessage().matches(
184-
"java.lang.IllegalArgumentException: Buffering capacity \\d+ exceeded")) {
185-
throw new IllegalArgumentException("Buffer capacity " + maxBufferSizeString + " exceeded. "
186-
+ "To modify buffer size, set the property httpJettyClient.maxBufferSize "
187-
+ "in the jmeter.properties file");
188-
} else if (e.getCause() instanceof TimeoutException) {
181+
return listener.get(timeout, TimeUnit.MILLISECONDS);
182+
} catch (TimeoutException e) {
183+
throw new TimeoutException("The request took more than " + timeout
184+
+ " milliseconds to complete");
185+
} catch (ExecutionException e) {
186+
if (e.getCause() != null && e.getCause() instanceof TimeoutException) {
189187
throw (TimeoutException) e.getCause();
188+
} else if (e.getCause() != null && e.getCause() instanceof IllegalArgumentException) {
189+
throw (IllegalArgumentException) e.getCause();
190190
}
191191
throw e;
192192
}

src/test/java/com/blazemeter/jmeter/http2/core/HTTP2JettyClientTest.java

Lines changed: 29 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,32 @@
11
package com.blazemeter.jmeter.http2.core;
22

3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.junit.Assert.assertThrows;
5+
36
import com.blazemeter.jmeter.http2.sampler.HTTP2Sampler;
47
import com.blazemeter.jmeter.http2.sampler.JMeterTestUtils;
58
import com.google.common.base.Stopwatch;
69
import com.google.common.io.Resources;
710
import jakarta.servlet.http.HttpServlet;
811
import jakarta.servlet.http.HttpServletRequest;
912
import jakarta.servlet.http.HttpServletResponse;
13+
import java.io.ByteArrayOutputStream;
14+
import java.io.IOException;
15+
import java.io.InputStream;
16+
import java.net.MalformedURLException;
17+
import java.net.URL;
18+
import java.nio.charset.StandardCharsets;
19+
import java.nio.file.Files;
20+
import java.nio.file.Paths;
21+
import java.util.Arrays;
22+
import java.util.Base64;
23+
import java.util.Collections;
24+
import java.util.List;
25+
import java.util.concurrent.ExecutionException;
26+
import java.util.concurrent.TimeUnit;
27+
import java.util.concurrent.TimeoutException;
28+
import java.util.stream.Collectors;
29+
import java.util.zip.GZIPOutputStream;
1030
import jodd.net.MimeTypes;
1131
import org.apache.jmeter.config.Arguments;
1232
import org.apache.jmeter.protocol.http.control.AuthManager;
@@ -55,27 +75,6 @@
5575
import org.junit.runner.RunWith;
5676
import org.mockito.junit.MockitoJUnitRunner;
5777

58-
import java.io.ByteArrayOutputStream;
59-
import java.io.IOException;
60-
import java.io.InputStream;
61-
import java.net.MalformedURLException;
62-
import java.net.URL;
63-
import java.nio.charset.StandardCharsets;
64-
import java.nio.file.Files;
65-
import java.nio.file.Paths;
66-
import java.util.Arrays;
67-
import java.util.Base64;
68-
import java.util.Collections;
69-
import java.util.List;
70-
import java.util.concurrent.ExecutionException;
71-
import java.util.concurrent.TimeUnit;
72-
import java.util.concurrent.TimeoutException;
73-
import java.util.stream.Collectors;
74-
import java.util.zip.GZIPOutputStream;
75-
76-
import static org.assertj.core.api.Assertions.assertThat;
77-
import static org.junit.Assert.assertThrows;
78-
7978
@RunWith(MockitoJUnitRunner.class)
8079
public class HTTP2JettyClientTest {
8180

@@ -301,17 +300,17 @@ public void shouldSendBodyInformationWhenRequestWithBodyRaw() throws Exception {
301300
}
302301

303302
private HTTPSampleResult buildOkResult(String requestBody, String requestContentType) {
304-
return buildResult(true, Code.OK, null,
303+
return buildResult(true, HttpStatus.Code.OK, null,
305304
requestBody != null ? requestBody.getBytes(StandardCharsets.UTF_8) : null,
306305
requestContentType);
307306
}
308307

309-
private HTTPSampleResult buildResult(boolean successful, Code statusCode,
308+
private HTTPSampleResult buildResult(boolean successful, HttpStatus.Code statusCode,
310309
HttpFields headers, byte[] requestBody, String requestContentType) {
311310

312311
Mutable httpFields = HttpFields.build()
313312
.add(HttpHeader.ACCEPT_ENCODING, "gzip")
314-
.add(HttpHeader.USER_AGENT, "Jetty/11.0.6");
313+
.add(HttpHeader.USER_AGENT, "Jetty/11.0.10");
315314

316315
if (requestContentType != null) {
317316
httpFields.add(HttpHeader.CONTENT_TYPE, requestContentType);
@@ -613,14 +612,12 @@ public void shouldGetFileDataWithFileIsSentAsBodyPart() throws Exception {
613612
validateResponse(result, expected);
614613
}
615614

616-
@Test
617-
public void shouldReturnErrorMessageWhenResponseTimeIsOver() throws Exception {
615+
@Test(expected = TimeoutException.class)
616+
public void shouldThrowTimeoutExceptionWhenResponseBiggerThanTimeout() throws Exception {
618617
buildStartedServer();
619618
long timeout = 1000;
620619
sampler.setResponseTimeout(String.valueOf(timeout));
621-
Stopwatch waitTime = Stopwatch.createStarted();
622-
assertThrows(TimeoutException.class, () -> sampleWithGet(SERVER_PATH_SLOW));
623-
softly.assertThat(waitTime.elapsed(TimeUnit.MILLISECONDS)).isGreaterThanOrEqualTo(timeout);
620+
sampleWithGet(SERVER_PATH_SLOW);
624621
}
625622

626623
@Test
@@ -869,7 +866,7 @@ public void shouldNotUseMultipartWhenHasOneFileWithEmptyParamName() throws Excep
869866
buildStartedServer();
870867
URL file = getClass().getResource("blazemeter-labs-logo.png");
871868
sampler.setHTTPFiles(new HTTPFileArg[]{new HTTPFileArg(file.getPath(), "", "image/png")});
872-
HTTPSampleResult expected = buildResult(true, Code.OK, null,
869+
HTTPSampleResult expected = buildResult(true, HttpStatus.Code.OK, null,
873870
Resources.toByteArray(file), "image/png");
874871
validateResponse(sample(SERVER_PATH_200_FILE_SENT, HTTPConstants.POST), expected);
875872
}
@@ -923,19 +920,12 @@ public void shouldThrowAnExceptionWhenBufferSizeIsBiggerThanMaxBufferSize() thro
923920
sampleWithGet(SERVER_PATH_BIG_RESPONSE);
924921
}
925922

926-
@Test
923+
@Test(expected = IllegalArgumentException.class)
927924
public void shouldNotGetAResponseWhenBufferSizeIsBiggerThanMaxBufferSize() throws Exception {
928925
buildStartedServer();
929926
JMeterUtils.setProperty("httpJettyClient.maxBufferSize", String.valueOf(BIG_BUFFER_SIZE - 1));
930927
//There is no response, since an exception is thrown in this case
931-
932-
Exception exception = assertThrows(IllegalArgumentException.class,
933-
() -> sampleWithGet(SERVER_PATH_BIG_RESPONSE));
934-
935-
assertThat(exception.toString()).contains(
936-
"java.lang.IllegalArgumentException: Buffer capacity 4194303 exceeded."
937-
+ " To modify buffer size, set the property httpJettyClient.maxBufferSize"
938-
+ " in the jmeter.properties file");
928+
sampleWithGet(SERVER_PATH_BIG_RESPONSE);
939929
}
940930

941931
}

0 commit comments

Comments
 (0)