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.sender;
19  
20  
21  import junit.framework.TestCase;
22  import java.io.*;
23  import java.util.*;
24  import org.apache.hadoop.chukwa.datacollection.sender.RetryListOfCollectors;
25  import org.apache.hadoop.conf.Configuration;
26  
27  public class TestRetryListOfCollectors extends TestCase {
28  
29    public void testRetryList() {
30      List<String> hosts = new ArrayList<String>();
31      hosts.add("host1");
32      hosts.add("host2");
33      hosts.add("host3");
34      hosts.add("host4");
35      Configuration conf = new Configuration();
36      RetryListOfCollectors rloc = new RetryListOfCollectors(hosts, conf);
37      rloc.shuffleList();
38      assertEquals(hosts.size(), rloc.total());
39  
40      for (int i = 0; i < hosts.size(); ++i) {
41        assertTrue(rloc.hasNext());
42        String s = rloc.next();
43        assertTrue(s != null);
44        System.out.println(s);
45      }
46  
47      if (rloc.hasNext()) {
48        String s = rloc.next();
49        System.out.println("saw unexpected collector " + s);
50        fail();
51      }
52    }
53    
54    public void testCollectorsFile() {
55      
56      try {
57      File tmpOutput = new File(System.getProperty("test.build.data", "/tmp"),
58          "collectors_test");
59      PrintWriter out = new PrintWriter(new FileOutputStream(tmpOutput));
60      
61      HashSet<String> validHosts = new HashSet<String>();
62      validHosts.add("http://host1:5052/");
63      validHosts.add("http://host2:5050/");
64      validHosts.add("http://host3:5052/");
65      validHosts.add("http://host4:5050/");
66      validHosts.add("http://host5:5052/");
67      validHosts.add("http://host6:5052/");
68      
69      out.println("host1");
70      out.println("host2:5050");
71      out.println("http://host3");
72      out.println("http://host4:5050");
73      out.println("http://host5:5052/");
74      out.println("host6:5052");
75      out.close();
76      
77      Configuration conf = new Configuration();
78      conf.setInt("chukwaCollector.http.port", 5052);
79      RetryListOfCollectors rloc = new RetryListOfCollectors(tmpOutput, conf);
80      for (int i = 0; i < validHosts.size(); ++i) {
81        assertTrue(rloc.hasNext());
82        String s = rloc.next();
83        assertTrue(s != null);
84        
85        System.out.println("host: " + s);
86        assertTrue(validHosts.contains(s));
87      }
88      
89      if (rloc.hasNext()) {
90        String s = rloc.next();
91        System.out.println("saw unexpected collector " + s);
92        fail();
93      }
94  
95      } catch(IOException e) {
96        e.printStackTrace();
97        fail();
98      }
99      
100   }
101 
102 }