Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add test for concurrent access

  • Loading branch information...
commit efff44a167e5ad8a3a5e115c16126017d147b28c 1 parent 57f0a3c
@stuartwdouglas stuartwdouglas authored
View
87 src/test/java/org/jboss/naming/remote/ClientConnectionTest.java
@@ -21,17 +21,15 @@
*/
package org.jboss.naming.remote;
-import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.URI;
-import java.security.Principal;
-import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
+
import javax.naming.Binding;
import javax.naming.Context;
import javax.naming.InitialContext;
@@ -39,10 +37,7 @@
import javax.naming.NameClassPair;
import javax.naming.NameNotFoundException;
import javax.naming.NamingEnumeration;
-import javax.security.auth.callback.Callback;
-import javax.security.auth.callback.CallbackHandler;
-import javax.security.auth.callback.NameCallback;
-import javax.security.auth.callback.UnsupportedCallbackException;
+
import org.jboss.ejb.client.ContextSelector;
import org.jboss.ejb.client.EJBClientContext;
import org.jboss.naming.remote.client.InitialContextFactory;
@@ -54,30 +49,22 @@
import org.jboss.remoting3.Endpoint;
import org.jboss.remoting3.Remoting;
import org.jboss.remoting3.remote.RemoteConnectionProviderFactory;
-import org.jboss.remoting3.security.AuthorizingCallbackHandler;
-import org.jboss.remoting3.security.ServerAuthenticationProvider;
-import org.jboss.remoting3.security.SimpleUserInfo;
-import org.jboss.remoting3.security.UserInfo;
import org.jboss.remoting3.spi.NetworkServerProvider;
import org.junit.AfterClass;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
import org.junit.BeforeClass;
import org.junit.Test;
import org.xnio.IoFuture;
import org.xnio.OptionMap;
import org.xnio.Options;
-import static org.xnio.Options.SASL_MECHANISMS;
-import static org.xnio.Options.SASL_POLICY_NOANONYMOUS;
-import static org.xnio.Options.SASL_PROPERTIES;
-import static org.xnio.Options.SSL_ENABLED;
-import org.xnio.Property;
-import org.xnio.Sequence;
import org.xnio.Xnio;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+import static org.xnio.Options.SSL_ENABLED;
+
/**
* @author John Bailey
*/
@@ -95,9 +82,9 @@ public static void beforeClass() throws Exception {
final NetworkServerProvider nsp = endpoint.getConnectionProviderInterface("remote", NetworkServerProvider.class);
final SocketAddress bindAddress = new InetSocketAddress("localhost", 7999);
- final OptionMap serverOptions = createOptionMap();
+ final OptionMap serverOptions = TestUtils.createOptionMap();
- nsp.createServer(bindAddress, serverOptions, new DefaultAuthenticationHandler(), null);
+ nsp.createServer(bindAddress, serverOptions, new TestUtils.DefaultAuthenticationHandler(), null);
server = new RemoteNamingService(localContext, Executors.newFixedThreadPool(10));
server.start(endpoint);
@@ -396,7 +383,7 @@ public void testWithExistingConnection() throws Exception {
final Endpoint endpoint = Remoting.createEndpoint("RemoteNaming", xnio, OptionMap.EMPTY);
endpoint.addConnectionProvider("remote", new RemoteConnectionProviderFactory(), OptionMap.create(SSL_ENABLED, false));
- final IoFuture<Connection> futureConnection = endpoint.connect(new URI("remote://localhost:7999"), OptionMap.create(Options.SASL_POLICY_NOANONYMOUS, false), new AnonymousCallbackHandler());
+ final IoFuture<Connection> futureConnection = endpoint.connect(new URI("remote://localhost:7999"), OptionMap.create(Options.SASL_POLICY_NOANONYMOUS, false), new TestUtils.AnonymousCallbackHandler());
final Connection connection = IoFutureHelper.get(futureConnection, 1000, TimeUnit.MILLISECONDS);
final Properties env = new Properties();
@@ -417,7 +404,7 @@ public void testSetupEjb() throws Exception {
final Endpoint endpoint = Remoting.createEndpoint("RemoteNaming", xnio, OptionMap.EMPTY);
endpoint.addConnectionProvider("remote", new RemoteConnectionProviderFactory(), OptionMap.create(SSL_ENABLED, false));
- final IoFuture<Connection> futureConnection = endpoint.connect(new URI("remote://localhost:7999"), OptionMap.create(Options.SASL_POLICY_NOANONYMOUS, false), new AnonymousCallbackHandler());
+ final IoFuture<Connection> futureConnection = endpoint.connect(new URI("remote://localhost:7999"), OptionMap.create(Options.SASL_POLICY_NOANONYMOUS, false), new TestUtils.AnonymousCallbackHandler());
final Connection connection = IoFutureHelper.get(futureConnection, 1000, TimeUnit.MILLISECONDS);
final ContextSelector<EJBClientContext> temp = new MockSelector();
@@ -437,54 +424,6 @@ public void testSetupEjb() throws Exception {
endpoint.close();
}
- public static final String ANONYMOUS = "ANONYMOUS";
-
- private static OptionMap createOptionMap() {
- OptionMap.Builder builder = OptionMap.builder();
- builder.set(SSL_ENABLED, false);
- builder.set(SASL_MECHANISMS, Sequence.<String>of(ANONYMOUS));
- builder.set(SASL_PROPERTIES, Sequence.<Property>empty());
- builder.set(SASL_POLICY_NOANONYMOUS, false);
-
- return builder.getMap();
- }
-
- private static class DefaultAuthenticationHandler implements ServerAuthenticationProvider {
- @Override
- public AuthorizingCallbackHandler getCallbackHandler(String mechanismName) {
- if (mechanismName.equals(ANONYMOUS)) {
- return new AuthorizingCallbackHandler() {
- public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
- for (Callback current : callbacks) {
- throw new UnsupportedCallbackException(current, "ANONYMOUS mechanism so not expecting a callback");
- }
- }
-
- @Override
- public UserInfo createUserInfo(Collection<Principal> remotingPrincipals) throws IOException {
- if (remotingPrincipals == null) {
- return null;
- }
- return new SimpleUserInfo(remotingPrincipals);
- }
- };
- }
- return null;
- }
- }
-
- private class AnonymousCallbackHandler implements CallbackHandler {
- public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
- for (Callback current : callbacks) {
- if (current instanceof NameCallback) {
- NameCallback ncb = (NameCallback) current;
- ncb.setName(ANONYMOUS);
- } else {
- throw new UnsupportedCallbackException(current);
- }
- }
- }
- }
private class MockSelector implements ContextSelector<EJBClientContext> {
public EJBClientContext getCurrent() {
View
90 src/test/java/org/jboss/naming/remote/ConcurrentConnectionTest.java
@@ -0,0 +1,90 @@
+package org.jboss.naming.remote;
+
+import java.net.InetSocketAddress;
+import java.net.SocketAddress;
+import java.util.Properties;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeoutException;
+
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+
+import org.jboss.naming.remote.server.RemoteNamingService;
+import org.jboss.remoting3.Endpoint;
+import org.jboss.remoting3.Remoting;
+import org.jboss.remoting3.remote.RemoteConnectionProviderFactory;
+import org.jboss.remoting3.spi.NetworkServerProvider;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.xnio.OptionMap;
+import org.xnio.Xnio;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * @author Stuart Douglas
+ */
+public class ConcurrentConnectionTest {
+
+ private static RemoteNamingService server;
+
+ private static final Context localContext = new MockContext();
+
+ @BeforeClass
+ public static void beforeClass() throws Exception {
+ final Xnio xnio = Xnio.getInstance();
+ final Endpoint endpoint = Remoting.createEndpoint("RemoteNaming", xnio, OptionMap.EMPTY);
+ endpoint.addConnectionProvider("remote", new RemoteConnectionProviderFactory(), OptionMap.EMPTY);
+
+ final NetworkServerProvider nsp = endpoint.getConnectionProviderInterface("remote", NetworkServerProvider.class);
+ final SocketAddress bindAddress = new InetSocketAddress("localhost", 7999);
+ final OptionMap serverOptions = TestUtils.createOptionMap();
+
+ nsp.createServer(bindAddress, serverOptions, new TestUtils.DefaultAuthenticationHandler(), null);
+ server = new RemoteNamingService(localContext, Executors.newFixedThreadPool(10));
+ server.start(endpoint);
+ }
+
+ @AfterClass
+ public static void afterClass() throws Exception {
+ server.stop();
+ }
+ @Test
+ public void multiThreadedStressTest() throws NamingException, ExecutionException, InterruptedException, TimeoutException {
+ ExecutorService executorService = Executors.newFixedThreadPool(10);
+ localContext.bind("test", "TestValue");
+ try {
+ final Future[] futures = new Future[1000];
+ for (int i = 0; i < 1000; ++i) {
+ futures[i] = executorService.submit(new Runnable() {
+ @Override
+ public void run() {
+ try {
+ Properties env = new Properties();
+ env.put(Context.INITIAL_CONTEXT_FACTORY, org.jboss.naming.remote.client.InitialContextFactory.class.getName());
+ env.put(Context.PROVIDER_URL, "remote://localhost:7999");
+ env.put("jboss.naming.client.ejb.context", "false");
+ InitialContext context = new InitialContext(env);
+ assertEquals("TestValue", context.lookup("test"));
+ context.close();
+ } catch (NamingException e) {
+ throw new RuntimeException(e);
+ }
+ }
+ });
+ }
+ for(int i = 0; i < 1000; ++i) {
+ futures[i].get();
+ }
+ } finally {
+ executorService.shutdownNow();
+ localContext.unbind("test");
+ }
+ }
+
+}
View
79 src/test/java/org/jboss/naming/remote/TestUtils.java
@@ -0,0 +1,79 @@
+package org.jboss.naming.remote;
+
+import java.io.IOException;
+import java.security.Principal;
+import java.util.Collection;
+
+import javax.security.auth.callback.Callback;
+import javax.security.auth.callback.CallbackHandler;
+import javax.security.auth.callback.NameCallback;
+import javax.security.auth.callback.UnsupportedCallbackException;
+
+import org.jboss.remoting3.security.AuthorizingCallbackHandler;
+import org.jboss.remoting3.security.ServerAuthenticationProvider;
+import org.jboss.remoting3.security.SimpleUserInfo;
+import org.jboss.remoting3.security.UserInfo;
+import org.xnio.OptionMap;
+import org.xnio.Property;
+import org.xnio.Sequence;
+
+import static org.xnio.Options.SASL_MECHANISMS;
+import static org.xnio.Options.SASL_POLICY_NOANONYMOUS;
+import static org.xnio.Options.SASL_PROPERTIES;
+import static org.xnio.Options.SSL_ENABLED;
+
+/**
+ * @author Stuart Douglas
+ */
+public class TestUtils {
+
+ public static final String ANONYMOUS = "ANONYMOUS";
+
+ public static OptionMap createOptionMap() {
+ OptionMap.Builder builder = OptionMap.builder();
+ builder.set(SSL_ENABLED, false);
+ builder.set(SASL_MECHANISMS, Sequence.<String>of(ANONYMOUS));
+ builder.set(SASL_PROPERTIES, Sequence.<Property>empty());
+ builder.set(SASL_POLICY_NOANONYMOUS, false);
+
+ return builder.getMap();
+ }
+
+ public static class DefaultAuthenticationHandler implements ServerAuthenticationProvider {
+ @Override
+ public AuthorizingCallbackHandler getCallbackHandler(String mechanismName) {
+ if (mechanismName.equals(ANONYMOUS)) {
+ return new AuthorizingCallbackHandler() {
+ public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
+ for (Callback current : callbacks) {
+ throw new UnsupportedCallbackException(current, "ANONYMOUS mechanism so not expecting a callback");
+ }
+ }
+
+ @Override
+ public UserInfo createUserInfo(Collection<Principal> remotingPrincipals) throws IOException {
+ if (remotingPrincipals == null) {
+ return null;
+ }
+ return new SimpleUserInfo(remotingPrincipals);
+ }
+ };
+ }
+ return null;
+ }
+ }
+
+ public static class AnonymousCallbackHandler implements CallbackHandler {
+ public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
+ for (Callback current : callbacks) {
+ if (current instanceof NameCallback) {
+ NameCallback ncb = (NameCallback) current;
+ ncb.setName(ANONYMOUS);
+ } else {
+ throw new UnsupportedCallbackException(current);
+ }
+ }
+ }
+ }
+
+}

0 comments on commit efff44a

Please sign in to comment.
Something went wrong with that request. Please try again.