|
11 | 11 | * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
|
12 | 12 | * or implied. See the License for the specific language governing permissions and limitations under
|
13 | 13 | * the License.
|
14 |
| - * |
15 | 14 | */
|
16 | 15 |
|
17 | 16 | package org.apache.geode.redis.mocks;
|
|
20 | 19 | import java.util.Collections;
|
21 | 20 | import java.util.List;
|
22 | 21 | import java.util.Objects;
|
| 22 | +import java.util.concurrent.CountDownLatch; |
| 23 | +import java.util.concurrent.TimeUnit; |
23 | 24 |
|
| 25 | +import redis.clients.jedis.Client; |
24 | 26 | import redis.clients.jedis.JedisPubSub;
|
25 | 27 |
|
26 | 28 | public class MockSubscriber extends JedisPubSub {
|
| 29 | + |
| 30 | + private final CountDownLatch subscriptionLatch; |
| 31 | + private final CountDownLatch unsubscriptionLatch; |
27 | 32 | private final List<String> receivedMessages = Collections.synchronizedList(new ArrayList<>());
|
28 | 33 | private final List<String> receivedPMessages = Collections.synchronizedList(new ArrayList<>());
|
| 34 | + public final List<UnsubscribeInfo> unsubscribeInfos = |
| 35 | + Collections.synchronizedList(new ArrayList<>()); |
| 36 | + public final List<UnsubscribeInfo> punsubscribeInfos = |
| 37 | + Collections.synchronizedList(new ArrayList<>()); |
| 38 | + private String localSocketAddress; |
| 39 | + |
| 40 | + public MockSubscriber() { |
| 41 | + this(new CountDownLatch(1)); |
| 42 | + } |
| 43 | + |
| 44 | + public MockSubscriber(CountDownLatch subscriptionLatch) { |
| 45 | + this(subscriptionLatch, new CountDownLatch(1)); |
| 46 | + } |
| 47 | + |
| 48 | + public MockSubscriber(CountDownLatch subscriptionLatch, CountDownLatch unsubscriptionLatch) { |
| 49 | + this.subscriptionLatch = subscriptionLatch; |
| 50 | + this.unsubscriptionLatch = unsubscriptionLatch; |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public void proceed(Client client, String... channels) { |
| 55 | + localSocketAddress = client.getSocket().getLocalSocketAddress().toString(); |
| 56 | + super.proceed(client, channels); |
| 57 | + } |
| 58 | + |
| 59 | + private void switchThreadName(String suffix) { |
| 60 | + String threadName = Thread.currentThread().getName(); |
| 61 | + int suffixIndex = threadName.indexOf(" -- "); |
| 62 | + if (suffixIndex >= 0) { |
| 63 | + threadName = threadName.substring(0, suffixIndex); |
| 64 | + } |
| 65 | + |
| 66 | + threadName += " -- " + suffix + " [" + localSocketAddress + "]"; |
| 67 | + Thread.currentThread().setName(threadName); |
| 68 | + } |
29 | 69 |
|
30 | 70 | public List<String> getReceivedMessages() {
|
31 |
| - return new ArrayList<>(receivedMessages); |
| 71 | + return receivedMessages; |
32 | 72 | }
|
33 | 73 |
|
34 | 74 | public List<String> getReceivedPMessages() {
|
35 | 75 | return new ArrayList<>(receivedPMessages);
|
36 | 76 | }
|
37 | 77 |
|
38 |
| - public final List<UnsubscribeInfo> unsubscribeInfos = |
39 |
| - Collections.synchronizedList(new ArrayList<>()); |
40 |
| - public final List<UnsubscribeInfo> punsubscribeInfos = |
41 |
| - Collections.synchronizedList(new ArrayList<>()); |
| 78 | + @Override |
| 79 | + public void onMessage(String channel, String message) { |
| 80 | + switchThreadName(String.format("MESSAGE %s %s", channel, message)); |
| 81 | + receivedMessages.add(message); |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public void onPMessage(String pattern, String channel, String message) { |
| 86 | + switchThreadName(String.format("PMESSAGE %s %s %s", pattern, channel, message)); |
| 87 | + receivedPMessages.add(message); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public void onSubscribe(String channel, int subscribedChannels) { |
| 92 | + switchThreadName(String.format("SUBSCRIBE %s", channel)); |
| 93 | + subscriptionLatch.countDown(); |
| 94 | + } |
| 95 | + |
| 96 | + private static final int AWAIT_TIMEOUT_MILLIS = 30000; |
| 97 | + |
| 98 | + public void awaitSubscribe(String channel) { |
| 99 | + try { |
| 100 | + if (!subscriptionLatch.await(AWAIT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)) { |
| 101 | + throw new RuntimeException("awaitSubscribe timed out for channel: " + channel); |
| 102 | + } |
| 103 | + } catch (InterruptedException e) { |
| 104 | + throw new RuntimeException(e); |
| 105 | + } |
| 106 | + } |
42 | 107 |
|
43 | 108 | @Override
|
44 | 109 | public void onUnsubscribe(String channel, int subscribedChannels) {
|
| 110 | + switchThreadName(String.format("UNSUBSCRIBE %s %d", channel, subscribedChannels)); |
45 | 111 | unsubscribeInfos.add(new UnsubscribeInfo(channel, subscribedChannels));
|
| 112 | + unsubscriptionLatch.countDown(); |
| 113 | + } |
| 114 | + |
| 115 | + public void awaitUnsubscribe(String channel) { |
| 116 | + try { |
| 117 | + if (!unsubscriptionLatch.await(AWAIT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)) { |
| 118 | + throw new RuntimeException("awaitUnsubscribe timed out for channel: " + channel); |
| 119 | + } |
| 120 | + } catch (InterruptedException e) { |
| 121 | + throw new RuntimeException(e); |
| 122 | + } |
46 | 123 | }
|
47 | 124 |
|
48 | 125 | @Override
|
49 | 126 | public void onPUnsubscribe(String pattern, int subscribedChannels) {
|
| 127 | + switchThreadName(String.format("PUNSUBSCRIBE %s %d", pattern, subscribedChannels)); |
50 | 128 | punsubscribeInfos.add(new UnsubscribeInfo(pattern, subscribedChannels));
|
51 | 129 | }
|
52 | 130 |
|
@@ -86,14 +164,4 @@ public String toString() {
|
86 | 164 | }
|
87 | 165 | }
|
88 | 166 |
|
89 |
| - |
90 |
| - @Override |
91 |
| - public void onMessage(String channel, String message) { |
92 |
| - receivedMessages.add(message); |
93 |
| - } |
94 |
| - |
95 |
| - @Override |
96 |
| - public void onPMessage(String pattern, String channel, String message) { |
97 |
| - receivedPMessages.add(message); |
98 |
| - } |
99 | 167 | }
|
0 commit comments