forked from igniterealtime/Smack
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[sinttest] Additional tests for s7 of XEP-0045
- Loading branch information
1 parent
2e94599
commit 212dd41
Showing
8 changed files
with
1,397 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
...ration-test/src/main/java/org/igniterealtime/smack/inttest/util/MultiResultSyncPoint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/** | ||
* | ||
* Copyright 2021 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.igniterealtime.smack.inttest.util; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.TimeoutException; | ||
|
||
import org.jivesoftware.smack.util.Objects; | ||
|
||
public class MultiResultSyncPoint<R, E extends Exception> { | ||
|
||
private final List<R> results; | ||
private E exception; | ||
private final int expectedResultCount; | ||
|
||
public MultiResultSyncPoint(int expectedResultCount) { | ||
this.expectedResultCount = expectedResultCount; | ||
this.results = new ArrayList<>(expectedResultCount); | ||
} | ||
|
||
public synchronized List<R> waitForResults(long timeout) 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("Timeout waiting " + timeout + " millis"); | ||
if (exception != null) throw exception; | ||
return new ArrayList<>(results); | ||
} | ||
|
||
public void signal(R result) { | ||
synchronized (this) { | ||
this.results.add(Objects.requireNonNull(result)); | ||
if (expectedResultCount <= results.size()) { | ||
notifyAll(); | ||
} | ||
} | ||
} | ||
|
||
public void signal(E exception) { | ||
synchronized (this) { | ||
this.exception = Objects.requireNonNull(exception); | ||
notifyAll(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.