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
|
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.messagebus;
import com.yahoo.messagebus.routing.RoutingContext;
import com.yahoo.messagebus.routing.RoutingPolicy;
import com.yahoo.messagebus.test.SimpleProtocol;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
/**
* @author Simon Thoresen Hult
*/
public class ProtocolRepositoryTestCase {
@Test
void requireThatPolicyCanBeNull() {
ProtocolRepository repo = new ProtocolRepository();
SimpleProtocol protocol = new SimpleProtocol();
repo.putProtocol(protocol);
assertNull(repo.getRoutingPolicy(SimpleProtocol.NAME, "Custom", null));
}
@Test
void requireThatPolicyCanBeCreated() {
ProtocolRepository repo = new ProtocolRepository();
SimpleProtocol protocol = new SimpleProtocol();
protocol.addPolicyFactory("Custom", new MyFactory());
repo.putProtocol(protocol);
assertNotNull(repo.getRoutingPolicy(SimpleProtocol.NAME, "Custom", null));
}
@Test
void requireThatPolicyIsCached() {
ProtocolRepository repo = new ProtocolRepository();
SimpleProtocol protocol = new SimpleProtocol();
protocol.addPolicyFactory("Custom", new MyFactory());
repo.putProtocol(protocol);
RoutingPolicy prev = repo.getRoutingPolicy(SimpleProtocol.NAME, "Custom", null);
assertNotNull(prev);
RoutingPolicy next = repo.getRoutingPolicy(SimpleProtocol.NAME, "Custom", null);
assertNotNull(next);
assertSame(prev, next);
}
@Test
void requireThatPolicyParamIsPartOfCacheKey() {
ProtocolRepository repo = new ProtocolRepository();
SimpleProtocol protocol = new SimpleProtocol();
protocol.addPolicyFactory("Custom", new MyFactory());
repo.putProtocol(protocol);
RoutingPolicy prev = repo.getRoutingPolicy(SimpleProtocol.NAME, "Custom", "foo");
assertNotNull(prev);
RoutingPolicy next = repo.getRoutingPolicy(SimpleProtocol.NAME, "Custom", "bar");
assertNotNull(next);
assertNotSame(prev, next);
}
@Test
void requireThatCreatePolicyExceptionIsCaught() {
ProtocolRepository repo = new ProtocolRepository();
SimpleProtocol protocol = new SimpleProtocol();
protocol.addPolicyFactory("Custom", new SimpleProtocol.PolicyFactory() {
@Override
public RoutingPolicy create(String param) {
throw new RuntimeException();
}
});
repo.putProtocol(protocol);
assertNull(repo.getRoutingPolicy(SimpleProtocol.NAME, "Custom", null));
}
private static class MyFactory implements SimpleProtocol.PolicyFactory {
@Override
public RoutingPolicy create(String param) {
return new MyPolicy();
}
}
private static class MyPolicy implements RoutingPolicy {
@Override
public void select(RoutingContext context) {
}
@Override
public void merge(RoutingContext context) {
}
@Override
public void destroy() {
}
}
}
|