Skip to content

Commit

Permalink
Add request time to output (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
krujos authored Oct 14, 2016
1 parent c931d66 commit a8010f5
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
11 changes: 11 additions & 0 deletions src/main/java/willitconnect/model/CheckedEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
* Holds a hostname or url that is checked to see if we can connect to it.
*/
public class CheckedEntry {
public static long DEFAULT_RESPONSE_TIME = -1L;
private Date lastChecked;
private String entry;
private boolean canConnect;
private int httpStatus;
private String httpProxy;
private long responseTime = DEFAULT_RESPONSE_TIME;

public boolean isCanConnect() {
return canConnect();
Expand Down Expand Up @@ -88,4 +90,13 @@ public void setHttpProxy(String proxy) {
public String getHttpProxy() {
return httpProxy;
}


public long getResponseTime() {
return responseTime;
}

public void setResponseTime(long responseTime) {
this.responseTime = responseTime;
}
}
3 changes: 3 additions & 0 deletions src/main/java/willitconnect/service/EntryChecker.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package willitconnect.service;

import org.apache.log4j.Logger;
import org.apache.tomcat.jni.Time;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.ClientHttpResponse;
Expand Down Expand Up @@ -45,13 +46,15 @@ public EntryChecker(RestTemplate restTemplate) {

public CheckedEntry check(CheckedEntry e) {
log.info("checking " + e.getEntry());
long startTime = Instant.now().toEpochMilli();
if (e.isValidUrl()) {
checkUrl(e);
} else if (e.isValidHostname()) {
checkHostname(e);
} else {
log.error(e.getEntry() + " is not a valid hostname");
}
e.setResponseTime(Instant.now().toEpochMilli() - startTime);
return e;
}

Expand Down
4 changes: 3 additions & 1 deletion src/main/script/Entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,10 @@ const Entry = ({
success,
httpStatus,
time,
responseTime,
}) =>
<ul>
<li> On {time}, I {success ? 'could' : 'could not'} connect.
<li> On {time}, I {success ? 'could' : 'could not'} connect. It took {responseTime} ms.
{httpStatus !== 0 && <span>Http Status: {httpStatus}</span>}
</li>
</ul>;
Expand Down Expand Up @@ -97,6 +98,7 @@ const StatelessEntry = (props) => {
success={attempt.canConnect}
httpStatus={attempt.httpStatus}
time={(new Date(attempt.time)).toLocaleString()}
responseTime={attempt.responseTime}
/>
);
}).reverse()}
Expand Down
10 changes: 9 additions & 1 deletion src/test/java/willitconnect/service/EntryCheckerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.time.Instant;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.core.Is.is;
Expand All @@ -24,6 +25,7 @@
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
import static org.springframework.test.web.client.response.MockRestResponseCreators.withServerError;
import static org.testng.AssertJUnit.assertEquals;
import static willitconnect.model.CheckedEntry.DEFAULT_RESPONSE_TIME;

public class EntryCheckerTest {

Expand Down Expand Up @@ -83,8 +85,14 @@ public void itShouldConnectToAURLThroughAProxy() {
entry.setHttpProxy("proxy.example.com:8080");
CheckedEntry returnedEntry = checker.check(entry);


assertThat(returnedEntry.getHttpProxy(),
is(equalTo("proxy.example.com:8080")));
}

@Test
public void itShouldSayHowLongItTookToConnect() {
CheckedEntry returnedEntry = checker.check(entry);

assertThat(returnedEntry.getResponseTime(), is(not(equalTo(DEFAULT_RESPONSE_TIME))));
}
}

0 comments on commit a8010f5

Please sign in to comment.