From 440b49763829237fbb966fd723cd71fcf81730ce Mon Sep 17 00:00:00 2001 From: Guus der Kinderen Date: Thu, 13 Jun 2024 15:31:18 +0200 Subject: [PATCH 1/2] [sinttest] Add AssertResult for MultiResultSyncPoint This adds an AssertResult implementation for MultiResultSyncPoint that mimics the equivalent for ResultSyncPoint. --- .../smack/inttest/AbstractSmackIntTest.java | 16 ++++++++++++++++ .../smack/inttest/util/MultiResultSyncPoint.java | 8 ++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/smack-integration-test/src/main/java/org/igniterealtime/smack/inttest/AbstractSmackIntTest.java b/smack-integration-test/src/main/java/org/igniterealtime/smack/inttest/AbstractSmackIntTest.java index 11334dee8..9f94fe26b 100644 --- a/smack-integration-test/src/main/java/org/igniterealtime/smack/inttest/AbstractSmackIntTest.java +++ b/smack-integration-test/src/main/java/org/igniterealtime/smack/inttest/AbstractSmackIntTest.java @@ -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 List assertResult(MultiResultSyncPoint syncPoint, String message) throws InterruptedException, TimeoutException, AssertionFailedError { + return assertResult(syncPoint, timeout, message); + } + + public static List assertResult(MultiResultSyncPoint 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); + } + } } diff --git a/smack-integration-test/src/main/java/org/igniterealtime/smack/inttest/util/MultiResultSyncPoint.java b/smack-integration-test/src/main/java/org/igniterealtime/smack/inttest/util/MultiResultSyncPoint.java index d04de77c1..da220ff69 100644 --- a/smack-integration-test/src/main/java/org/igniterealtime/smack/inttest/util/MultiResultSyncPoint.java +++ b/smack-integration-test/src/main/java/org/igniterealtime/smack/inttest/util/MultiResultSyncPoint.java @@ -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 { } public synchronized List waitForResults(long timeout) throws E, InterruptedException, TimeoutException { + return waitForResults(timeout, null); + } + + public synchronized List 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); } From 7e9a5713e96812dc64ba2555fdf79646bb15f707 Mon Sep 17 00:00:00 2001 From: Guus der Kinderen Date: Thu, 13 Jun 2024 15:32:23 +0200 Subject: [PATCH 2/2] [sinttest] Improve test assertion message Making use of the new assertion handling for MultiResultSyncPoint, the integration test that uses that implementation can now get improved assertion messages. This will allow users to more quickly determine why a test is failing. --- .../smackx/muc/MultiUserChatOccupantIntegrationTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/smack-integration-test/src/main/java/org/jivesoftware/smackx/muc/MultiUserChatOccupantIntegrationTest.java b/smack-integration-test/src/main/java/org/jivesoftware/smackx/muc/MultiUserChatOccupantIntegrationTest.java index 4caad88d9..3be33ae6d 100644 --- a/smack-integration-test/src/main/java/org/jivesoftware/smackx/muc/MultiUserChatOccupantIntegrationTest.java +++ b/smack-integration-test/src/main/java/org/jivesoftware/smackx/muc/MultiUserChatOccupantIntegrationTest.java @@ -293,10 +293,10 @@ public class MultiUserChatOccupantIntegrationTest extends AbstractMultiUserChatI try { mucAsSeenByThree.join(nicknameThree); - List results = syncPoint.waitForResults(timeout); + List 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)");