1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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 }