|
20 | 20 |
|
21 | 21 | package com.arangodb.internal; |
22 | 22 |
|
23 | | -import static org.hamcrest.Matchers.anyOf; |
24 | | -import static org.hamcrest.Matchers.is; |
25 | | -import static org.hamcrest.Matchers.not; |
26 | | -import static org.hamcrest.Matchers.nullValue; |
27 | | -import static org.junit.Assert.assertThat; |
28 | | - |
| 23 | +import com.arangodb.ArangoDBException; |
| 24 | +import com.arangodb.internal.net.*; |
| 25 | +import com.arangodb.util.ArangoSerialization; |
29 | 26 | import org.junit.Test; |
30 | 27 |
|
31 | | -import com.arangodb.internal.net.FallbackHostHandler; |
32 | | -import com.arangodb.internal.net.Host; |
33 | | -import com.arangodb.internal.net.HostDescription; |
34 | | -import com.arangodb.internal.net.HostHandler; |
35 | | -import com.arangodb.internal.net.HostImpl; |
36 | | -import com.arangodb.internal.net.HostResolver; |
37 | | -import com.arangodb.internal.net.HostSet; |
38 | | -import com.arangodb.internal.net.RandomHostHandler; |
39 | | -import com.arangodb.internal.net.RoundRobinHostHandler; |
40 | | -import com.arangodb.util.ArangoSerialization; |
| 28 | +import static org.hamcrest.Matchers.*; |
| 29 | +import static org.junit.Assert.assertThat; |
| 30 | +import static org.junit.Assert.fail; |
41 | 31 |
|
42 | 32 | /** |
43 | 33 | * @author Mark Vollmary |
44 | | - * |
45 | 34 | */ |
46 | 35 | public class HostHandlerTest { |
47 | 36 |
|
48 | | - private static final Host HOST_0 = new HostImpl(null, new HostDescription("127.0.0.1", 8529)); |
49 | | - private static final Host HOST_1 = new HostImpl(null, new HostDescription("127.0.0.2", 8529)); |
50 | | - private static final Host HOST_2 = new HostImpl(null, new HostDescription("127.0.0.3", 8529)); |
51 | | - |
52 | | - private static final HostResolver SINGLE_HOST = new HostResolver() { |
53 | | - |
54 | | - @Override |
55 | | - public HostSet resolve(final boolean initial, final boolean closeConnections) { |
56 | | - |
57 | | - HostSet set = new HostSet(); |
58 | | - set.addHost(HOST_0); |
59 | | - return set; |
60 | | - } |
61 | | - |
62 | | - @Override |
63 | | - public void init(ArangoExecutorSync executor, ArangoSerialization arangoSerialization) { |
64 | | - |
65 | | - } |
66 | | - |
67 | | - }; |
68 | | - |
69 | | - private static final HostResolver MULTIPLE_HOSTS = new HostResolver() { |
70 | | - |
71 | | - @Override |
72 | | - public HostSet resolve(final boolean initial, final boolean closeConnections) { |
73 | | - |
74 | | - HostSet set = new HostSet(); |
75 | | - set.addHost(HOST_0); |
76 | | - set.addHost(HOST_1); |
77 | | - set.addHost(HOST_2); |
78 | | - return set; |
79 | | - } |
80 | | - |
81 | | - @Override |
82 | | - public void init(ArangoExecutorSync executor, ArangoSerialization arangoSerialization) { |
83 | | - |
84 | | - } |
85 | | - |
86 | | - }; |
87 | | - |
88 | | - @Test |
89 | | - public void fallbachHostHandlerSingleHost() { |
90 | | - final HostHandler handler = new FallbackHostHandler(SINGLE_HOST); |
91 | | - assertThat(handler.get(null, null), is(HOST_0)); |
92 | | - handler.fail(); |
93 | | - assertThat(handler.get(null, null), is(HOST_0)); |
94 | | - } |
95 | | - |
96 | | - @Test |
97 | | - public void fallbackHostHandlerMultipleHosts() { |
98 | | - final HostHandler handler = new FallbackHostHandler(MULTIPLE_HOSTS); |
99 | | - for (int i = 0; i < 3; i++) { |
100 | | - assertThat(handler.get(null, null), is(HOST_0)); |
101 | | - handler.fail(); |
102 | | - assertThat(handler.get(null, null), is(HOST_1)); |
103 | | - handler.fail(); |
104 | | - assertThat(handler.get(null, null), is(HOST_2)); |
105 | | - if (i < 2) { |
106 | | - handler.fail(); |
107 | | - assertThat(handler.get(null, null), is(HOST_0)); |
108 | | - } else { |
109 | | - handler.fail(); |
110 | | - assertThat(handler.get(null, null), is(nullValue())); |
111 | | - } |
112 | | - } |
113 | | - } |
114 | | - |
115 | | - @Test |
116 | | - public void randomHostHandlerSingleHost() { |
117 | | - final HostHandler handler = new RandomHostHandler(SINGLE_HOST, new FallbackHostHandler(SINGLE_HOST)); |
118 | | - assertThat(handler.get(null, null), is(HOST_0)); |
119 | | - handler.fail(); |
120 | | - assertThat(handler.get(null, null), is(HOST_0)); |
121 | | - } |
122 | | - |
123 | | - @Test |
124 | | - public void randomHostHandlerMultipeHosts() { |
125 | | - final HostHandler handler = new RandomHostHandler(MULTIPLE_HOSTS, new FallbackHostHandler(MULTIPLE_HOSTS)); |
126 | | - final Host pick0 = handler.get(null, null); |
127 | | - assertThat(pick0, anyOf(is(HOST_0), is(HOST_1), is(HOST_2))); |
128 | | - handler.fail(); |
129 | | - assertThat(handler.get(null, null), anyOf(is(HOST_0), is(HOST_1), is(HOST_2))); |
130 | | - handler.success(); |
131 | | - assertThat(handler.get(null, null), is(pick0)); |
132 | | - } |
133 | | - |
134 | | - @Test |
135 | | - public void roundRobinHostHandlerSingleHost() { |
136 | | - final HostHandler handler = new RoundRobinHostHandler(SINGLE_HOST); |
137 | | - assertThat(handler.get(null, null), is(HOST_0)); |
138 | | - handler.fail(); |
139 | | - assertThat(handler.get(null, null), is(HOST_0)); |
140 | | - } |
141 | | - |
142 | | - @Test |
143 | | - public void roundRobinHostHandlerMultipleHosts() { |
144 | | - final HostHandler handler = new RoundRobinHostHandler(MULTIPLE_HOSTS); |
145 | | - final Host pick0 = handler.get(null, null); |
146 | | - assertThat(pick0, anyOf(is(HOST_0), is(HOST_1), is(HOST_2))); |
147 | | - final Host pick1 = handler.get(null, null); |
148 | | - assertThat(pick1, anyOf(is(HOST_0), is(HOST_1), is(HOST_2))); |
149 | | - assertThat(pick1, is(not(pick0))); |
150 | | - final Host pick2 = handler.get(null, null); |
151 | | - assertThat(pick2, anyOf(is(HOST_0), is(HOST_1), is(HOST_2))); |
152 | | - assertThat(pick2, not(anyOf(is(pick0), is(pick1)))); |
153 | | - final Host pick4 = handler.get(null, null); |
154 | | - assertThat(pick4, is(pick0)); |
155 | | - } |
| 37 | + private static final Host HOST_0 = new HostImpl(null, new HostDescription("127.0.0.1", 8529)); |
| 38 | + private static final Host HOST_1 = new HostImpl(null, new HostDescription("127.0.0.2", 8529)); |
| 39 | + private static final Host HOST_2 = new HostImpl(null, new HostDescription("127.0.0.3", 8529)); |
| 40 | + |
| 41 | + private static final HostResolver SINGLE_HOST = new HostResolver() { |
| 42 | + |
| 43 | + @Override |
| 44 | + public HostSet resolve(final boolean initial, final boolean closeConnections) { |
| 45 | + |
| 46 | + HostSet set = new HostSet(); |
| 47 | + set.addHost(HOST_0); |
| 48 | + return set; |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public void init(ArangoExecutorSync executor, ArangoSerialization arangoSerialization) { |
| 53 | + |
| 54 | + } |
| 55 | + |
| 56 | + }; |
| 57 | + |
| 58 | + private static final HostResolver MULTIPLE_HOSTS = new HostResolver() { |
| 59 | + |
| 60 | + @Override |
| 61 | + public HostSet resolve(final boolean initial, final boolean closeConnections) { |
| 62 | + |
| 63 | + HostSet set = new HostSet(); |
| 64 | + set.addHost(HOST_0); |
| 65 | + set.addHost(HOST_1); |
| 66 | + set.addHost(HOST_2); |
| 67 | + return set; |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public void init(ArangoExecutorSync executor, ArangoSerialization arangoSerialization) { |
| 72 | + |
| 73 | + } |
| 74 | + |
| 75 | + }; |
| 76 | + |
| 77 | + @Test |
| 78 | + public void fallbachHostHandlerSingleHost() { |
| 79 | + final HostHandler handler = new FallbackHostHandler(SINGLE_HOST); |
| 80 | + assertThat(handler.get(null, null), is(HOST_0)); |
| 81 | + handler.fail(); |
| 82 | + assertThat(handler.get(null, null), is(HOST_0)); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void fallbackHostHandlerMultipleHosts() { |
| 87 | + final HostHandler handler = new FallbackHostHandler(MULTIPLE_HOSTS); |
| 88 | + for (int i = 0; i < 3; i++) { |
| 89 | + assertThat(handler.get(null, null), is(HOST_0)); |
| 90 | + handler.fail(); |
| 91 | + assertThat(handler.get(null, null), is(HOST_1)); |
| 92 | + handler.fail(); |
| 93 | + assertThat(handler.get(null, null), is(HOST_2)); |
| 94 | + if (i < 2) { |
| 95 | + handler.fail(); |
| 96 | + assertThat(handler.get(null, null), is(HOST_0)); |
| 97 | + } else { |
| 98 | + handler.fail(); |
| 99 | + try { |
| 100 | + handler.get(null, null); |
| 101 | + fail(); |
| 102 | + } catch (ArangoDBException ignored) { |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void randomHostHandlerSingleHost() { |
| 110 | + final HostHandler handler = new RandomHostHandler(SINGLE_HOST, new FallbackHostHandler(SINGLE_HOST)); |
| 111 | + assertThat(handler.get(null, null), is(HOST_0)); |
| 112 | + handler.fail(); |
| 113 | + assertThat(handler.get(null, null), is(HOST_0)); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + public void randomHostHandlerMultipeHosts() { |
| 118 | + final HostHandler handler = new RandomHostHandler(MULTIPLE_HOSTS, new FallbackHostHandler(MULTIPLE_HOSTS)); |
| 119 | + final Host pick0 = handler.get(null, null); |
| 120 | + assertThat(pick0, anyOf(is(HOST_0), is(HOST_1), is(HOST_2))); |
| 121 | + handler.fail(); |
| 122 | + assertThat(handler.get(null, null), anyOf(is(HOST_0), is(HOST_1), is(HOST_2))); |
| 123 | + handler.success(); |
| 124 | + assertThat(handler.get(null, null), is(pick0)); |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + public void roundRobinHostHandlerSingleHost() { |
| 129 | + final HostHandler handler = new RoundRobinHostHandler(SINGLE_HOST); |
| 130 | + assertThat(handler.get(null, null), is(HOST_0)); |
| 131 | + handler.fail(); |
| 132 | + assertThat(handler.get(null, null), is(HOST_0)); |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + public void roundRobinHostHandlerMultipleHosts() { |
| 137 | + final HostHandler handler = new RoundRobinHostHandler(MULTIPLE_HOSTS); |
| 138 | + final Host pick0 = handler.get(null, null); |
| 139 | + assertThat(pick0, anyOf(is(HOST_0), is(HOST_1), is(HOST_2))); |
| 140 | + final Host pick1 = handler.get(null, null); |
| 141 | + assertThat(pick1, anyOf(is(HOST_0), is(HOST_1), is(HOST_2))); |
| 142 | + assertThat(pick1, is(not(pick0))); |
| 143 | + final Host pick2 = handler.get(null, null); |
| 144 | + assertThat(pick2, anyOf(is(HOST_0), is(HOST_1), is(HOST_2))); |
| 145 | + assertThat(pick2, not(anyOf(is(pick0), is(pick1)))); |
| 146 | + final Host pick4 = handler.get(null, null); |
| 147 | + assertThat(pick4, is(pick0)); |
| 148 | + } |
156 | 149 |
|
157 | 150 | } |
0 commit comments