Permalink
Browse files

fix running DeafSelectorProvider in test suite

  • Loading branch information...
1 parent d6d3eca commit 8ebb02947b74ff9e6130a438abaa378e7eedfac5 Jax Law committed Nov 27, 2012
View
@@ -17,3 +17,4 @@ eclipse-classes
.arc
*.releaseBackup
release.properties
+dependency-reduced-pom.xml
@@ -0,0 +1,85 @@
+package com.facebook.nifty.server;
+
+import java.io.IOException;
+import java.net.ProtocolFamily;
+import java.nio.channels.Channel;
+import java.nio.channels.DatagramChannel;
+import java.nio.channels.Pipe;
+import java.nio.channels.ServerSocketChannel;
+import java.nio.channels.SocketChannel;
+import java.nio.channels.spi.AbstractSelector;
+import java.nio.channels.spi.SelectorProvider;
+
+/**
+ * SelectorProvider.provider() caches whatever initialized statically, so
+ * when running in a test suite simply clearing the property
+ * "java.nio.channels.spi.SelectorProvider" would not work to restore
+ * other tests to operate on normal selector provider.
+ *
+ * This is a hack to make it work.
+ */
+public class DelegateSelectorProvider extends SelectorProvider {
+ private static final SelectorProvider original ;
+ private static final SelectorProvider deaf = new DeafSelectorProvider();
+ private static SelectorProvider delegate ;
+
+ static {
+ try {
+ // hack to work around compiler complaints about sun.nio.ch.PollSelectorProvider
+ // being proprietary
+ delegate = original = (SelectorProvider) Class.forName("sun.nio.ch.PollSelectorProvider").newInstance();
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ System.setProperty(
+ "java.nio.channels.spi.SelectorProvider",
+ DelegateSelectorProvider.class.getName()
+ );
+ }
+
+ public static void init() {
+ }
+
+ public static void makeDeaf() {
+ delegate = deaf ;
+ }
+
+ public static void makeUndeaf() {
+ delegate = original;
+ }
+
+ @Override
+ public DatagramChannel openDatagramChannel() throws IOException {
+ return delegate.openDatagramChannel();
+ }
+
+ @Override
+ public DatagramChannel openDatagramChannel(ProtocolFamily family) throws IOException {
+ return delegate.openDatagramChannel(family);
+ }
+
+ @Override
+ public Pipe openPipe() throws IOException {
+ return delegate.openPipe();
+ }
+
+ @Override
+ public AbstractSelector openSelector() throws IOException {
+ return delegate.openSelector();
+ }
+
+ @Override
+ public ServerSocketChannel openServerSocketChannel() throws IOException {
+ return delegate.openServerSocketChannel();
+ }
+
+ @Override
+ public SocketChannel openSocketChannel() throws IOException {
+ return delegate.openSocketChannel();
+ }
+
+ @Override
+ public Channel inheritedChannel() throws IOException {
+ return delegate.inheritedChannel();
+ }
+}
@@ -20,7 +20,10 @@
import com.google.common.base.Throwables;
import com.google.common.util.concurrent.ListenableFuture;
import io.airlift.units.Duration;
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
+import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
@@ -39,23 +42,27 @@
private static final Duration TEST_WRITE_TIMEOUT = new Duration(500, TimeUnit.MILLISECONDS);
@BeforeTest(alwaysRun = true)
- public void setup() throws IOException
- {
- System.setProperty("java.nio.channels.spi.SelectorProvider", DeafSelectorProvider.class.getName());
+ public void init() {
+ // must load DelegateSelectorProvider before entire test suite to
+ // properly wire up selector provider.
+ DelegateSelectorProvider.init();
}
- @AfterTest(alwaysRun = true)
- public void tearDown()
- {
- System.clearProperty("java.nio.channels.spi.SelectorProvider");
+ @BeforeMethod(alwaysRun = true)
+ public void setup() {
+ DelegateSelectorProvider.makeDeaf();
+ }
+
+ @AfterMethod(alwaysRun = true)
+ public void tearDown() {
+ DelegateSelectorProvider.makeUndeaf();
}
@Test(timeOut = 2000)
public void testSyncConnectTimeout() throws ConnectException, IOException
{
ServerSocket serverSocket = new ServerSocket(0);
int port = serverSocket.getLocalPort();
-
final NiftyClient client = new NiftyClient();
try {
client.connectSync(new InetSocketAddress(port),

0 comments on commit 8ebb029

Please sign in to comment.