-
Notifications
You must be signed in to change notification settings - Fork 384
Add signal/wait model for TestAuditlogImpl #1758
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,9 @@ | |
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.concurrent.CountDownLatch; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
|
|
||
| import org.opensearch.common.settings.Settings; | ||
|
|
||
|
|
@@ -25,23 +28,60 @@ | |
|
|
||
| public class TestAuditlogImpl extends AuditLogSink { | ||
|
|
||
| /** Use the results of `doThenWaitForMessages(...)` instead */ | ||
| @Deprecated | ||
| public static List<AuditMessage> messages = new ArrayList<AuditMessage>(100); | ||
| /** Check messages indvidually instead of searching this string */ | ||
| @Deprecated | ||
| public static StringBuffer sb = new StringBuffer(); | ||
| private static final AtomicReference<CountDownLatch> countDownRef = new AtomicReference<>(); | ||
| private static final AtomicReference<List<AuditMessage>> messagesRef = new AtomicReference<>(); | ||
|
|
||
| public TestAuditlogImpl(String name, Settings settings, String settingsPrefix, AuditLogSink fallbackSink) { | ||
| super(name, settings, null, fallbackSink); | ||
| } | ||
|
|
||
|
|
||
| public synchronized boolean doStore(AuditMessage msg) { | ||
| public boolean doStore(AuditMessage msg) { | ||
| if (messagesRef.get() == null || countDownRef.get() == null) { | ||
| throw new RuntimeException("No message latch is waiting"); | ||
| } | ||
| sb.append(msg.toPrettyString()+System.lineSeparator()); | ||
| messages.add(msg); | ||
| messagesRef.get().add(msg); | ||
| countDownRef.get().countDown(); | ||
| return true; | ||
| } | ||
|
|
||
| /** Unneeded after switching to `doThenWaitForMessages(...)` as data is automatically flushed */ | ||
| @Deprecated | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there any reason for keeping 2 different methods for the exactly same purpose? If not, replacing the old method with the new method would retain consistency.(Same question as the one above.)
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The testRestMethod has been the source of trouble, lets incrementally invest in the space. If none of these other tests need to be updated, it will save us time/trouble. Each assert that was updated required investigation to confirm the correct behavior. With the api deprecation, this will help ensure new tests use the more reliable technique. |
||
| public static synchronized void clear() { | ||
| sb.setLength(0); | ||
| messages.clear(); | ||
| doThenWaitForMessages(() -> {}, 0); | ||
| } | ||
|
|
||
| /** | ||
| * Perform an action and then wait until the expected number of messages have been found. | ||
| */ | ||
| public static List<AuditMessage> doThenWaitForMessages(final Runnable action, final int expectedCount) { | ||
| final CountDownLatch latch = new CountDownLatch(expectedCount); | ||
| final List<AuditMessage> messages = new ArrayList<>(); | ||
| countDownRef.set(latch); | ||
| messagesRef.set(messages); | ||
|
|
||
| TestAuditlogImpl.sb = new StringBuffer(); | ||
| TestAuditlogImpl.messages = messages; | ||
|
|
||
| try { | ||
| action.run(); | ||
| final boolean foundAll = latch.await(1, TimeUnit.SECONDS); | ||
|
peternied marked this conversation as resolved.
Outdated
|
||
| if (!foundAll) { | ||
| throw new RuntimeException("Did not recieve all " + expectedCount +" audit messages after a short wait."); | ||
| } | ||
| if (messages.size() != expectedCount) { | ||
| throw new RuntimeException("Unexpected number of messages, was expecting " + expectedCount + ", recieved " + messages.size()); | ||
| } | ||
| } catch (final InterruptedException e) { | ||
| throw new RuntimeException("Unexpected exception", e); | ||
| } | ||
| return new ArrayList<>(messages); | ||
| } | ||
|
|
||
| @Override | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.