This repository has been archived by the owner on May 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 789
/
WebClientFactoryTest.java
204 lines (166 loc) · 7.25 KB
/
WebClientFactoryTest.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
/**
* Copyright (c) 2014,2018 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.smarthome.io.net.http.internal;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import static org.mockito.MockitoAnnotations.initMocks;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.stream.Stream;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.api.ContentResponse;
import org.eclipse.jetty.websocket.client.WebSocketClient;
import org.eclipse.smarthome.io.net.http.TrustManagerProvider;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.mockito.Mock;
public class WebClientFactoryTest {
private WebClientFactoryImpl webClientFactory;
private static final String TEST_URL = "https://www.eclipse.org/";
@Mock
private TrustManagerProvider trustmanagerProvider;
@Before
public void setup() {
initMocks(this);
webClientFactory = new WebClientFactoryImpl();
webClientFactory.setTrustmanagerProvider(trustmanagerProvider);
}
@Test
public void testGetClients() throws Exception {
webClientFactory.activate(createConfigMap(10, 200, 60, 5, 10, 60));
HttpClient httpClient = webClientFactory.getCommonHttpClient();
WebSocketClient webSocketClient = webClientFactory.getCommonWebSocketClient();
assertThat(httpClient, is(notNullValue()));
assertThat(webSocketClient, is(notNullValue()));
webClientFactory.deactivate();
}
@Test
public void testGetHttpClientWithEndpoint() throws Exception {
webClientFactory.activate(createConfigMap(10, 200, 60, 5, 10, 60));
when(trustmanagerProvider.getTrustManagers("https://www.heise.de")).thenReturn(Stream.empty());
HttpClient httpClient = webClientFactory.createHttpClient("consumer", TEST_URL);
assertThat(httpClient, is(notNullValue()));
verify(trustmanagerProvider).getTrustManagers(TEST_URL);
httpClient.stop();
webClientFactory.deactivate();
}
@Test
public void testGetWebSocketClientWithEndpoint() throws Exception {
webClientFactory.activate(createConfigMap(10, 200, 60, 5, 10, 60));
when(trustmanagerProvider.getTrustManagers("https://www.heise.de")).thenReturn(Stream.empty());
WebSocketClient webSocketClient = webClientFactory.createWebSocketClient("consumer", TEST_URL);
assertThat(webSocketClient, is(notNullValue()));
verify(trustmanagerProvider).getTrustManagers(TEST_URL);
webSocketClient.stop();
webClientFactory.deactivate();
}
@Ignore("only for manual test")
@Test
public void testMultiThreadedShared() throws Exception {
webClientFactory.activate(createConfigMap(10, 200, 60, 5, 10, 60));
ThreadPoolExecutor workers = new ThreadPoolExecutor(20, 80, 60, TimeUnit.SECONDS,
new ArrayBlockingQueue<Runnable>(50 * 50));
List<HttpClient> clients = new ArrayList<>();
final int CLIENTS = 2;
final int REQUESTS = 2;
for (int i = 0; i < CLIENTS; i++) {
HttpClient httpClient = webClientFactory.getCommonHttpClient();
clients.add(httpClient);
}
final List<String> failures = new ArrayList<>();
for (int i = 0; i < REQUESTS; i++) {
for (final HttpClient client : clients) {
workers.execute(new Runnable() {
@Override
public void run() {
try {
ContentResponse response = client.GET(TEST_URL);
if (response.getStatus() != 200) {
failures.add("Statuscode != 200");
}
} catch (InterruptedException | ExecutionException | TimeoutException e) {
failures.add("Unexpected exception:" + e.getMessage());
}
}
});
}
}
workers.shutdown();
workers.awaitTermination(120, TimeUnit.SECONDS);
if (!failures.isEmpty()) {
fail(failures.toString());
}
webClientFactory.deactivate();
}
@Ignore("only for manual test")
@Test
public void testMultiThreadedCustom() throws Exception {
webClientFactory.activate(createConfigMap(10, 200, 60, 5, 10, 60));
ThreadPoolExecutor workers = new ThreadPoolExecutor(20, 80, 60, TimeUnit.SECONDS,
new ArrayBlockingQueue<Runnable>(50 * 50));
List<HttpClient> clients = new ArrayList<>();
final int CLIENTS = 2;
final int REQUESTS = 2;
for (int i = 0; i < CLIENTS; i++) {
HttpClient httpClient = webClientFactory.createHttpClient("consumer" + i, "https://www.heise.de");
clients.add(httpClient);
}
final List<String> failures = new ArrayList<>();
for (int i = 0; i < REQUESTS; i++) {
for (final HttpClient client : clients) {
workers.execute(new Runnable() {
@Override
public void run() {
try {
ContentResponse response = client.GET(TEST_URL);
if (response.getStatus() != 200) {
failures.add("Statuscode != 200");
}
} catch (InterruptedException | ExecutionException | TimeoutException e) {
failures.add("Unexpected exception:" + e.getMessage());
}
}
});
}
}
workers.shutdown();
workers.awaitTermination(120, TimeUnit.SECONDS);
if (!failures.isEmpty()) {
fail(failures.toString());
}
for (HttpClient client : clients) {
client.stop();
}
webClientFactory.deactivate();
}
private Map<String, Object> createConfigMap(int minThreadsShared, int maxThreadsShared, int keepAliveTimeoutShared,
int minThreadsCustom, int maxThreadsCustom, int keepAliveTimeoutCustom) {
Map<String, Object> configMap = new HashMap<>();
configMap.put("minThreadsShared", minThreadsShared);
configMap.put("maxThreadsShared", maxThreadsShared);
configMap.put("keepAliveTimeoutShared", keepAliveTimeoutShared);
configMap.put("minThreadsCustom", minThreadsCustom);
configMap.put("maxThreadsCustom", maxThreadsCustom);
configMap.put("keepAliveTimeoutCustom", keepAliveTimeoutCustom);
return configMap;
}
}