You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by lg...@apache.org on 2016/02/20 20:04:14 UTC

mina-sshd git commit: Added more listener related tests to DefaultSshFutureTest

Repository: mina-sshd
Updated Branches:
  refs/heads/master ddfe6f6b6 -> b0cfc334d


Added more listener related tests to DefaultSshFutureTest


Project: http://git-wip-us.apache.org/repos/asf/mina-sshd/repo
Commit: http://git-wip-us.apache.org/repos/asf/mina-sshd/commit/b0cfc334
Tree: http://git-wip-us.apache.org/repos/asf/mina-sshd/tree/b0cfc334
Diff: http://git-wip-us.apache.org/repos/asf/mina-sshd/diff/b0cfc334

Branch: refs/heads/master
Commit: b0cfc334d135fb4663e7cd8e6e8fed6ccf928eb3
Parents: ddfe6f6
Author: Lyor Goldstein <ly...@gmail.com>
Authored: Sat Feb 20 21:04:46 2016 +0200
Committer: Lyor Goldstein <ly...@gmail.com>
Committed: Sat Feb 20 21:04:46 2016 +0200

----------------------------------------------------------------------
 .../sshd/common/future/DefaultSshFuture.java    |  19 +++
 .../common/future/DefaultSshFutureTest.java     | 142 +++++++++++++++++++
 .../apache/sshd/common/future/FutureTest.java   |  56 --------
 3 files changed, 161 insertions(+), 56 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/b0cfc334/sshd-core/src/main/java/org/apache/sshd/common/future/DefaultSshFuture.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/future/DefaultSshFuture.java b/sshd-core/src/main/java/org/apache/sshd/common/future/DefaultSshFuture.java
index 4c35210..b2763e1 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/future/DefaultSshFuture.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/future/DefaultSshFuture.java
@@ -104,6 +104,25 @@ public class DefaultSshFuture<T extends SshFuture> extends AbstractSshFuture<T>
         notifyListeners();
     }
 
+    public int getNumRegisteredListeners() {
+        synchronized (lock) {
+            if (listeners == null) {
+                return 0;
+            } else if (listeners instanceof SshFutureListener) {
+                return 1;
+            } else {
+                int l = Array.getLength(listeners);
+                int count = 0;
+                for (int i = 0; i < l; i++) {
+                    if (Array.get(listeners, i) != null) {
+                        count++;
+                    }
+                }
+                return count;
+            }
+        }
+    }
+
     /**
      * @return The result of the asynchronous operation - or {@code null}
      * if none set.

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/b0cfc334/sshd-core/src/test/java/org/apache/sshd/common/future/DefaultSshFutureTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/common/future/DefaultSshFutureTest.java b/sshd-core/src/test/java/org/apache/sshd/common/future/DefaultSshFutureTest.java
new file mode 100644
index 0000000..6182f55
--- /dev/null
+++ b/sshd-core/src/test/java/org/apache/sshd/common/future/DefaultSshFutureTest.java
@@ -0,0 +1,142 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.sshd.common.future;
+
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.apache.sshd.util.test.BaseTestSupport;
+import org.junit.FixMethodOrder;
+import org.junit.Test;
+import org.junit.runners.MethodSorters;
+import org.mockito.Mockito;
+
+/**
+ */
+@FixMethodOrder(MethodSorters.NAME_ASCENDING)
+public class DefaultSshFutureTest extends BaseTestSupport {
+    public DefaultSshFutureTest() {
+        super();
+    }
+
+    @Test
+    @SuppressWarnings("rawtypes")
+    public void testAwaitUninterrupted() {
+        final DefaultSshFuture future = new DefaultSshFuture(null);
+        final Object expected = new Object();
+        new Thread() {
+            @Override
+            public void run() {
+                try {
+                    Thread.sleep(TimeUnit.SECONDS.toMillis(2L));
+                } catch (InterruptedException e) {
+                    throw new RuntimeException(e);
+                }
+                future.setValue(expected);
+            }
+        }.start();
+
+        future.awaitUninterruptibly();
+        assertSame("Mismatched signalled value", expected, future.getValue());
+    }
+
+    @Test
+    @SuppressWarnings({ "rawtypes", "unchecked" })
+    public void testNotifyMultipleListeners() {
+        final DefaultSshFuture future = new DefaultSshFuture(null);
+        final AtomicInteger listenerCount = new AtomicInteger(0);
+        final Object expected = new Object();
+        final SshFutureListener listener = new SshFutureListener() {
+            @Override
+            public void operationComplete(SshFuture f) {
+                assertSame("Mismatched future instance", future, f);
+                assertSame("Mismatched value object", expected, future.getValue());
+                listenerCount.incrementAndGet();
+            }
+        };
+
+        final int NUM_LISTENERS = Byte.SIZE;
+        for (int index = 0; index < NUM_LISTENERS; index++) {
+            future.addListener(listener);
+        }
+
+        future.setValue(expected);
+        assertEquals("Mismatched listeners invocation count", NUM_LISTENERS, listenerCount.get());
+    }
+
+    @Test
+    @SuppressWarnings({ "rawtypes", "unchecked" })
+    public void testListenerInvokedDirectlyAfterResultSet() {
+        final DefaultSshFuture future = new DefaultSshFuture(null);
+        final AtomicInteger listenerCount = new AtomicInteger(0);
+        final Object expected = new Object();
+        final SshFutureListener listener = new SshFutureListener() {
+            @Override
+            public void operationComplete(SshFuture f) {
+                assertSame("Mismatched future instance", future, f);
+                assertSame("Mismatched value object", expected, future.getValue());
+                listenerCount.incrementAndGet();
+            }
+        };
+        future.setValue(expected);
+
+        future.addListener(listener);
+        assertEquals("Mismatched number of registered listeners", 0, future.getNumRegisteredListeners());
+        assertEquals("Listener not invoked", 1, listenerCount.get());
+    }
+
+    @Test
+    @SuppressWarnings({ "rawtypes", "unchecked" })
+    public void testAddAndRemoveRegisteredListenersBeforeResultSet() {
+        DefaultSshFuture future = new DefaultSshFuture(null);
+        SshFutureListener listener = Mockito.mock(SshFutureListener.class);
+        for (int index = 1; index <= Byte.SIZE; index++) {
+            future.addListener(listener);
+            assertEquals("Mismatched number of added listeners", index, future.getNumRegisteredListeners());
+        }
+
+        for (int index = future.getNumRegisteredListeners() - 1; index >= 0; index--) {
+            future.removeListener(listener);
+            assertEquals("Mismatched number of remaining listeners", index, future.getNumRegisteredListeners());
+        }
+    }
+
+    @Test
+    @SuppressWarnings({ "rawtypes", "unchecked" })
+    public void testListenerNotRemovedIfResultSet() {
+        final DefaultSshFuture future = new DefaultSshFuture(null);
+        final AtomicInteger listenerCount = new AtomicInteger(0);
+        final Object expected = new Object();
+        final SshFutureListener listener = new SshFutureListener() {
+            @Override
+            public void operationComplete(SshFuture f) {
+                assertSame("Mismatched future instance", future, f);
+                assertSame("Mismatched value object", expected, future.getValue());
+                listenerCount.incrementAndGet();
+            }
+        };
+        future.addListener(listener);
+        future.setValue(expected);
+        assertEquals("Mismatched number of registered listeners", 1, future.getNumRegisteredListeners());
+        assertEquals("Listener not invoked", 1, listenerCount.get());
+
+        future.removeListener(listener);
+        assertEquals("Mismatched number of remaining listeners", 1, future.getNumRegisteredListeners());
+    }
+}

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/b0cfc334/sshd-core/src/test/java/org/apache/sshd/common/future/FutureTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/common/future/FutureTest.java b/sshd-core/src/test/java/org/apache/sshd/common/future/FutureTest.java
deleted file mode 100644
index c938614..0000000
--- a/sshd-core/src/test/java/org/apache/sshd/common/future/FutureTest.java
+++ /dev/null
@@ -1,56 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you 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.apache.sshd.common.future;
-
-import java.util.concurrent.TimeUnit;
-
-import org.apache.sshd.util.test.BaseTestSupport;
-import org.junit.FixMethodOrder;
-import org.junit.Test;
-import org.junit.runners.MethodSorters;
-
-/**
- */
-@FixMethodOrder(MethodSorters.NAME_ASCENDING)
-public class FutureTest extends BaseTestSupport {
-    public FutureTest() {
-        super();
-    }
-
-    @Test
-    @SuppressWarnings("rawtypes")
-    public void testAwaitUnint() {
-        final DefaultSshFuture future = new DefaultSshFuture(null);
-        final Object expected = new Object();
-        new Thread() {
-            @Override
-            public void run() {
-                try {
-                    Thread.sleep(TimeUnit.SECONDS.toMillis(2L));
-                } catch (InterruptedException e) {
-                    throw new RuntimeException(e);
-                }
-                future.setValue(expected);
-            }
-        }.start();
-
-        future.awaitUninterruptibly();
-        assertSame("Mismatched signalled value", expected, future.getValue());
-    }
-}