KEYCLOAK-2850 Migrated ConcurrencyTest

This commit is contained in:
Stian Thorgersen 2016-04-20 10:07:16 +02:00
parent ecd3d2187f
commit 37256b6b8f
5 changed files with 2 additions and 391 deletions

View file

@ -33,8 +33,6 @@ 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;
@ -44,7 +42,7 @@ import static org.junit.Assert.*;
/**
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
*/
public class ConcurrencyTest extends AbstractClientTest {
public class ConcurrencyTest extends AbstractAdminTest {
private static final Logger log = Logger.getLogger(ConcurrencyTest.class);
@ -274,7 +272,7 @@ public class ConcurrencyTest extends AbstractClientTest {
lock.lock();
}
Keycloak keycloak = Keycloak.getInstance("http://localhost:8081/auth", "master", "admin", "admin", org.keycloak.models.Constants.ADMIN_CLI_CLIENT_ID);
Keycloak keycloak = Keycloak.getInstance(getAuthServerRoot().toString(), "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);

View file

@ -34,7 +34,6 @@ import org.keycloak.representations.idm.RealmRepresentation;
import org.keycloak.services.managers.RealmManager;
import org.keycloak.services.managers.ResourceAdminManager;
import org.keycloak.testsuite.OAuthClient;
import org.keycloak.testsuite.admin.ApiUtil;
import org.keycloak.testsuite.pages.AccountSessionsPage;
import org.keycloak.testsuite.pages.LoginPage;
import org.keycloak.testsuite.rule.AbstractKeycloakRule;

View file

@ -1,124 +0,0 @@
/*
* 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.junit.After;
import org.junit.Before;
import org.junit.ClassRule;
import org.keycloak.admin.client.Keycloak;
import org.keycloak.admin.client.resource.RealmResource;
import org.keycloak.models.Constants;
import org.keycloak.models.RealmModel;
import org.keycloak.representations.idm.ClientRepresentation;
import org.keycloak.representations.idm.IdentityProviderRepresentation;
import org.keycloak.representations.idm.RealmRepresentation;
import org.keycloak.services.managers.RealmManager;
import org.keycloak.testsuite.rule.KeycloakRule;
import java.util.*;
import static org.junit.Assert.assertArrayEquals;
/**
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
*/
public abstract class AbstractClientTest {
protected static final String REALM_NAME = "admin-client-test";
@ClassRule
public static KeycloakRule keycloakRule = new KeycloakRule();
protected Keycloak keycloak;
protected RealmResource realm;
@Before
public void before() {
keycloakRule.configure(new KeycloakRule.KeycloakSetup() {
@Override
public void config(RealmManager manager, RealmModel adminstrationRealm, RealmModel appRealm) {
appRealm.getClientByClientId("test-app").setDirectAccessGrantsEnabled(true);
}
});
keycloak = Keycloak.getInstance("http://localhost:8081/auth", "master", "admin", "admin", Constants.ADMIN_CLI_CLIENT_ID);
RealmRepresentation rep = new RealmRepresentation();
rep.setRealm(REALM_NAME);
rep.setEnabled(true);
Map<String, String> config = new HashMap<>();
config.put("from", "auto@keycloak.org");
config.put("host", "localhost");
config.put("port", "3025");
rep.setSmtpServer(config);
keycloak.realms().create(rep);
realm = keycloak.realm(REALM_NAME);
}
@After
public void after() {
for (RealmRepresentation r : keycloak.realms().findAll()) {
if (r.getRealm().equals(REALM_NAME)) {
keycloak.realm(REALM_NAME).remove();
}
}
keycloak.close();
}
public static <T> void assertNames(List<T> actual, String... expected) {
Arrays.sort(expected);
String[] actualNames = names(actual);
assertArrayEquals("Expected: " + Arrays.toString(expected) + ", was: " + Arrays.toString(actualNames), expected, actualNames);
}
public static <T> List<T> sort(List<T> list) {
Collections.sort(list, new Comparator<Object>() {
@Override
public int compare(Object o1, Object o2) {
return name(o1).compareTo(name(o2));
}
});
return list;
}
public static <T> String[] names(List<T> list) {
String[] names = new String[list.size()];
for (int i = 0; i < list.size(); i++) {
names[i] = name(list.get(i));
}
Arrays.sort(names);
return names;
}
public static String name(Object o1) {
if (o1 instanceof RealmRepresentation) {
return ((RealmRepresentation) o1).getRealm();
} else if (o1 instanceof ClientRepresentation) {
return ((ClientRepresentation) o1).getClientId();
} else if (o1 instanceof IdentityProviderRepresentation) {
return ((IdentityProviderRepresentation) o1).getAlias();
}
throw new IllegalArgumentException();
}
}

View file

@ -1,50 +0,0 @@
/*
* 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.keycloak.admin.client.resource.ClientResource;
import org.keycloak.admin.client.resource.RealmResource;
import org.keycloak.representations.idm.ClientRepresentation;
import javax.ws.rs.core.Response;
import java.net.URI;
/**
* Created by st on 28.05.15.
*/
public class ApiUtil {
public static String getCreatedId(Response response) {
URI location = response.getLocation();
if (location == null) {
return null;
}
String path = location.getPath();
return path.substring(path.lastIndexOf('/') + 1);
}
public static ClientResource findClientByClientId(RealmResource realm, String clientId) {
for (ClientRepresentation c : realm.clients().findAll()) {
if (c.getClientId().equals(clientId)) {
return realm.clients().get(c.getId());
}
}
return null;
}
}

View file

@ -1,212 +0,0 @@
/*
* 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.BeforeClass;
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.models.ClientModel;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.KeycloakSessionFactory;
import org.keycloak.models.RealmModel;
import org.keycloak.representations.idm.ClientRepresentation;
import org.keycloak.representations.idm.GroupRepresentation;
import org.keycloak.representations.idm.RoleRepresentation;
import org.keycloak.services.DefaultKeycloakSessionFactory;
import org.keycloak.services.resources.KeycloakApplication;
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.atomic.AtomicReference;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
/**
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
*/
@Ignore
public class ClusteredConcurrencyTest {
private static final Logger log = Logger.getLogger(ClusteredConcurrencyTest.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;
public static DefaultKeycloakSessionFactory node1factory;
public static DefaultKeycloakSessionFactory node2factory;
public static DefaultKeycloakSessionFactory[] nodes = new DefaultKeycloakSessionFactory[2];
@BeforeClass
public static void initKeycloak() throws Exception {
System.setProperty("keycloak.connectionsInfinispan.clustered", "true");
System.setProperty("keycloak.connectionsInfinispan.async", "false");
KeycloakApplication.loadConfig();
node1factory = new DefaultKeycloakSessionFactory();
node1factory.init();
nodes[0] = node1factory;
node2factory = new DefaultKeycloakSessionFactory();
node2factory.init();
nodes[1] = node2factory;
KeycloakSession session = nodes[0].create();
session.getTransaction().begin();
session.realms().createRealm("testrealm");
session.getTransaction().commit();
session = nodes[1].create();
session.getTransaction().begin();
RealmModel realm = session.realms().getRealmByName("testrealm");
Assert.assertNotNull(realm);
session.getTransaction().commit();
}
@Test
public void createClient() throws Throwable {
System.out.println("***************************");
long start = System.currentTimeMillis();
run(new KeycloakRunnable() {
@Override
public void run(int threadNum, int iterationNum) {
String name = "c-" + threadNum + "-" + iterationNum;
int node1 = threadNum % 2;
int node2 = 0;
if (node1 == 0) node2 = 1;
String id = null;
{
KeycloakSession session = nodes[node1].create();
session.getTransaction().begin();
RealmModel realm = session.realms().getRealmByName("testrealm");
ClientModel client = realm.addClient(name);
id = client.getId();
session.getTransaction().commit();
}
{
KeycloakSession session = nodes[node2].create();
session.getTransaction().begin();
RealmModel realm = session.realms().getRealmByName("testrealm");
boolean found = false;
for (ClientModel client : realm.getClients()) {
if (client.getId().equals(id)) {
found = true;
}
}
session.getTransaction().commit();
if (!found) {
fail("Client " + name + " not found in client list");
}
}
{
KeycloakSession session = nodes[node1].create();
session.getTransaction().begin();
RealmModel realm = session.realms().getRealmByName("testrealm");
boolean found = false;
for (ClientModel client : realm.getClients()) {
if (client.getId().equals(id)) {
found = true;
}
}
session.getTransaction().commit();
if (!found) {
fail("Client " + name + " not found in client list");
}
}
}
});
long end = System.currentTimeMillis() - start;
System.out.println("createClient took " + end);
}
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();
}
for (int i = 0; i < numIterationsPerThread && latch.getCount() > 0; i++) {
log.infov("thread {0}, iteration {1}", threadNum, i);
runnable.run(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(int threadNum, int iterationNum);
}
}