-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
ProtocolInstallerRepositoryTest.java
299 lines (253 loc) · 11.7 KB
/
ProtocolInstallerRepositoryTest.java
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
* Copyright (c) 2002-2018 "Neo4j,"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j Enterprise Edition. The included source
* code can be redistributed and/or modified under the terms of the
* GNU AFFERO GENERAL PUBLIC LICENSE Version 3
* (http://www.fsf.org/licensing/licenses/agpl-3.0.html) with the
* Commons Clause, as found in the associated LICENSE.txt file.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* Neo4j object code can be licensed independently from the source
* under separate terms from the AGPL. Inquiries can be directed to:
* licensing@neo4j.com
*
* More information is also available at:
* https://neo4j.com/licensing/
*/
package org.neo4j.causalclustering.protocol;
import org.junit.Test;
import java.util.Collection;
import java.util.List;
import org.neo4j.causalclustering.core.consensus.protocol.v1.RaftProtocolClientInstallerV1;
import org.neo4j.causalclustering.core.consensus.protocol.v1.RaftProtocolServerInstallerV1;
import org.neo4j.causalclustering.handlers.VoidPipelineWrapperFactory;
import org.neo4j.causalclustering.protocol.Protocol.ApplicationProtocols;
import org.neo4j.causalclustering.protocol.Protocol.ModifierProtocols;
import org.neo4j.causalclustering.protocol.ProtocolInstaller.Orientation;
import org.neo4j.causalclustering.protocol.handshake.ProtocolStack;
import org.neo4j.causalclustering.protocol.handshake.TestProtocols.TestApplicationProtocols;
import org.neo4j.causalclustering.protocol.handshake.TestProtocols.TestModifierProtocols;
import org.neo4j.logging.NullLogProvider;
import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.sameInstance;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
public class ProtocolInstallerRepositoryTest
{
private List<ModifierProtocolInstaller<Orientation.Client>> clientModifiers =
asList( new SnappyClientInstaller(),
new LZOClientInstaller(),
new LZ4ClientInstaller(),
new LZ4HighCompressionClientInstaller(),
new Rot13ClientInstaller() );
private List<ModifierProtocolInstaller<Orientation.Server>> serverModifiers =
asList( new SnappyServerInstaller(),
new LZOServerInstaller(),
new LZ4ServerInstaller(),
new LZ4ValidatingServerInstaller(),
new Rot13ServerInstaller() );
private final NettyPipelineBuilderFactory pipelineBuilderFactory =
new NettyPipelineBuilderFactory( VoidPipelineWrapperFactory.VOID_WRAPPER );
private final RaftProtocolClientInstallerV1.Factory raftProtocolClientInstaller =
new RaftProtocolClientInstallerV1.Factory( pipelineBuilderFactory, NullLogProvider.getInstance() );
private final RaftProtocolServerInstallerV1.Factory raftProtocolServerInstaller =
new RaftProtocolServerInstallerV1.Factory( null, pipelineBuilderFactory, NullLogProvider.getInstance() );
private final ProtocolInstallerRepository<Orientation.Client> clientRepository =
new ProtocolInstallerRepository<>( asList( raftProtocolClientInstaller ), clientModifiers );
private final ProtocolInstallerRepository<Orientation.Server> serverRepository =
new ProtocolInstallerRepository<>( asList( raftProtocolServerInstaller ), serverModifiers );
@Test
public void shouldReturnRaftServerInstaller()
{
assertEquals(
raftProtocolServerInstaller.applicationProtocol(),
serverRepository.installerFor( new ProtocolStack( ApplicationProtocols.RAFT_1, emptyList() ) ).applicationProtocol() );
}
@Test
public void shouldReturnRaftClientInstaller()
{
assertEquals(
raftProtocolClientInstaller.applicationProtocol(),
clientRepository.installerFor( new ProtocolStack( ApplicationProtocols.RAFT_1, emptyList() ) ).applicationProtocol() );
}
@Test
public void shouldReturnModifierProtocolsForClient()
{
// given
Protocol.ModifierProtocol expected = TestModifierProtocols.SNAPPY;
ProtocolStack protocolStack = new ProtocolStack( ApplicationProtocols.RAFT_1, asList( expected ) );
// when
Collection<Collection<Protocol.ModifierProtocol>> actual = clientRepository.installerFor( protocolStack ).modifiers();
// then
assertThat( actual, contains( contains( expected ) ) );
}
@Test
public void shouldReturnModifierProtocolsForServer()
{
// given
Protocol.ModifierProtocol expected = TestModifierProtocols.SNAPPY;
ProtocolStack protocolStack = new ProtocolStack( ApplicationProtocols.RAFT_1, asList( expected ) );
// when
Collection<Collection<Protocol.ModifierProtocol>> actual = serverRepository.installerFor( protocolStack ).modifiers();
// then
assertThat( actual, contains( contains( expected ) ) );
}
@Test
public void shouldReturnModifierProtocolsForProtocolWithSharedInstallerForClient()
{
// given
Protocol.ModifierProtocol expected = TestModifierProtocols.LZ4_HIGH_COMPRESSION_VALIDATING;
TestModifierProtocols alsoSupported = TestModifierProtocols.LZ4_HIGH_COMPRESSION;
ProtocolStack protocolStack = new ProtocolStack( ApplicationProtocols.RAFT_1, asList( expected ) );
// when
Collection<Collection<Protocol.ModifierProtocol>> actual = clientRepository.installerFor( protocolStack ).modifiers();
// then
assertThat( actual, contains( containsInAnyOrder( expected, alsoSupported ) )) ;
}
@Test
public void shouldReturnModifierProtocolsForProtocolWithSharedInstallerForServer()
{
// given
Protocol.ModifierProtocol expected = TestModifierProtocols.LZ4_HIGH_COMPRESSION_VALIDATING;
TestModifierProtocols alsoSupported = TestModifierProtocols.LZ4_VALIDATING;
ProtocolStack protocolStack = new ProtocolStack( ApplicationProtocols.RAFT_1, asList( expected ) );
// when
Collection<Collection<Protocol.ModifierProtocol>> actual = serverRepository.installerFor( protocolStack ).modifiers();
// then
assertThat( actual, contains( containsInAnyOrder( expected, alsoSupported ) )) ;
}
@Test
public void shouldUseDifferentInstancesOfProtocolInstaller()
{
// given
ProtocolStack protocolStack1 = new ProtocolStack( ApplicationProtocols.RAFT_1, asList( TestModifierProtocols.SNAPPY ) );
ProtocolStack protocolStack2 = new ProtocolStack( ApplicationProtocols.RAFT_1, asList( TestModifierProtocols.LZO ) );
// when
ProtocolInstaller protocolInstaller1 = clientRepository.installerFor( protocolStack1 );
ProtocolInstaller protocolInstaller2 = clientRepository.installerFor( protocolStack2 );
// then
assertThat( protocolInstaller1, not( sameInstance( protocolInstaller2 ) ) );
}
@Test( expected = IllegalArgumentException.class )
public void shouldThrowIfAttemptingToCreateInstallerForMultipleModifiersWithSameIdentifier()
{
// given
ProtocolStack protocolStack = new ProtocolStack(
ApplicationProtocols.RAFT_1,
asList( TestModifierProtocols.SNAPPY, TestModifierProtocols.LZO ) );
// when
clientRepository.installerFor( protocolStack );
// then throw
}
@Test( expected = IllegalArgumentException.class )
public void shouldNotInitialiseIfMultipleInstallersForSameProtocolForServer()
{
new ProtocolInstallerRepository<>( asList( raftProtocolServerInstaller, raftProtocolServerInstaller ), emptyList() );
}
@Test( expected = IllegalArgumentException.class )
public void shouldNotInitialiseIfMultipleInstallersForSameProtocolForClient()
{
new ProtocolInstallerRepository<>( asList( raftProtocolClientInstaller, raftProtocolClientInstaller ), emptyList() );
}
@Test( expected = IllegalStateException.class )
public void shouldThrowIfUnknownProtocolForServer()
{
serverRepository.installerFor( new ProtocolStack( TestApplicationProtocols.RAFT_3, emptyList() ) );
}
@Test( expected = IllegalStateException.class )
public void shouldThrowIfUnknownProtocolForClient()
{
clientRepository.installerFor( new ProtocolStack( TestApplicationProtocols.RAFT_3, emptyList() ) );
}
@Test( expected = IllegalStateException.class )
public void shouldThrowIfUnknownModifierProtocol()
{
// given
// setup used TestModifierProtocols, doesn't know about production protocols
Protocol.ModifierProtocol unknownProtocol = ModifierProtocols.COMPRESSION_SNAPPY;
// when
serverRepository.installerFor( new ProtocolStack( ApplicationProtocols.RAFT_1, asList( unknownProtocol ) ) );
// then throw
}
// Dummy installers
private static class SnappyClientInstaller extends ModifierProtocolInstaller.BaseClientModifier
{
private SnappyClientInstaller()
{
super( "snappy", null, TestModifierProtocols.SNAPPY );
}
}
private static class LZOClientInstaller extends ModifierProtocolInstaller.BaseClientModifier
{
private LZOClientInstaller()
{
super( "lzo", null, TestModifierProtocols.LZO );
}
}
private static class LZ4ClientInstaller extends ModifierProtocolInstaller.BaseClientModifier
{
private LZ4ClientInstaller()
{
super( "lz4", null, TestModifierProtocols.LZ4, TestModifierProtocols.LZ4_VALIDATING );
}
}
private static class LZ4HighCompressionClientInstaller extends ModifierProtocolInstaller.BaseClientModifier
{
private LZ4HighCompressionClientInstaller()
{
super( "lz4", null, TestModifierProtocols.LZ4_HIGH_COMPRESSION, TestModifierProtocols.LZ4_HIGH_COMPRESSION_VALIDATING );
}
}
private class Rot13ClientInstaller extends ModifierProtocolInstaller.BaseClientModifier
{
Rot13ClientInstaller()
{
super( "rot13", null, TestModifierProtocols.ROT13 );
}
}
private static class SnappyServerInstaller extends ModifierProtocolInstaller.BaseServerModifier
{
private SnappyServerInstaller()
{
super( "snappy", null, TestModifierProtocols.SNAPPY );
}
}
private static class LZOServerInstaller extends ModifierProtocolInstaller.BaseServerModifier
{
private LZOServerInstaller()
{
super( "lzo", null, TestModifierProtocols.LZO );
}
}
private static class LZ4ServerInstaller extends ModifierProtocolInstaller.BaseServerModifier
{
private LZ4ServerInstaller()
{
super( "lz4", null, TestModifierProtocols.LZ4, TestModifierProtocols.LZ4_HIGH_COMPRESSION );
}
}
private static class LZ4ValidatingServerInstaller extends ModifierProtocolInstaller.BaseServerModifier
{
private LZ4ValidatingServerInstaller()
{
super( "lz4", null, TestModifierProtocols.LZ4_VALIDATING, TestModifierProtocols.LZ4_HIGH_COMPRESSION_VALIDATING );
}
}
private class Rot13ServerInstaller extends ModifierProtocolInstaller.BaseServerModifier
{
Rot13ServerInstaller()
{
super( "rot13", null, TestModifierProtocols.ROT13 );
}
}
}