Browse Source

ZOOKEEPER-1730. Make ZooKeeper easier to test - support simulating a session expiration (Jordan Zimmerman via michim)

git-svn-id: https://svn.apache.org/repos/asf/zookeeper/trunk@1583513 13f79535-47bb-0310-9956-ffa450edef68
Michi Mutsuzaki 11 years ago
parent
commit
2b8f98be74

+ 3 - 0
CHANGES.txt

@@ -833,6 +833,9 @@ IMPROVEMENTS:
   write new config to disk or create new connections (Alexander Shraer via
   michim)
 
+  ZOOKEEPER-1730. Make ZooKeeper easier to test - support simulating a session
+  expiration (Jordan Zimmerman via michim)
+
 headers
 
 Release 3.4.0 - 

+ 2 - 1
src/java/main/org/apache/zookeeper/ClientCnxn.java

@@ -1432,7 +1432,8 @@ public class ClientCnxn {
 
     private int xid = 1;
 
-    private volatile States state = States.NOT_CONNECTED;
+    // @VisibleForTesting
+    volatile States state = States.NOT_CONNECTED;
 
     /*
      * getXid() is called externally by ClientCnxnNIO::doIO() when packets are sent from the outgoingQueue to

+ 29 - 0
src/java/main/org/apache/zookeeper/Testable.java

@@ -0,0 +1,29 @@
+/**
+ * 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.zookeeper;
+
+/**
+ * Abstraction that exposes various methods useful for testing ZooKeeper
+ */
+public interface Testable {
+    /**
+     * Cause the ZooKeeper instance to behave as if the session expired
+     */
+    void injectSessionExpiration();
+}

+ 6 - 1
src/java/main/org/apache/zookeeper/ZooKeeper.java

@@ -136,7 +136,7 @@ public class ZooKeeper {
         LOG = LoggerFactory.getLogger(ZooKeeper.class);
         Environment.logEnv("Client environment:", LOG);
     }
-    
+
     private final StaticHostProvider hostProvider;
     
     /**
@@ -862,6 +862,11 @@ public class ZooKeeper {
         cnxn.start();
     }
 
+    // VisibleForTesting
+    public Testable getTestable() {
+        return new ZooKeeperTestable(this, cnxn);
+    }
+
     /**
      * The session id for this ZooKeeper client instance. The value returned is
      * not valid until the client connects to a server and may change after a

+ 47 - 0
src/java/main/org/apache/zookeeper/ZooKeeperTestable.java

@@ -0,0 +1,47 @@
+/**
+ * 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.zookeeper;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+class ZooKeeperTestable implements Testable {
+    private static final Logger LOG = LoggerFactory
+            .getLogger(ZooKeeperTestable.class);
+
+    private final ZooKeeper zooKeeper;
+    private final ClientCnxn clientCnxn;
+
+    ZooKeeperTestable(ZooKeeper zooKeeper, ClientCnxn clientCnxn) {
+        this.zooKeeper = zooKeeper;
+        this.clientCnxn = clientCnxn;
+    }
+
+    @Override
+    public void injectSessionExpiration() {
+        LOG.info("injectSessionExpiration() called");
+
+        clientCnxn.eventThread.queueEvent(new WatchedEvent(
+                Watcher.Event.EventType.None,
+                Watcher.Event.KeeperState.Expired, null));
+        clientCnxn.eventThread.queueEventOfDeath();
+        clientCnxn.sendThread.getClientCnxnSocket().wakeupCnxn();
+        clientCnxn.state = ZooKeeper.States.CLOSED;
+    }
+}

+ 59 - 0
src/java/test/org/apache/zookeeper/ZooKeeperTestableTest.java

@@ -0,0 +1,59 @@
+/**
+ * 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.zookeeper;
+
+import org.apache.zookeeper.test.ClientBase;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.io.IOException;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+public class ZooKeeperTestableTest extends ClientBase {
+    @Test
+    public void testSessionExpiration() throws IOException, InterruptedException,
+            KeeperException {
+        ZooKeeper zk = createClient();
+
+        final CountDownLatch expirationLatch = new CountDownLatch(1);
+        Watcher watcher = new Watcher() {
+            @Override
+            public void process(WatchedEvent event) {
+                if ( event.getState() == Event.KeeperState.Expired ) {
+                    expirationLatch.countDown();
+                }
+            }
+        };
+        zk.exists("/foo", watcher);
+
+        zk.getTestable().injectSessionExpiration();
+        Assert.assertTrue(expirationLatch.await(5, TimeUnit.SECONDS));
+
+        boolean gotException = false;
+        try {
+            zk.exists("/foo", false);
+            Assert.fail("Should have thrown a SessionExpiredException");
+        } catch (KeeperException.SessionExpiredException e) {
+            // correct
+            gotException = true;
+        }
+        Assert.assertTrue(gotException);
+    }
+}