1 package org.apache.helix.manager.zk;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import java.util.Date;
23
24 import org.apache.helix.InstanceType;
25 import org.apache.helix.TestHelper;
26 import org.apache.helix.ZkTestHelper;
27 import org.apache.helix.integration.ZkIntegrationTestBase;
28 import org.apache.helix.manager.zk.ZKHelixManager;
29 import org.testng.Assert;
30 import org.testng.annotations.Test;
31
32
33 public class TestHandleNewSession extends ZkIntegrationTestBase
34 {
35 @Test
36 public void testHandleNewSession() throws Exception
37 {
38 // Logger.getRootLogger().setLevel(Level.INFO);
39 String className = TestHelper.getTestClassName();
40 String methodName = TestHelper.getTestMethodName();
41 String clusterName = className + "_" + methodName;
42
43 System.out.println("START " + clusterName + " at "
44 + new Date(System.currentTimeMillis()));
45
46
47 TestHelper.setupCluster(clusterName, ZK_ADDR, 12918, // participant port
48 "localhost", // participant name prefix
49 "TestDB", // resource name prefix
50 1, // resources
51 10, // partitions per resource
52 5, // number of nodes
53 3, // replicas
54 "MasterSlave",
55 true); // do rebalance
56
57 ZKHelixManager manager =
58 new ZKHelixManager(clusterName,
59 "localhost_12918",
60 InstanceType.PARTICIPANT,
61 ZK_ADDR);
62 manager.connect();
63
64 // Logger.getRootLogger().setLevel(Level.INFO);
65 String lastSessionId = manager.getSessionId();
66 for (int i = 0; i < 3; i++)
67 {
68 // System.err.println("curSessionId: " + lastSessionId);
69 ZkTestHelper.expireSession(manager._zkClient);
70
71 String sessionId = manager.getSessionId();
72 Assert.assertTrue(sessionId.compareTo(lastSessionId) > 0, "Session id should be increased after expiry");
73 lastSessionId = sessionId;
74
75 // make sure session id is not 0
76 Assert.assertFalse(sessionId.equals("0"),
77 "Hit race condition in zhclient.handleNewSession(). sessionId is not returned yet.");
78
79 // TODO: need to test session expiry during handleNewSession()
80 }
81
82 // Logger.getRootLogger().setLevel(Level.INFO);
83 System.out.println("Disconnecting ...");
84 manager.disconnect();
85
86 System.out.println("END " + clusterName + " at "
87 + new Date(System.currentTimeMillis()));
88
89 }
90 }