• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

igniterealtime / Smack / #3201

27 Jun 2024 02:42PM UTC coverage: 38.942% (-0.002%) from 38.944%
#3201

push

github

web-flow
Merge pull request #599 from guusdk/sint_assertresult-multisync

[sinttest] Improving assertions for MultiResultSyncPoint

2 of 10 new or added lines in 3 files covered. (20.0%)

2 existing lines in 1 file now uncovered.

16967 of 43570 relevant lines covered (38.94%)

0.39 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

17.95
/smack-integration-test/src/main/java/org/igniterealtime/smack/inttest/AbstractSmackIntTest.java
1
/**
2
 *
3
 * Copyright 2015-2024 Florian Schmaus
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 *     http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
package org.igniterealtime.smack.inttest;
18

19
import java.io.File;
20
import java.io.IOException;
21
import java.net.HttpURLConnection;
22
import java.net.URL;
23
import java.util.List;
24
import java.util.Random;
25
import java.util.concurrent.TimeoutException;
26
import java.util.logging.Logger;
27

28
import javax.net.ssl.HttpsURLConnection;
29

30
import org.jivesoftware.smack.SmackException.NoResponseException;
31
import org.jivesoftware.smack.SmackException.NotConnectedException;
32
import org.jivesoftware.smack.StanzaCollector;
33
import org.jivesoftware.smack.XMPPConnection;
34
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
35
import org.jivesoftware.smack.filter.StanzaFilter;
36

37
import org.igniterealtime.smack.inttest.util.MultiResultSyncPoint;
38
import org.igniterealtime.smack.inttest.util.ResultSyncPoint;
39

40
import org.opentest4j.AssertionFailedError;
41

42
public abstract class AbstractSmackIntTest {
43

44
    protected static final Logger LOGGER = Logger.getLogger(AbstractSmackIntTest.class.getName());
1✔
45

46
    protected static final Random INSECURE_RANDOM = new Random();
1✔
47

48
    protected final String testRunId;
49

50
    protected final long timeout;
51

52
    protected final Configuration sinttestConfiguration;
53

54
    protected AbstractSmackIntTest(SmackIntegrationTestEnvironment environment) {
1✔
55
        this.testRunId = environment.testRunId;
1✔
56
        this.sinttestConfiguration = environment.configuration;
1✔
57
        this.timeout = environment.configuration.replyTimeout;
1✔
58
    }
1✔
59

60
    protected void performActionAndWaitUntilStanzaReceived(Runnable action, XMPPConnection connection, StanzaFilter filter)
61
                    throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
62
        StanzaCollector.Configuration configuration = StanzaCollector.newConfiguration().setStanzaFilter(
×
63
                        filter).setSize(1);
×
64
        try (StanzaCollector collector = connection.createStanzaCollector(configuration)) {
×
65
            action.run();
×
66
            collector.nextResultOrThrow(timeout);
×
67
        }
68
    }
×
69

70
    protected void waitUntilTrue(Condition condition) throws TimeoutException, NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
71
        final long deadline = System.currentTimeMillis() + timeout;
×
72
        do {
73
            if (condition.evaluate()) {
×
74
                return;
×
75
            }
76
            Thread.sleep(15);
×
77
        } while (System.currentTimeMillis() <= deadline);
×
78
        throw new TimeoutException("Timeout waiting for condition to become true. Timeout was " + timeout + " ms.");
×
79
    }
80

81
    protected interface Condition {
82
        boolean evaluate() throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException;
83
    }
84

85
    protected File createNewTempFile() throws IOException {
86
        File file = File.createTempFile("smack-integration-test-" + testRunId + "-temp-file", null);
×
87
        file.deleteOnExit();
×
88
        return file;
×
89
    }
90

91
    protected HttpURLConnection getHttpUrlConnectionFor(URL url) throws IOException {
92
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
×
93
        if (sinttestConfiguration.sslContextFactory != null && urlConnection instanceof HttpsURLConnection) {
×
94
            HttpsURLConnection httpsUrlConnection = (HttpsURLConnection) urlConnection;
×
95
            httpsUrlConnection.setSSLSocketFactory(sinttestConfiguration.sslContextFactory.createSslContext().getSocketFactory());
×
96
        }
97
        return urlConnection;
×
98
    }
99

100
    public <R> R assertResult(ResultSyncPoint<R, ?> syncPoint, String message) throws InterruptedException, TimeoutException, AssertionFailedError {
101
        return assertResult(syncPoint, timeout, message);
×
102
    }
103

104
    public static <R> R assertResult(ResultSyncPoint<R, ?> syncPoint, long timeout, String message) throws InterruptedException, TimeoutException, AssertionFailedError {
105
        try {
106
            return syncPoint.waitForResult(timeout, message);
×
107
        } catch (InterruptedException | TimeoutException e) {
×
108
            throw e;
×
109
        } catch (Exception e) {
×
110
            throw new AssertionFailedError(message, e);
×
111
        }
112
    }
113

114
    public <R> List<R> assertResult(MultiResultSyncPoint<R, ?> syncPoint, String message) throws InterruptedException, TimeoutException, AssertionFailedError {
NEW
115
        return assertResult(syncPoint, timeout, message);
×
116
    }
117

118
    public static <R> List<R> assertResult(MultiResultSyncPoint<R, ?> syncPoint, long timeout, String message) throws InterruptedException, TimeoutException, AssertionFailedError {
119
        try {
NEW
120
            return syncPoint.waitForResults(timeout, message);
×
NEW
121
        } catch (InterruptedException | TimeoutException e) {
×
NEW
122
            throw e;
×
NEW
123
        } catch (Exception e) {
×
NEW
124
            throw new AssertionFailedError(message, e);
×
125
        }
126
    }
127
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc