View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.chukwa.datacollection.agent;
19  
20  
21  import java.util.ArrayList;
22  import org.apache.hadoop.conf.Configuration;
23  import org.apache.hadoop.chukwa.conf.ChukwaConfiguration;
24  import org.apache.hadoop.chukwa.datacollection.agent.ChukwaAgent;
25  import org.apache.hadoop.chukwa.datacollection.controller.ChukwaAgentController;
26  import org.apache.hadoop.chukwa.datacollection.test.ConsoleOutConnector;
27  import junit.framework.TestCase;
28  //Note this test takes a minimum of 
29  // 20 * 2 + 6* 20 = 160 seconds. 
30  public class TestAgent extends TestCase {
31  
32    public void testStopAndStart() {
33  
34      try {
35        Configuration conf = new Configuration();
36        conf.setInt("chukwaAgent.control.port", 0);
37        ChukwaAgent agent = new ChukwaAgent(conf);
38        ConsoleOutConnector conn = new ConsoleOutConnector(agent, true);
39        conn.start();
40  
41        int portno = agent.getControllerPort();
42        ChukwaAgentController cli = new ChukwaAgentController("localhost", portno);
43  
44        for (int i = 1; i < 20; ++i) {
45          String adaptorId = cli.add(
46              "org.apache.hadoop.chukwa.util.ConstRateAdaptor", "raw" + i, "2000"
47                  + i, 0);
48          assertNotNull(adaptorId);
49          Thread.sleep(2000);
50          cli.removeAll();
51        }
52        agent.shutdown();
53        conn.shutdown();
54        Thread.sleep(2000);
55      } catch (Exception e) {
56        e.printStackTrace();
57        fail(e.toString());
58      }
59    }
60  
61    public void testMultiStopAndStart() {
62  
63      try {
64        Configuration conf = new Configuration();
65        conf.setInt("chukwaAgent.control.port", 0);
66        ChukwaAgent agent = new ChukwaAgent(conf);
67        ConsoleOutConnector conn = new ConsoleOutConnector(agent, true);
68        conn.start();
69        int count = agent.adaptorCount();
70        for (int trial = 0; trial < 20; ++trial) {
71          ArrayList<String> runningAdaptors = new ArrayList<String>();
72  
73          for (int i = 1; i < 7; ++i) {
74            String l = agent
75                .processAddCommand("add  org.apache.hadoop.chukwa.util.ConstRateAdaptor  raw"
76                    + i + " 2000" + i + " 0");
77            assertTrue(l != null);
78            runningAdaptors.add(l);
79          }
80          Thread.sleep(1000);
81          for (String l : runningAdaptors)
82            agent.stopAdaptor(l, false);
83          Thread.sleep(5000);
84          assertTrue(agent.adaptorCount() == count);
85        }
86        agent.shutdown();
87        Thread.sleep(2000);
88      } catch (Exception e) {
89        e.printStackTrace();
90        fail(e.toString());
91      }
92    }
93  
94    public void testLogRotate() {
95  
96    }
97  
98  }