Skip to content

Commit

Permalink
Improve test reliability by checking the actual connections is same a…
Browse files Browse the repository at this point in the history
…s before test class instead of 0
  • Loading branch information
Tara Drwenski authored and haileyajohnson committed Jan 31, 2024
1 parent 1d4b9cf commit b9254d2
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,16 @@ protected Registry<ConnectionSocketFactory> getRegistry() {
return this.protocolregistry;
}

// For testing
void validate() {
assert actualconnections == 0;
}

// For testing
int getActualConnections() {
return actualconnections;
}

public abstract HttpClientConnectionManager newManager(HTTPMethod m);

public abstract void freeManager(HTTPMethod method);
Expand Down
8 changes: 6 additions & 2 deletions httpservices/src/main/java/ucar/httpservices/HTTPSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -1116,9 +1116,13 @@ public String getSessionURL() {
return getSessionURI();
}

// Obsolete
// make package private as only needed for testing
// For testing
static void validatestate() {
connmgr.validate();
}

// For testing
static int getActualConnections() {
return connmgr.getActualConnections();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

package ucar.httpservices;

import static com.google.common.truth.Truth.assertThat;

import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
Expand Down Expand Up @@ -75,6 +77,7 @@ public class TestThreading extends UnitTestCommon {
protected String[] testurls;

protected int nthreads = DFALTTHREADS;
private final int actualConnectionsBefore;

//////////////////////////////////////////////////

Expand All @@ -92,6 +95,7 @@ public TestThreading() {
}
}
definetests();
actualConnectionsBefore = HTTPSession.getActualConnections();
}

protected void definetests() {
Expand Down Expand Up @@ -152,7 +156,7 @@ public void testThreadingN() throws HTTPException, InterruptedException {
}
}
logger.debug("All threads terminated");
HTTPSession.validatestate();
assertThat(HTTPSession.getActualConnections()).isEqualTo(actualConnectionsBefore);
}

@Test
Expand All @@ -173,7 +177,7 @@ public void testThreading1() throws HTTPException {
}
}
logger.debug("All threads terminated");
HTTPSession.validatestate();
assertThat(HTTPSession.getActualConnections()).isEqualTo(actualConnectionsBefore);
}

static class Runner implements Runnable {
Expand Down
10 changes: 6 additions & 4 deletions httpservices/src/test/java/ucar/httpservices/TestUrlCreds.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import org.apache.http.auth.Credentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.junit.AfterClass;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
Expand All @@ -21,6 +21,7 @@ public class TestUrlCreds {
private String username;
private String password;
private String host;
private final int actualConnectionsBefore;

// Each parameter should be placed as an argument here
// Every time runner triggers, it will pass the arguments
Expand All @@ -30,6 +31,7 @@ public TestUrlCreds(String username, String password, String host) {
this.username = username;
this.password = password;
this.host = host;
actualConnectionsBefore = HTTPSession.getActualConnections();
}

@Parameterized.Parameters
Expand Down Expand Up @@ -78,8 +80,8 @@ public void testUrlCredDefaultProvider() throws HTTPException {
}
}

@AfterClass
public static void checkAllConnectionsClosed() {
HTTPSession.validatestate();
@After
public void checkAllConnectionsClosed() {
assertThat(HTTPSession.getActualConnections()).isEqualTo(actualConnectionsBefore);
}
}

0 comments on commit b9254d2

Please sign in to comment.