View Javadoc

1   package org.apache.helix.participant;
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 org.apache.helix.NotificationContext;
23  import org.apache.helix.TestHelper;
24  import org.apache.helix.ZkUnitTestBase;
25  import org.apache.helix.model.Message;
26  import org.apache.helix.model.Message.MessageType;
27  import org.apache.helix.participant.DistClusterControllerStateModel;
28  import org.testng.annotations.BeforeMethod;
29  import org.testng.annotations.Test;
30  
31  
32  public class TestDistControllerStateModel extends ZkUnitTestBase
33  {
34    final String clusterName = CLUSTER_PREFIX + "_" + getShortClassName();
35    DistClusterControllerStateModel stateModel = null;
36  
37    @BeforeMethod()
38    public void beforeMethod()
39    {
40      stateModel = new DistClusterControllerStateModel(ZK_ADDR);
41      if (_gZkClient.exists("/" + clusterName))
42      {
43        _gZkClient.deleteRecursive("/" + clusterName);
44      }
45      TestHelper.setupEmptyCluster(_gZkClient, clusterName);
46    }
47  
48    @Test()
49    public void testOnBecomeStandbyFromOffline()
50    {
51      stateModel.onBecomeStandbyFromOffline(null, null);
52    }
53  
54    @Test()
55    public void testOnBecomeLeaderFromStandby()
56    {
57      Message message = new Message(MessageType.STATE_TRANSITION, "0");
58      message.setPartitionName(clusterName);
59      message.setTgtName("controller_0");
60      try
61      {
62        stateModel.onBecomeLeaderFromStandby(message, new NotificationContext(null));
63      }
64      catch (Exception e)
65      {
66        // TODO Auto-generated catch block
67        e.printStackTrace();
68      }
69      stateModel.onBecomeStandbyFromLeader(message, new NotificationContext(null));
70    }
71  
72    @Test()
73    public void testOnBecomeStandbyFromLeader()
74    {
75      Message message = new Message(MessageType.STATE_TRANSITION, "0");
76      message.setPartitionName(clusterName);
77      message.setTgtName("controller_0");
78      stateModel.onBecomeStandbyFromLeader(message, new NotificationContext(null));
79    }
80  
81    @Test()
82    public void testOnBecomeOfflineFromStandby()
83    {
84      Message message = new Message(MessageType.STATE_TRANSITION, "0");
85      message.setPartitionName(clusterName);
86      message.setTgtName("controller_0");
87  
88      stateModel.onBecomeOfflineFromStandby(message, null);
89    }
90  
91    @Test()
92    public void testOnBecomeDroppedFromOffline()
93    {
94      stateModel.onBecomeDroppedFromOffline(null, null);
95    }
96  
97    @Test()
98    public void testOnBecomeOfflineFromDropped()
99    {
100     stateModel.onBecomeOfflineFromDropped(null, null);
101   }
102 
103   @Test()
104   public void testRollbackOnError()
105   {
106     Message message = new Message(MessageType.STATE_TRANSITION, "0");
107     message.setPartitionName(clusterName);
108     message.setTgtName("controller_0");
109     try
110     {
111       stateModel.onBecomeLeaderFromStandby(message, new NotificationContext(null));
112     }
113     catch (Exception e)
114     {
115       // TODO Auto-generated catch block
116       e.printStackTrace();
117     }
118     stateModel.rollbackOnError(message, new NotificationContext(null), null);
119   }
120 
121   @Test()
122   public void testReset()
123   {
124     Message message = new Message(MessageType.STATE_TRANSITION, "0");
125     message.setPartitionName(clusterName);
126     message.setTgtName("controller_0");
127     try
128     {
129       stateModel.onBecomeLeaderFromStandby(message, new NotificationContext(null));
130     }
131     catch (Exception e)
132     {
133       // TODO Auto-generated catch block
134       e.printStackTrace();
135     }
136     stateModel.reset();
137   }
138 
139 }