-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
much better approach for general async java handling #1681
theres no need to overload karate with async, the scenarios can be wildly different but the patterns will be similar - and thats the example in this commit since you can pass params and even functions from a karate test the possibilities are many - and waiting for multiple messages is demonstrated especially how you can wait until a user-defined condition
- Loading branch information
Showing
5 changed files
with
55 additions
and
63 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,79 @@ | ||
package mock.async; | ||
|
||
import com.intuit.karate.EventContext; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.function.Predicate; | ||
import javax.jms.Connection; | ||
import javax.jms.Destination; | ||
import javax.jms.MessageConsumer; | ||
import javax.jms.MessageListener; | ||
import javax.jms.Session; | ||
import javax.jms.TextMessage; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
|
||
public class QueueConsumer { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(QueueConsumer.class); | ||
|
||
public static final String QUEUE_NAME = "MOCK.ASYNC"; | ||
|
||
private final Connection connection; | ||
private final MessageConsumer consumer; | ||
private final Session session; | ||
|
||
// in more complex tests or for re-usability, this field and append() / | ||
// collect() / clear() methods can be in a separate / static class | ||
private final List messages = new ArrayList(); | ||
|
||
public synchronized void append(Object message) { | ||
messages.add(message); | ||
if (condition.test(message)) { | ||
logger.debug("condition met, will signal completion"); | ||
future.complete(Boolean.TRUE); | ||
} else { | ||
logger.debug("condition not met, will continue waiting"); | ||
} | ||
} | ||
|
||
public synchronized List collect() { | ||
return messages; | ||
} | ||
|
||
private CompletableFuture future = new CompletableFuture(); | ||
private Predicate condition = o -> true; // just a default | ||
|
||
// note how you can pass data in from the test for very dynamic checks | ||
public List waitUntilCount(int count) { | ||
condition = o -> messages.size() == count; | ||
try { | ||
future.get(5000, TimeUnit.MILLISECONDS); | ||
} catch (Exception e) { | ||
logger.error("wait timed out: {}", e + ""); | ||
} | ||
return messages; | ||
} | ||
|
||
public QueueConsumer() { | ||
this.connection = QueueUtils.getConnection(); | ||
try { | ||
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); | ||
Destination destination = session.createQueue(QUEUE_NAME); | ||
consumer = session.createConsumer(destination); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
public void listen(EventContext context) { | ||
setMessageListener(message -> { | ||
TextMessage tm = (TextMessage) message; | ||
try { | ||
context.signalAppend(tm.getText()); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
}); | ||
} | ||
|
||
public void setMessageListener(MessageListener ml) { | ||
try { | ||
consumer.setMessageListener(ml); | ||
consumer.setMessageListener(message -> { | ||
TextMessage tm = (TextMessage) message; | ||
try { | ||
// this is where we "collect" messages for assertions later | ||
append(tm.getText()); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
}); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
} | ||
} |
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