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

fix: re-use existing connection info during force refresh #180

Merged
merged 1 commit into from
Sep 18, 2023
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 @@ -187,8 +187,7 @@ public void forceRefresh() {
"[%s] Force Refresh: the next refresh operation was cancelled."
+ " Scheduling new refresh operation immediately.",
instanceName));
current = executor.submit(this::performRefresh);
next = current;
next = executor.submit(this::performRefresh);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

public class ConnectionInfoCacheTest {

private static final int DEFAULT_WAIT = 100;
private static final String TEST_INSTANCE_IP = "10.0.0.1";
private static final String TEST_INSTANCE_ID = "some-instance-id";
private static final Instant ONE_HOUR_FROM_NOW = Instant.now().plus(1, ChronoUnit.HOURS);
Expand Down Expand Up @@ -288,7 +289,7 @@ public void testGetConnectionInfo_isRateLimited() {
}

@Test
public void testForceRefresh_schedulesNextRefreshImmediately() {
public void testForceRefresh_schedulesNextRefreshImmediately() throws InterruptedException {
ScheduledExecutorService executor = Executors.newScheduledThreadPool(2);

InMemoryConnectionInfoRepo connectionInfoRepo = new InMemoryConnectionInfoRepo();
Expand Down Expand Up @@ -329,8 +330,20 @@ public void testForceRefresh_schedulesNextRefreshImmediately() {

connectionInfoCache.forceRefresh();

// After the force refresh, new refresh data is available.
// Refresh data hasn't changed because we re-use the existing connection info.
connectionInfo = connectionInfoCache.getConnectionInfo();
assertThat(connectionInfoRepo.getIndex()).isEqualTo(1);

for (int i = 0; i < 10; i++) {
connectionInfo = connectionInfoCache.getConnectionInfo();
if (connectionInfoRepo.getIndex() > 1) {
break;
}
Thread.sleep(DEFAULT_WAIT);
}

// After a few seconds, new refresh data should be available.
assertThat(connectionInfoRepo.getIndex()).isEqualTo(2);
assertThat(
connectionInfo
.getClientCertificate()
Expand All @@ -341,7 +354,8 @@ public void testForceRefresh_schedulesNextRefreshImmediately() {
}

@Test
public void testForceRefresh_refreshCalledOnlyOnceDuringMultipleCalls() {
public void testForceRefresh_refreshCalledOnlyOnceDuringMultipleCalls()
throws InterruptedException {
ScheduledExecutorService executor = Executors.newScheduledThreadPool(2);

InMemoryConnectionInfoRepo connectionInfoRepo = new InMemoryConnectionInfoRepo();
Expand Down Expand Up @@ -389,9 +403,18 @@ public void testForceRefresh_refreshCalledOnlyOnceDuringMultipleCalls() {
connectionInfoCache.forceRefresh();
// This second call should be ignored as there is a refresh operation in progress.
connectionInfoCache.forceRefresh();
assertThat(connectionInfoRepo.getIndex()).isEqualTo(1);

for (int i = 0; i < 10; i++) {
connectionInfo = connectionInfoCache.getConnectionInfo();
if (connectionInfoRepo.getIndex() > 1) {
break;
}
Thread.sleep(DEFAULT_WAIT);
}

// After the force refresh, new refresh data is available.
connectionInfo = connectionInfoCache.getConnectionInfo();
// After a few seconds, new refresh data should be available.
assertThat(connectionInfoRepo.getIndex()).isEqualTo(2);
assertThat(
connectionInfo
.getClientCertificate()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,8 @@ public ConnectionInfo getConnectionInfo(InstanceName instanceName, KeyPair publi
public final void addResponses(Callable<ConnectionInfo>... callables) {
registeredCallables.addAll(Arrays.asList(callables));
}

public final int getIndex() {
return this.index.get();
}
}