aboutsummaryrefslogtreecommitdiffstats
path: root/messagebus/src/main/java/com/yahoo/messagebus/test/Receptor.java
blob: bf7d0bef003f5744fa8769f355d69879a3936826 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.messagebus.test;

import com.yahoo.messagebus.Message;
import com.yahoo.messagebus.MessageHandler;
import com.yahoo.messagebus.Reply;
import com.yahoo.messagebus.ReplyHandler;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * @author <a href="mailto:havardpe@yahoo-inc.com">Haavard Pettersen</a>
 */
public class Receptor implements MessageHandler, ReplyHandler {

    private final BlockingQueue<Message> msg = new LinkedBlockingQueue<>();
    private final BlockingQueue<Reply> reply = new LinkedBlockingQueue<>();

    public void reset() {
        msg.clear();
        reply.clear();
    }

    public void handleMessage(Message msg) {
        this.msg.add(msg);
    }

    public void handleReply(Reply reply) {
        this.reply.add(reply);
    }

    public Message getMessage(int seconds) {
        try {
            return msg.poll(seconds, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            return null;
        }
    }

    public Reply getReply(int seconds) {
        try {
            return reply.poll(seconds, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            return null;
        }
    }
}