Skip to content

Commit 3dc5e5f

Browse files
committed
Some cleanup / unit test fixes
I forgot to re-enable the new unit test which required some uncommitted TestHTTPServer changes. The test is dependent on the TestHTTPServer not sending the response till the request has completed.
1 parent d3b0a56 commit 3dc5e5f

File tree

4 files changed

+35
-35
lines changed

4 files changed

+35
-35
lines changed

client/src/main/java/org/threadly/litesockets/client/http/HTTPClient.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import java.util.concurrent.ExecutionException;
1616
import java.util.concurrent.RejectedExecutionException;
1717
import java.util.concurrent.TimeUnit;
18-
import java.util.function.Consumer;
1918

2019
import javax.net.ssl.SSLContext;
2120
import javax.net.ssl.SSLEngine;
@@ -83,7 +82,7 @@ public class HTTPClient extends AbstractService {
8382

8483
/**
8584
* <p>This is the default constructor it will create its own {@link SingleThreadScheduler} to use as a threadpool, as
86-
* well as using the default {@value DEFAULT_CONCURRENT} and {@value MAX_HTTP_RESPONSE}. This means we will do at most
85+
* well as using the default {@value DEFAULT_CONCURRENT} and no queue limit. This means we will do at most
8786
* 2 HTTPRequests at the same time, and those responses can be up to 1mb in size.</p>
8887
*
8988
*/

client/src/test/java/org/threadly/litesockets/client/http/HTTPClientTests.java

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,8 @@
4545
import org.threadly.litesockets.utils.PortUtils;
4646
import org.threadly.test.concurrent.AsyncVerifier;
4747
import org.threadly.test.concurrent.TestCondition;
48-
import org.threadly.test.concurrent.TestUtils;
4948
import org.threadly.util.ArrayIterator;
5049
import org.threadly.util.Clock;
51-
import org.threadly.util.debug.Profiler;
5250

5351
public class HTTPClientTests {
5452
static String CONTENT = "TEST123";
@@ -107,13 +105,13 @@ public void manyRequestsConcurrent() throws IOException, InterruptedException, T
107105
@Override
108106
public void run() {
109107
ClientHTTPRequest chr = hrb.buildClientHTTPRequest();
110-
final long start = Clock.accurateForwardProgressingMillis();
108+
//final long start = Clock.accurateForwardProgressingMillis();
111109

112110
final ListenableFuture<HTTPResponseData> lf = httpClient.requestAsync(chr);
113111
lf.callback(new FutureCallback<HTTPResponseData>() {
114112
@Override
115113
public void handleResult(HTTPResponseData result) {
116-
System.out.println("DELAY:"+(Clock.accurateForwardProgressingMillis()-start));
114+
//System.out.println("DELAY:"+(Clock.accurateForwardProgressingMillis()-start));
117115
av.assertEquals(CONTENT, result.getBodyAsString());
118116
av.signalComplete();
119117
}
@@ -127,13 +125,6 @@ public void handleFailure(Throwable t) {
127125
for(int i=0; i<number; i++) {
128126
CLIENT_PS.execute(run);
129127
}
130-
131-
Profiler p = new Profiler();
132-
TestUtils.sleep(2_000);
133-
p.start();
134-
TestUtils.sleep(2_000);
135-
p.stop();
136-
System.out.println(p.dump());
137128

138129
av.waitForTest(10_000, number);
139130

@@ -234,7 +225,7 @@ public void handleFailure(Throwable t) {
234225
@Test
235226
public void blockingRequest() throws IOException, HTTPParsingException {
236227
int port = PortUtils.findTCPPort();
237-
fakeServer = new TestHTTPServer(port, RESPONSE_CL, CONTENT, false, true);
228+
fakeServer = new TestHTTPServer(port, RESPONSE_CL, CONTENT, false, true, true);
238229
final HTTPRequestBuilder hrb = new HTTPRequestBuilder(new URL("http://localhost:"+port));
239230
hrb.setHTTPAddress(new HTTPAddress("localhost", port, false), true);
240231
final HTTPClient httpClient = new HTTPClient();
@@ -257,7 +248,7 @@ public void noContentLengthNoBody() throws IOException, HTTPParsingException {
257248
@Test
258249
public void noContentLengthWithBody() throws IOException, HTTPParsingException {
259250
int port = PortUtils.findTCPPort();
260-
fakeServer = new TestHTTPServer(port, RESPONSE_NO_CL, CONTENT, false, true);
251+
fakeServer = new TestHTTPServer(port, RESPONSE_NO_CL, CONTENT, false, true, true);
261252
final HTTPRequestBuilder hrb = new HTTPRequestBuilder(new URL("http://localhost:"+port));
262253

263254
final HTTPClient httpClient = new HTTPClient();
@@ -267,10 +258,10 @@ public void noContentLengthWithBody() throws IOException, HTTPParsingException {
267258
assertEquals(CONTENT, hrs.getBodyAsString());
268259
}
269260

270-
//@Test
261+
@Test
271262
public void streamedBodyRequest() throws IOException, HTTPParsingException, InterruptedException, ExecutionException {
272263
int port = PortUtils.findTCPPort();
273-
fakeServer = new TestHTTPServer(port, RESPONSE_CL, CONTENT, false, true);
264+
fakeServer = new TestHTTPServer(port, RESPONSE_CL, CONTENT, false, true, true);
274265
ByteBuffer write1 = ByteBuffer.allocate(100);
275266
ByteBuffer write2 = ByteBuffer.allocate(100);
276267
SettableListenableFuture<ByteBuffer> write1SLF = new SettableListenableFuture<>();
@@ -298,7 +289,7 @@ public void streamedBodyRequest() throws IOException, HTTPParsingException, Inte
298289
@Test
299290
public void streamedBodyResponse() throws IOException, HTTPParsingException {
300291
int port = PortUtils.findTCPPort();
301-
fakeServer = new TestHTTPServer(port, RESPONSE_HUGE, LARGE_CONTENT, false, false);
292+
fakeServer = new TestHTTPServer(port, RESPONSE_HUGE, LARGE_CONTENT, false, false, false);
302293
AtomicInteger readContentSize = new AtomicInteger(0);
303294
final HTTPRequestBuilder hrb = new HTTPRequestBuilder(new URL("http://localhost:"+port))
304295
.setBodyConsumer(new BodyConsumer() {
@@ -349,8 +340,7 @@ public void closeBeforeLength() throws IOException, HTTPParsingException {
349340
httpClient.request(hrb.buildClientHTTPRequest());
350341
fail();
351342
} catch(HTTPParsingException e) {
352-
e.printStackTrace();
353-
assertEquals("Did not get complete body!", e.getMessage());
343+
assertTrue(e.getMessage().startsWith("Body not complete"));
354344
}
355345
}
356346

@@ -546,7 +536,7 @@ public void accept(Client c) {
546536
@Test
547537
public void urlRequest() throws HTTPParsingException, IOException {
548538
int port = PortUtils.findTCPPort();
549-
fakeServer = new TestHTTPServer(port, RESPONSE_CL, CONTENT, false, false);
539+
fakeServer = new TestHTTPServer(port, RESPONSE_CL, CONTENT, false, false, true);
550540
final HTTPClient httpClient = new HTTPClient();
551541
httpClient.start();
552542
HTTPResponseData hrd = httpClient.request(new URL("http://localhost:"+port));

client/src/test/java/org/threadly/litesockets/client/http/TestHTTPServer.java

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.threadly.litesockets.protocols.http.response.HTTPResponse;
2323
import org.threadly.litesockets.protocols.websocket.WSFrame;
2424
import org.threadly.litesockets.utils.SSLUtils.FullTrustManager;
25+
import org.threadly.util.ExceptionUtils;
2526

2627
public class TestHTTPServer {
2728
private final TrustManager[] myTMs = new TrustManager [] {new FullTrustManager() };
@@ -35,9 +36,15 @@ public class TestHTTPServer {
3536
private TCPServer server;
3637
private TransactionalByteBuffers sendBack = new TransactionalByteBuffers();
3738
private boolean closeOnSend;
39+
private boolean delayResponse;
3840
private HTTPResponse hr;
3941

4042
public TestHTTPServer(int port, HTTPResponse hr, String sendBack, boolean doSSL, boolean closeOnSend) throws IOException {
43+
this(port, hr, sendBack, doSSL, closeOnSend, false);
44+
}
45+
46+
public TestHTTPServer(int port, HTTPResponse hr, String sendBack, boolean doSSL,
47+
boolean closeOnSend, boolean delayResponse) throws IOException {
4148
if(doSSL) {
4249
doSSLCrap();
4350
}
@@ -53,6 +60,7 @@ public TestHTTPServer(int port, HTTPResponse hr, String sendBack, boolean doSSL,
5360
server.start();
5461
this.sendBack.add(ByteBuffer.wrap(sendBack.getBytes()));
5562
this.closeOnSend = closeOnSend;
63+
this.delayResponse = delayResponse;
5664
}
5765

5866
private void doSSLCrap() {
@@ -81,9 +89,7 @@ public void stop() {
8189
private void onClient(final Client c) {
8290
final HTTPRequestProcessor hrp = new HTTPRequestProcessor();
8391
hrp.addHTTPRequestCallback(new HTTPRequestCallback() {
84-
85-
@Override
86-
public void headersFinished(HTTPRequest hreq) {
92+
private void sendResponse() {
8793
ListenableFuture<?> lf = c.write(hr.getMergedByteBuffers());
8894
sendBack.begin();
8995

@@ -94,31 +100,35 @@ public void headersFinished(HTTPRequest hreq) {
94100
if(closeOnSend) {
95101
lf.listener(c::close);
96102
}
97-
hrp.reset();
103+
}
104+
105+
@Override
106+
public void headersFinished(HTTPRequest hreq) {
107+
if (! delayResponse) {
108+
sendResponse();
109+
}
98110
}
99111

100112
@Override
101113
public void bodyData(ByteBuffer bb) {
102-
// TODO Auto-generated method stub
103-
114+
// ignored
104115
}
105116

106117
@Override
107118
public void websocketData(WSFrame wsf, ByteBuffer bb) {
108-
// TODO Auto-generated method stub
109-
119+
// ignored
110120
}
111121

112122
@Override
113123
public void finished() {
114-
// TODO Auto-generated method stub
115-
124+
if (delayResponse) {
125+
sendResponse();
126+
}
116127
}
117128

118129
@Override
119130
public void hasError(Throwable t) {
120-
// TODO Auto-generated method stub
121-
131+
ExceptionUtils.handleException(t);
122132
}});
123133
clients.putIfAbsent(c, hrp);
124134
c.setReader((lc)->hrp.processData(lc.getRead()));

protocol/src/main/java/org/threadly/litesockets/protocols/http/response/HTTPResponseProcessor.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,9 @@ public void connectionClosed() {
138138
reset(new HTTPParsingException("Did not complete chunked encoding!"));
139139
}
140140
} else {
141-
if(response.getHeaders().getContentLength() > 0 && response.getHeaders().getContentLength() != this.currentBodySize) {
142-
reset(new HTTPParsingException("Did not get complete body!"));
141+
long contentLength = response.getHeaders().getContentLength();
142+
if(contentLength > 0 && contentLength != this.currentBodySize) {
143+
reset(new HTTPParsingException("Body not completed! (" + currentBodySize + "/" + contentLength + ")"));
143144
} else {
144145
reset(null);
145146
}

0 commit comments

Comments
 (0)