aboutsummaryrefslogtreecommitdiffstats
path: root/messagebus/src/test/java/com/yahoo/messagebus/test/ReceptorTestCase.java
blob: e4e87a9321e15c2c3742ad2d573d38c7b909a9a2 (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright Yahoo. 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.Reply;
import com.yahoo.text.Utf8String;
import org.junit.jupiter.api.Test;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import static org.junit.jupiter.api.Assertions.*;

/**
 * @author Simon Thoresen Hult
 */
public class ReceptorTestCase {

    @Test
    void requireThatAccessorsWork() {
        Receptor receptor = new Receptor();
        assertNull(receptor.getMessage(0));
        Message msg = new MyMessage();
        receptor.handleMessage(msg);
        assertSame(msg, receptor.getMessage(0));

        Reply reply = new MyReply();
        receptor.handleReply(reply);
        assertSame(reply, receptor.getReply(0));
    }

    @Test
    void requireThatMessagesAndRepliesAreTrackedIndividually() {
        Receptor receptor = new Receptor();
        receptor.handleMessage(new MyMessage());
        receptor.handleReply(new MyReply());
        assertNotNull(receptor.getMessage(0));
        assertNotNull(receptor.getReply(0));

        receptor.handleMessage(new MyMessage());
        receptor.handleReply(new MyReply());
        assertNotNull(receptor.getReply(0));
        assertNotNull(receptor.getMessage(0));
    }

    @Test
    void requireThatMessagesCanBeWaitedFor() {
        final Receptor receptor = new Receptor();
        Thread thread = new Thread() {

            @Override
            public void run() {
                try {
                    Thread.sleep(100);
                    receptor.handleMessage(new MyMessage());
                } catch (InterruptedException e) {

                }
            }
        };
        thread.start();
        assertNotNull(receptor.getMessage(60));
    }

    @Test
    void requireThatMessageWaitCanBeInterrupted() throws InterruptedException {
        final Receptor receptor = new Receptor();
        final CountDownLatch latch = new CountDownLatch(1);
        Thread thread = new Thread() {

            @Override
            public void run() {
                receptor.getMessage(60);
                latch.countDown();
            }
        };
        thread.start();
        thread.interrupt();
        assertTrue(latch.await(30, TimeUnit.SECONDS));
    }

    @Test
    void requireThatRepliesCanBeWaitedFor() {
        final Receptor receptor = new Receptor();
        Thread thread = new Thread() {

            @Override
            public void run() {
                try {
                    Thread.sleep(100);
                    receptor.handleReply(new MyReply());
                } catch (InterruptedException e) {

                }
            }
        };
        thread.start();
        assertNotNull(receptor.getReply(60));
    }

    @Test
    void requireThatReplyWaitCanBeInterrupted() throws InterruptedException {
        final Receptor receptor = new Receptor();
        final CountDownLatch latch = new CountDownLatch(1);
        Thread thread = new Thread() {

            @Override
            public void run() {
                receptor.getReply(60);
                latch.countDown();
            }
        };
        thread.start();
        thread.interrupt();
        assertTrue(latch.await(30, TimeUnit.SECONDS));
    }

    private static class MyMessage extends Message {

        @Override
        public Utf8String getProtocol() {
            return null;
        }

        @Override
        public int getType() {
            return 0;
        }
    }

    private static class MyReply extends Reply {

        @Override
        public Utf8String getProtocol() {
            return null;
        }

        @Override
        public int getType() {
            return 0;
        }
    }

}