1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.chukwa.datacollection.adaptor.filetailer;
19
20 import java.io.File;
21 import java.io.FileOutputStream;
22 import java.io.IOException;
23 import java.io.PrintWriter;
24 import junit.framework.Assert;
25 import junit.framework.TestCase;
26 import org.apache.hadoop.chukwa.conf.ChukwaConfiguration;
27 import org.apache.hadoop.chukwa.datacollection.agent.ChukwaAgent;
28 import org.apache.hadoop.chukwa.datacollection.controller.ChukwaAgentController;
29 import org.apache.hadoop.conf.Configuration;
30
31 public class TestFileExpirationPolicy extends TestCase {
32
33 public void testExpiration() {
34 ChukwaAgent agent = null;
35
36 try {
37 Configuration conf = new ChukwaConfiguration();
38 conf.set("chukwaAgent.control.port", "0");
39 agent = new ChukwaAgent(conf);
40
41 FileTailingAdaptor.GRACEFUL_PERIOD = 30 * 1000;
42
43 String adaptorId = agent
44 .processAddCommand("add org.apache.hadoop.chukwa.datacollection.adaptor.filetailer.CharFileTailingAdaptorUTF8NewLineEscaped MyType 0 /myWrongPath"
45 + System.currentTimeMillis() + " 0");
46
47 assertTrue(adaptorId != null);
48
49 assertNotNull(agent.getAdaptor(adaptorId));
50
51 Thread.sleep(FileTailingAdaptor.GRACEFUL_PERIOD + 10000);
52 assertNull(agent.getAdaptor(adaptorId));
53
54 } catch (Exception e) {
55 Assert.fail("Exception in TestFileExpirationPolicy");
56 } finally {
57 if (agent != null) {
58 agent.shutdown();
59 try {
60 Thread.sleep(2000);
61 } catch (Exception ex) {
62 }
63 }
64 }
65
66 }
67
68 public void testExpirationOnFileThatHasBennDeleted() {
69 ChukwaAgent agent = null;
70 File testFile = null;
71 try {
72
73 File tempDir = new File(System.getProperty("test.build.data", "/tmp"));
74 if (!tempDir.exists()) {
75 tempDir.mkdirs();
76 }
77 String logFile = tempDir.getPath() + "/chukwatestExpiration.txt";
78 testFile = makeTestFile(logFile, 8000);
79
80 Configuration conf = new ChukwaConfiguration();
81 conf.set("chukwaAgent.control.port", "0");
82 agent = new ChukwaAgent(conf);
83
84
85 ChukwaAgentController cli = new ChukwaAgentController("localhost", agent.getControllerPort());
86 cli.removeAll();
87
88
89 Thread.sleep(5000);
90
91 assertTrue(testFile.canRead() == true);
92
93 FileTailingAdaptor.GRACEFUL_PERIOD = 30 * 1000;
94 String adaptorId = agent
95 .processAddCommand("add org.apache.hadoop.chukwa.datacollection.adaptor.filetailer.CharFileTailingAdaptorUTF8NewLineEscaped MyType 0 "
96 + logFile + " 0");
97
98 assertTrue(adaptorId != null);
99
100 assertNotNull(agent.getAdaptor(adaptorId));
101
102 Thread.sleep(10000);
103 testFile.delete();
104
105 Thread.sleep(FileTailingAdaptor.GRACEFUL_PERIOD + 10000);
106 assertNull(agent.getAdaptor(adaptorId));
107 agent.shutdown();
108 Thread.sleep(2000);
109 } catch (Exception e) {
110 Assert.fail("Exception in TestFileExpirationPolicy");
111 } finally {
112 if (agent != null) {
113 agent.shutdown();
114 try {
115 Thread.sleep(2000);
116 } catch (Exception ex) {
117 }
118 }
119 }
120 }
121
122 private File makeTestFile(String name, int size) throws IOException {
123 File tmpOutput = new File(name);
124 FileOutputStream fos = new FileOutputStream(tmpOutput);
125
126 PrintWriter pw = new PrintWriter(fos);
127 for (int i = 0; i < size; ++i) {
128 pw.print(i + " ");
129 pw.println("abcdefghijklmnopqrstuvwxyz");
130 }
131 pw.flush();
132 pw.close();
133 return tmpOutput;
134 }
135 }