-
Notifications
You must be signed in to change notification settings - Fork 615
/
TestServer.java
177 lines (149 loc) · 6.06 KB
/
TestServer.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
package org.infinispan.server.test.core;
import java.io.Closeable;
import java.io.File;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import org.infinispan.client.hotrod.RemoteCacheManager;
import org.infinispan.client.hotrod.configuration.ConfigurationBuilder;
import org.infinispan.client.rest.RestClient;
import org.infinispan.client.rest.configuration.RestClientConfigurationBuilder;
import org.infinispan.commons.test.Exceptions;
import net.spy.memcached.MemcachedClient;
/**
* Class that contains all the logic related to the server driver
*
* @author Katia Aresti
* @since 11
*/
public class TestServer {
protected InfinispanServerDriver serverDriver;
protected final List<Consumer<File>> configurationEnhancers = new ArrayList<>();
protected InfinispanServerTestConfiguration configuration;
public TestServer(InfinispanServerTestConfiguration configuration) {
this.configuration = configuration;
}
public boolean isDriverInitialized() {
return serverDriver != null;
}
public InfinispanServerDriver getDriver() {
if (serverDriver == null) {
throw new IllegalStateException("ServerDriver is null. Server driver not initialized");
}
return serverDriver;
}
public void stopServerDriver(String testName) {
getDriver().stop(testName);
serverDriver = null;
}
/**
* @return a client configured against the Hot Rod endpoint exposed by the server
*/
public RemoteCacheManager newHotRodClient() {
return newHotRodClient(new ConfigurationBuilder());
}
public RemoteCacheManager newHotRodClient(ConfigurationBuilder builder) {
return newHotRodClient(builder, 11222);
}
/**
* Create a client adding in the initial server list the server address given by the index.
* If empty, it will add all servers by default OR those that were configured in the server list
* @param index the server index
* @return a client configured against the Hot Rod endpoint exposed by the server
*/
public RemoteCacheManager newHotRodClient(ConfigurationBuilder builder, int port, int... index) {
if (index.length == 0) {
if (builder.servers().isEmpty()) {
for (int i = 0; i < getDriver().getConfiguration().numServers(); i++) {
configureHotRodClient(builder, port, i);
}
}
} else {
for (int i : index) {
configureHotRodClient(builder, port, i);
}
}
return getDriver().createRemoteCacheManager(builder);
}
private void configureHotRodClient(ConfigurationBuilder builder, int port, int i) {
if (getDriver().getConfiguration().runMode() == ServerRunMode.CONTAINER) {
ContainerInfinispanServerDriver containerDriver = (ContainerInfinispanServerDriver) serverDriver;
InfinispanGenericContainer container = containerDriver.getContainer(i);
builder.addServer().host(container.getIpAddress().getHostAddress()).port(port);
} else {
InetSocketAddress serverAddress = getDriver().getServerSocket(i, port);
builder.addServer().host(serverAddress.getHostName()).port(serverAddress.getPort());
}
}
public RestClient newRestClient(RestClientConfigurationBuilder builder) {
return newRestClient(builder, 11222);
}
public RestClient newRestClient(RestClientConfigurationBuilder builder, int port) {
// Add all known server addresses
for (int i = 0; i < getDriver().getConfiguration().numServers(); i++) {
InetSocketAddress serverAddress = getDriver().getServerSocket(i, port);
builder.addServer().host(serverAddress.getHostName()).port(serverAddress.getPort());
}
return RestClient.forConfiguration(builder.build());
}
CloseableMemcachedClient newMemcachedClient() {
List<InetSocketAddress> addresses = new ArrayList<>();
for (int i = 0; i < getDriver().getConfiguration().numServers(); i++) {
InetSocketAddress unresolved = getDriver().getServerSocket(i, 11221);
addresses.add(new InetSocketAddress(unresolved.getHostName(), unresolved.getPort()));
}
MemcachedClient memcachedClient = Exceptions.unchecked(() -> new MemcachedClient(addresses));
return new CloseableMemcachedClient(memcachedClient);
}
public void beforeListeners() {
configuration.listeners().forEach(l -> l.before(serverDriver));
}
public void afterListeners() {
configuration.listeners().forEach(l -> l.after(serverDriver));
}
public boolean isContainerRunWithDefaultServerConfig() {
return configuration.isDefaultFile() && configuration.runMode() == ServerRunMode.CONTAINER;
}
public static class CloseableMemcachedClient implements Closeable {
final MemcachedClient client;
public CloseableMemcachedClient(MemcachedClient client) {
this.client = client;
}
public MemcachedClient getClient() {
return client;
}
@Override
public void close() {
client.shutdown();
}
}
/**
* Create a new REST client to connect to the server
*
* @param builder client configuration
* @param n the server number
*
* @return a client configured against the nth server
*/
public RestClient newRestClientForServer(RestClientConfigurationBuilder builder, int port, int n) {
InetSocketAddress serverAddress = getDriver().getServerSocket(n, port);
builder.addServer().host(serverAddress.getHostName()).port(serverAddress.getPort());
return RestClient.forConfiguration(builder.build());
}
public void add(Consumer<File> enhancer) {
configurationEnhancers.add(enhancer);
}
public void initServerDriver() {
serverDriver = configuration.runMode().newDriver(configuration);
}
public void enhanceConfiguration() {
configurationEnhancers.forEach(c -> c.accept(serverDriver.getConfDir()));
}
public boolean hasCrossSiteEnabled() {
return configuration.site() != null;
}
public String getSiteName() {
return configuration.site();
}
}