/
ConcurrencyTest.java
executable file
·317 lines (275 loc) · 11.6 KB
/
ConcurrencyTest.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
* Copyright 2016 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.keycloak.testsuite.admin;
import org.jboss.logging.Logger;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import org.keycloak.admin.client.Keycloak;
import org.keycloak.admin.client.resource.ClientResource;
import org.keycloak.admin.client.resource.RealmResource;
import org.keycloak.representations.idm.ClientRepresentation;
import org.keycloak.representations.idm.GroupRepresentation;
import org.keycloak.representations.idm.RoleRepresentation;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.core.Response;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import static org.junit.Assert.*;
/**
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
*/
public class ConcurrencyTest extends AbstractClientTest {
private static final Logger log = Logger.getLogger(ConcurrencyTest.class);
private static final int DEFAULT_THREADS = 10;
private static final int DEFAULT_ITERATIONS = 100;
// If enabled only one request is allowed at the time. Useful for checking that test is working.
private static final boolean SYNCHRONIZED = false;
boolean passedCreateClient = false;
boolean passedCreateRole = false;
//@Test
public void testAllConcurrently() throws Throwable {
Thread client = new Thread(new Runnable() {
@Override
public void run() {
try {
createClient();
passedCreateClient = true;
} catch (Throwable throwable) {
throw new RuntimeException(throwable);
}
}
});
Thread role = new Thread(new Runnable() {
@Override
public void run() {
try {
createRole();
passedCreateRole = true;
} catch (Throwable throwable) {
throw new RuntimeException(throwable);
}
}
});
client.start();
role.start();
client.join();
role.join();
Assert.assertTrue(passedCreateClient);
Assert.assertTrue(passedCreateRole);
}
@Test
public void createClient() throws Throwable {
System.out.println("***************************");
long start = System.currentTimeMillis();
run(new KeycloakRunnable() {
@Override
public void run(Keycloak keycloak, RealmResource realm, int threadNum, int iterationNum) {
String name = "c-" + threadNum + "-" + iterationNum;
ClientRepresentation c = new ClientRepresentation();
c.setClientId(name);
Response response = realm.clients().create(c);
String id = ApiUtil.getCreatedId(response);
response.close();
c = realm.clients().get(id).toRepresentation();
assertNotNull(c);
boolean found = false;
for (ClientRepresentation r : realm.clients().findAll()) {
if (r.getClientId().equals(name)) {
found = true;
break;
}
}
if (!found) {
fail("Client " + name + " not found in client list");
}
}
});
long end = System.currentTimeMillis() - start;
System.out.println("createClient took " + end);
}
@Test
public void createGroup() throws Throwable {
System.out.println("***************************");
long start = System.currentTimeMillis();
run(new KeycloakRunnable() {
@Override
public void run(Keycloak keycloak, RealmResource realm, int threadNum, int iterationNum) {
String name = "c-" + threadNum + "-" + iterationNum;
GroupRepresentation c = new GroupRepresentation();
c.setName(name);
Response response = realm.groups().add(c);
String id = ApiUtil.getCreatedId(response);
response.close();
c = realm.groups().group(id).toRepresentation();
assertNotNull(c);
boolean found = false;
for (GroupRepresentation r : realm.groups().groups()) {
if (r.getName().equals(name)) {
found = true;
break;
}
}
if (!found) {
fail("Group " + name + " not found in group list");
}
}
});
long end = System.currentTimeMillis() - start;
System.out.println("createGroup took " + end);
}
@Test
@Ignore
public void createRemoveClient() throws Throwable {
// FYI< this will fail as HSQL seems to be trying to perform table locks.
System.out.println("***************************");
long start = System.currentTimeMillis();
run(new KeycloakRunnable() {
@Override
public void run(Keycloak keycloak, RealmResource realm, int threadNum, int iterationNum) {
String name = "c-" + threadNum + "-" + iterationNum;
ClientRepresentation c = new ClientRepresentation();
c.setClientId(name);
Response response = realm.clients().create(c);
String id = ApiUtil.getCreatedId(response);
response.close();
c = realm.clients().get(id).toRepresentation();
assertNotNull(c);
boolean found = false;
for (ClientRepresentation r : realm.clients().findAll()) {
if (r.getClientId().equals(name)) {
found = true;
break;
}
}
if (!found) {
fail("Client " + name + " not found in client list");
}
realm.clients().get(id).remove();
try {
c = realm.clients().get(id).toRepresentation();
fail("Client " + name + " should not be found. Should throw a 404");
} catch (NotFoundException e) {
}
found = false;
for (ClientRepresentation r : realm.clients().findAll()) {
if (r.getClientId().equals(name)) {
found = true;
break;
}
}
Assert.assertFalse("Client " + name + " should not be in client list", found);
}
});
long end = System.currentTimeMillis() - start;
System.out.println("createClient took " + end);
}
@Test
public void createRole() throws Throwable {
long start = System.currentTimeMillis();
run(new KeycloakRunnable() {
@Override
public void run(Keycloak keycloak, RealmResource realm, int threadNum, int iterationNum) {
String name = "r-" + threadNum + "-" + iterationNum;
RoleRepresentation r = new RoleRepresentation(name, null, false);
realm.roles().create(r);
assertNotNull(realm.roles().get(name).toRepresentation());
}
});
long end = System.currentTimeMillis() - start;
System.out.println("createRole took " + end);
}
@Test
public void createClientRole() throws Throwable {
long start = System.currentTimeMillis();
ClientRepresentation c = new ClientRepresentation();
c.setClientId("client");
Response response = realm.clients().create(c);
final String clientId = ApiUtil.getCreatedId(response);
response.close();
System.out.println("*********************************************");
run(new KeycloakRunnable() {
@Override
public void run(Keycloak keycloak, RealmResource realm, int threadNum, int iterationNum) {
String name = "r-" + threadNum + "-" + iterationNum;
RoleRepresentation r = new RoleRepresentation(name, null, false);
ClientResource client = realm.clients().get(clientId);
client.roles().create(r);
assertNotNull(client.roles().get(name).toRepresentation());
}
});
long end = System.currentTimeMillis() - start;
System.out.println("createClientRole took " + end);
System.out.println("*********************************************");
}
private void run(final KeycloakRunnable runnable) throws Throwable {
run(runnable, DEFAULT_THREADS, DEFAULT_ITERATIONS);
}
private void run(final KeycloakRunnable runnable, final int numThreads, final int numIterationsPerThread) throws Throwable {
final CountDownLatch latch = new CountDownLatch(numThreads);
final AtomicReference<Throwable> failed = new AtomicReference();
final List<Thread> threads = new LinkedList<>();
final Lock lock = SYNCHRONIZED ? new ReentrantLock() : null;
for (int t = 0; t < numThreads; t++) {
final int threadNum = t;
Thread thread = new Thread() {
@Override
public void run() {
try {
if (lock != null) {
lock.lock();
}
Keycloak keycloak = Keycloak.getInstance("http://localhost:8081/auth", "master", "admin", "admin", org.keycloak.models.Constants.ADMIN_CLI_CLIENT_ID);
RealmResource realm = keycloak.realm(REALM_NAME);
for (int i = 0; i < numIterationsPerThread && latch.getCount() > 0; i++) {
log.infov("thread {0}, iteration {1}", threadNum, i);
runnable.run(keycloak, realm, threadNum, i);
}
latch.countDown();
} catch (Throwable t) {
failed.compareAndSet(null, t);
while (latch.getCount() > 0) {
latch.countDown();
}
} finally {
if (lock != null) {
lock.unlock();
}
}
}
};
thread.start();
threads.add(thread);
}
latch.await();
for (Thread t : threads) {
t.join();
}
if (failed.get() != null) {
throw failed.get();
}
}
interface KeycloakRunnable {
void run(Keycloak keycloak, RealmResource realm, int threadNum, int iterationNum);
}
}