-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
GetServersProcedureV1RoutingTest.java
118 lines (103 loc) · 4.53 KB
/
GetServersProcedureV1RoutingTest.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
/*
* Copyright (c) 2002-2018 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* 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.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.causalclustering.load_balancing.procedure;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.neo4j.causalclustering.core.consensus.LeaderLocator;
import org.neo4j.causalclustering.discovery.CoreServerInfo;
import org.neo4j.causalclustering.discovery.CoreTopology;
import org.neo4j.causalclustering.discovery.CoreTopologyService;
import org.neo4j.causalclustering.discovery.ReadReplicaTopology;
import org.neo4j.causalclustering.identity.ClusterId;
import org.neo4j.causalclustering.identity.MemberId;
import org.neo4j.kernel.configuration.Config;
import static java.util.Collections.emptyMap;
import static org.junit.Assert.assertFalse;
import static org.junit.runners.Parameterized.Parameters;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.neo4j.causalclustering.discovery.TestTopology.addressesForCore;
import static org.neo4j.causalclustering.identity.RaftTestMember.member;
import static org.neo4j.helpers.collection.Iterators.asList;
import static org.neo4j.logging.NullLogProvider.getInstance;
@RunWith( Parameterized.class )
public class GetServersProcedureV1RoutingTest
{
@Parameters
public static Collection<Object> data()
{
return Arrays.asList( 1, 2 );
} //the write endpoints are always index 0
@Parameter
public int serverClass;
private ClusterId clusterId = new ClusterId( UUID.randomUUID() );
private Config config = Config.defaults();
@Test
public void shouldReturnEndpointsInDifferentOrders() throws Exception
{
// given
final CoreTopologyService coreTopologyService = mock( CoreTopologyService.class );
LeaderLocator leaderLocator = mock( LeaderLocator.class );
when( leaderLocator.getLeader() ).thenReturn( member( 0 ) );
Map<MemberId,CoreServerInfo> coreMembers = new HashMap<>();
coreMembers.put( member( 0 ), addressesForCore( 0 ) );
coreMembers.put( member( 1 ), addressesForCore( 1 ) );
coreMembers.put( member( 2 ), addressesForCore( 2 ) );
final CoreTopology clusterTopology = new CoreTopology( clusterId, false, coreMembers );
when( coreTopologyService.coreServers() ).thenReturn( clusterTopology );
when( coreTopologyService.readReplicas() ).thenReturn( new ReadReplicaTopology( emptyMap() ) );
final LegacyGetServersProcedure proc =
new LegacyGetServersProcedure( coreTopologyService, leaderLocator, config, getInstance() );
// when
Object[] endpoints = getEndpoints( proc );
//then
Object[] endpointsInDifferentOrder = getEndpoints( proc );
for ( int i = 0; i < 100; i++ )
{
if ( Arrays.deepEquals( endpointsInDifferentOrder, endpoints ) )
{
endpointsInDifferentOrder = getEndpoints( proc );
}
else
{
//Different order of servers, no need to retry.
break;
}
}
assertFalse( Arrays.deepEquals( endpoints, endpointsInDifferentOrder ) );
}
private Object[] getEndpoints( LegacyGetServersProcedure proc )
throws org.neo4j.kernel.api.exceptions.ProcedureException
{
List<Object[]> results = asList( proc.apply( null, new Object[0] ) );
Object[] rows = results.get( 0 );
List<Map<String,Object[]>> servers = (List<Map<String,Object[]>>) rows[1];
Map<String,Object[]> endpoints = servers.get( serverClass );
return endpoints.get( "addresses" );
}
}