1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-12-24 11:38:00 +01:00

[sinttest] Add AssertResult for MultiResultSyncPoint

This adds an AssertResult implementation for MultiResultSyncPoint that mimics the equivalent for ResultSyncPoint.
This commit is contained in:
Guus der Kinderen 2024-06-13 15:31:18 +02:00
parent 4f09244253
commit 440b497638
2 changed files with 22 additions and 2 deletions

View file

@ -20,6 +20,7 @@ import java.io.File;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeoutException;
import java.util.logging.Logger;
@ -33,6 +34,7 @@ import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
import org.jivesoftware.smack.filter.StanzaFilter;
import org.igniterealtime.smack.inttest.util.MultiResultSyncPoint;
import org.igniterealtime.smack.inttest.util.ResultSyncPoint;
import org.opentest4j.AssertionFailedError;
@ -108,4 +110,18 @@ public abstract class AbstractSmackIntTest {
throw new AssertionFailedError(message, e);
}
}
public <R> List<R> assertResult(MultiResultSyncPoint<R, ?> syncPoint, String message) throws InterruptedException, TimeoutException, AssertionFailedError {
return assertResult(syncPoint, timeout, message);
}
public static <R> List<R> assertResult(MultiResultSyncPoint<R, ?> syncPoint, long timeout, String message) throws InterruptedException, TimeoutException, AssertionFailedError {
try {
return syncPoint.waitForResults(timeout, message);
} catch (InterruptedException | TimeoutException e) {
throw e;
} catch (Exception e) {
throw new AssertionFailedError(message, e);
}
}
}

View file

@ -1,6 +1,6 @@
/**
*
* Copyright 2021 Guus der Kinderen
* Copyright 2021-2024 Guus der Kinderen
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -34,13 +34,17 @@ public class MultiResultSyncPoint<R, E extends Exception> {
}
public synchronized List<R> waitForResults(long timeout) throws E, InterruptedException, TimeoutException {
return waitForResults(timeout, null);
}
public synchronized List<R> waitForResults(long timeout, String timeoutMessage) throws E, InterruptedException, TimeoutException {
long now = System.currentTimeMillis();
final long deadline = now + timeout;
while (results.size() < expectedResultCount && exception == null && now < deadline) {
wait(deadline - now);
now = System.currentTimeMillis();
}
if (now >= deadline) throw new TimeoutException("MultiResultSyncPoint timeout waiting " + timeout + " millis. Got " + results.size() + " results of " + expectedResultCount + " results");
if (now >= deadline) throw new TimeoutException((timeoutMessage != null ? timeoutMessage + " " : "") + "MultiResultSyncPoint timeout waiting " + timeout + " millis. Got " + results.size() + " results of " + expectedResultCount + " results");
if (exception != null) throw exception;
return new ArrayList<>(results);
}