Skip to content

Commit

Permalink
Merge pull request #599 from guusdk/sint_assertresult-multisync
Browse files Browse the repository at this point in the history
[sinttest] Improving assertions for MultiResultSyncPoint
  • Loading branch information
Flowdalic authored Jun 27, 2024
2 parents 98ff4d8 + 7e9a571 commit 68fa904
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
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;
Expand All @@ -33,6 +34,7 @@
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;
Expand Down Expand Up @@ -108,4 +110,18 @@ public static <R> R assertResult(ResultSyncPoint<R, ?> syncPoint, long timeout,
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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -34,13 +34,17 @@ public MultiResultSyncPoint(int expectedResultCount) {
}

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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,10 +292,10 @@ public void mucJoinPresenceBroadcastTest() throws Exception {
try {
mucAsSeenByThree.join(nicknameThree);

List<Presence> results = syncPoint.waitForResults(timeout);
List<Presence> results = assertResult(syncPoint, "Expected all occupants of room '" + mucAddress + "' to be notified of '" + conThree.getUser() + "' using nickname '" + nicknameThree + "' joining the room (but one or more did not get notified).");
assertTrue(results.stream().allMatch(
result -> JidCreate.fullFrom(mucAddress, nicknameThree).equals(result.getFrom())),
"Expected all occupants of room '" + mucAddress + "' to be notified of '" + conThree.getUser() + "' using nickname '" + nicknameThree + "' joining the room (but one or more got notified ).");
"Expected all occupants of room '" + mucAddress + "' to be notified of '" + conThree.getUser() + "' using nickname '" + nicknameThree + "' joining the room (but one or more got notified for a different user).");
assertTrue(results.stream().anyMatch(
result -> result.getTo().equals(conOne.getUser().asEntityFullJidIfPossible())),
"Expected '" + conOne.getUser().asEntityFullJidIfPossible() + "' to be notified of '" + conThree.getUser() + "' joining room '" + mucAddress + "' (but did not)");
Expand Down

0 comments on commit 68fa904

Please sign in to comment.