Skip to content
Open
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 @@ -22,10 +22,9 @@
import static org.mockito.Mockito.*;

import java.text.MessageFormat;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.LockSupport;

import jakarta.inject.Provider;

Expand Down Expand Up @@ -148,18 +147,18 @@ public void run() {

private static class LongTask implements Task {

private AtomicBoolean locked = new AtomicBoolean(false);
private Thread executingThread;
private final CountDownLatch latch = new CountDownLatch(1);

@Override
public void execute(TokenStorageAdapter adapter) throws DataLayerException {
this.executingThread = Thread.currentThread();
debug("Locking");
locked.set(true);
while (!locked.compareAndSet(false, true)) {
debug("Task still locked - parking thread");
LockSupport.park(this);
debug("Thread unparked");
try {
if (!latch.await(30, TimeUnit.SECONDS)) {
fail("LongTask was not unblocked within 30 seconds; test is likely hung.");
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new DataLayerException("Task was interrupted while waiting on latch", e);
}
debug("Thread unlocked - continuing");
}
Expand All @@ -169,10 +168,7 @@ public void processError(DataLayerException error) {}

public void unblock() {
debug("Setting task unlocked");
locked.set(false);
debug("Unparking thread {0}", executingThread);
LockSupport.unpark(executingThread);
debug("Unparked thread {0}", executingThread);
latch.countDown();
}

}
Expand Down
Loading