/**
 * 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 test_session_expiration;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooKeeper;

import common.CountdownWatcher;

public class TestSessionExpiration {

    public static void main(String[] args) throws Exception {
        System.out.println("Starting zk1");
        
        // Open a client connection - zk1
        CountdownWatcher watch1 = new CountdownWatcher("zk1");
        ZooKeeper zk1 = new ZooKeeper(args[0], 10000, watch1);
        watch1.waitForConnected(10000);
        
        zk1.getData("/", false, null);

        System.out.println("Starting zk2");
        
        // now attach a second client zk2 with the same sessionid/passwd
        CountdownWatcher watch2 = new CountdownWatcher("zk2");
        ZooKeeper zk2 = new ZooKeeper(args[0], 10000, watch2,
                zk1.getSessionId(), zk1.getSessionPasswd());
        watch2.waitForConnected(10000);

        // close the second client, the session is now invalid
        System.out.println("Closing zk2");
        zk2.close();
        
        System.out.println("Attempting use of zk1");

        try {
            // this will throw session expired exception
            zk1.getData("/", false, null);
        } catch (KeeperException.SessionExpiredException e) {
            System.out.println("Got session expired on zk1!");
            return;
        }
        // there's a gotcha though - if you run this on against a quorum
        // (vs standalone) you may get a KeeperException.SessionMovedException
        // instead. This is thrown if a client moved from one server to
        // a second, but then attempts to talk to the first server (should
        // never happen, but could in certain bad situations), this example
        // simulates that situation in the sense that the client with 
        // session id zk1.getSessionId() has moved 
        // Again, if you run against standalone server you won't see this
        // If you run against quorum you need to determine which server
        // zk1 is attached to - we are adding this capability in 3.3.0 - and
        // have zk2 attach to that same server.
        
        System.err.println("Oops, this should NOT have happened!");
    }
}

@@ -1093,33 +1093,25 @@
 ting zk1%22);%0A
-        
 %0A
+
         // O
@@ -1306,25 +1306,17 @@
 10000);%0A
-        
 %0A
+
         
@@ -1391,25 +1391,17 @@
  zk2%22);%0A
-        
 %0A
+
         
@@ -1819,25 +1819,17 @@
 lose();%0A
-        
 %0A
+
         
@@ -2158,40 +2158,106 @@
  // 
-there's a gotcha though - if you
+3.2.0 and later:%0A        // There's a gotcha though - In version 3.2.0 and later if you%0A        //
  run
@@ -2281,27 +2281,16 @@
 a quorum
-%0A        //
  (vs sta
@@ -2311,16 +2311,27 @@
 ay get a
+%0A        //
  KeeperE
@@ -2360,27 +2360,16 @@
 xception
-%0A        //
  instead
@@ -2377,16 +2377,27 @@
  This is
+%0A        //
  thrown 
@@ -2432,27 +2432,16 @@
 erver to
-%0A        //
  a secon
@@ -2446,16 +2446,27 @@
 ond, but
+%0A        //
  then at
@@ -2507,16 +2507,22 @@
  (should
+ never
 %0A       
@@ -2524,22 +2524,16 @@
       //
- never
  happen,
@@ -2653,17 +2653,16 @@
 ent with
- 
 %0A       
@@ -2704,17 +2704,27 @@
 as moved
- 
+%0A        //
 %0A       
@@ -2784,21 +2784,117 @@
 on't see
- this
+%0A        // this. If you run against a server version 3.1.x or earlier%0A        // you won't see this.
 %0A       
@@ -2935,16 +2935,23 @@
 need to 
+easily 
 determin
@@ -2957,23 +2957,16 @@
 ne which
- server
 %0A       
@@ -2968,16 +2968,23 @@
       //
+ server
  zk1 is 
@@ -3026,16 +3026,27 @@
 pability
+%0A        //
  in 3.3.
@@ -3052,27 +3052,16 @@
 .0 - and
-%0A        //
  have zk
@@ -3090,24 +3090,16 @@
 server.%0A
-        
 %0A       

update comment - session moved is 3.2.x and later only