Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve test reliability #1298

Merged
merged 1 commit into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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);
}
}
Loading