Skip to content

Commit

Permalink
improve test for two different edrs.
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelmag110 committed Oct 17, 2024
1 parent ce178d2 commit 18f5963
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@

public class InMemoryEdrLock implements EndpointDataReferenceLock {

private static final int LOCK_TIMEOUT = 5000;

private final EndpointDataReferenceEntryIndex entryIndex;
private final TransactionContext transactionContext;
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
Expand All @@ -48,7 +50,6 @@ public InMemoryEdrLock(EndpointDataReferenceEntryIndex entryIndex, TransactionCo

@Override
public StoreResult<Boolean> acquireLock(String edrId, DataAddress edr) {

lock.readLock().lock();
try {
if (!lockedEdrs.contains(edrId)) {
Expand All @@ -67,10 +68,10 @@ public StoreResult<Boolean> acquireLock(String edrId, DataAddress edr) {
}

var timeout = 0;
while (lockedEdrs.contains(edrId) && timeout <= 15000) {
//block until updated
while (lockedEdrs.contains(edrId) && timeout <= LOCK_TIMEOUT) {
//block until row updated
try {
sleep(1000);
sleep(LOCK_TIMEOUT);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ public StoreResult<Boolean> acquireLock(String edrId, DataAddress edr) {
// this blocks until Postgres can acquire the row-level lock
var edrEntry = queryExecutor.single(connection, false, this::mapEdr, sql, edrId);

// check again, to abort
if (!isExpired(edr, edrEntry)) {
return StoreResult.success(false);
}
Expand Down Expand Up @@ -81,7 +80,7 @@ public boolean isExpired(DataAddress edr, EndpointDataReferenceEntry metadata) {

@Override
public void releaseLock(String edrId) {
return;
// do nothing since the lock is implicitly released by the row update.
}

private EndpointDataReferenceEntry mapEdr(ResultSet resultSet) throws SQLException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nimbusds.jose.JOSEException;
import com.nimbusds.jose.JWSAlgorithm;
Expand Down Expand Up @@ -62,11 +61,9 @@
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.IntStream;

import static java.lang.String.format;
import static org.assertj.core.api.Assertions.assertThat;
import static org.eclipse.edc.spi.constants.CoreConstants.EDC_NAMESPACE;
import static org.eclipse.edc.util.io.Ports.getFreePort;
import static org.eclipse.tractusx.edc.edr.spi.CoreConstants.EDR_PROPERTY_AUTHORIZATION;
import static org.eclipse.tractusx.edc.edr.spi.CoreConstants.EDR_PROPERTY_EXPIRES_IN;
import static org.eclipse.tractusx.edc.edr.spi.CoreConstants.EDR_PROPERTY_REFRESH_AUDIENCE;
import static org.eclipse.tractusx.edc.edr.spi.CoreConstants.EDR_PROPERTY_REFRESH_ENDPOINT;
Expand Down Expand Up @@ -102,8 +99,8 @@ protected static Map<String, String> with(Map<String, String> original, Map<Stri


abstract static class Tests {
private final ObjectMapper mapper = new ObjectMapper();
private final Random random = new Random();
private final ObjectMapper mapper = new ObjectMapper();
private ClientAndServer mockedRefreshApi;
private ECKey providerSigningKey;
private String refreshEndpoint;
Expand Down Expand Up @@ -156,9 +153,9 @@ void getEdrWithRefresh_success() {
}
}

@DisplayName("Verify HTTP 200 response and body when refreshing succeeds")
@DisplayName("When multiple requests to refresh, to different edrs, verify all return non expired token")
@Test
void getEdrWithRefresh_subsequentRequestFails() throws InterruptedException {
void getEdrWithRefresh_subsequentRequestReturn() throws InterruptedException {

try (var client = new MockServerClient("localhost", mockedRefreshApi.getPort())) {
var claims = new JWTClaimsSet.Builder().claim("iss", "did:web:provider").build();
Expand All @@ -169,7 +166,8 @@ void getEdrWithRefresh_subsequentRequestFails() throws InterruptedException {
client.when(request().withMethod("POST").withPath("/refresh/token").withBody(exact("")))
.respond(response().withStatusCode(200).withDelay(Delay.milliseconds(5000)).withBody(tokenResponseBodyString));

storeEdr("test-id", true);
storeEdr("test-id-1", true);
storeEdr("test-id-2", true);
var numThreads = 50;
var jitter = 100; // maximum time between threads are spawned
var latch = new CountDownLatch(50);
Expand All @@ -183,29 +181,34 @@ void getEdrWithRefresh_subsequentRequestFails() throws InterruptedException {
try {
Thread.sleep(wait);
new Thread(() -> {
var tr = CONSUMER.edrs().getEdrWithRefresh("test-id", true)
.assertThat()
.statusCode(200)
.extract().asString();
latch.countDown();
assertThat(tr).contains(accessToken);
var edrNumber = random.nextInt(1, 3);
try {
var tr = CONSUMER.edrs().getEdrWithRefresh("test-id-%s".formatted(edrNumber), true)
.assertThat()
.statusCode(200)
.extract().asString();

assertThat(tr).contains(accessToken);
} catch (AssertionError e) {
failed.set(true);
} finally {
latch.countDown();
}


}).start();
} catch (InterruptedException e) {
failed.set(true);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
});


// assert the correct endpoint was called
latch.await();
assertThat(failed.get()).isFalse();

client.verify(request()
.withQueryStringParameter("grant_type", "refresh_token")
.withMethod("POST")
.withPath("/refresh/token"), VerificationTimes.exactly(1));
.withPath("/refresh/token"), VerificationTimes.exactly(2));

}
}
Expand Down Expand Up @@ -382,7 +385,6 @@ private void storeEdr(String transferProcessId, boolean isExpired) {
.property(EDR_PROPERTY_REFRESH_ENDPOINT, refreshEndpoint)
.property(EDR_PROPERTY_REFRESH_AUDIENCE, refreshAudience)
.build();
System.out.println(edr.getStringProperty(EDC_NAMESPACE + "authorization"));
var entry = EndpointDataReferenceEntry.Builder.newInstance()
.clock(isExpired ? // defaults to an expired token
Clock.fixed(Instant.now().minusSeconds(3600), ZoneId.systemDefault()) :
Expand Down

0 comments on commit 18f5963

Please sign in to comment.