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.adaptor.filetailer;
19  
20  import static org.apache.hadoop.chukwa.util.TempFileUtil.makeTestFile;
21  import static org.junit.Assert.*;
22  
23  import java.io.File;
24  import java.io.IOException;
25  
26  import org.apache.hadoop.chukwa.conf.ChukwaConfiguration;
27  import org.apache.hadoop.chukwa.datacollection.agent.ChukwaAgent;
28  import org.apache.hadoop.chukwa.datacollection.agent.ChukwaAgent.AlreadyRunningException;
29  import org.apache.hadoop.chukwa.datacollection.connector.ChunkCatcherConnector;
30  import org.junit.After;
31  import org.junit.Test;
32  
33  public class TestFileTailer {
34  	private ChukwaAgent agent;
35  	private String adaptorId;
36  	private File testFile;
37  
38  	@After
39  	public void tearDown() throws Exception {
40  		agent.stopAdaptor(adaptorId, false);
41  		agent.shutdown();
42  		if (testFile != null) {
43  			testFile.delete();
44  		}
45  	}
46  
47  	@Test
48  	public void testDontSleepIfHasMoreData() throws AlreadyRunningException, IOException, InterruptedException {
49  		ChukwaConfiguration cc = new ChukwaConfiguration();
50  		cc.setInt("chukwaAgent.fileTailingAdaptor.maxReadSize", 18); // small in order to have hasMoreData=true
51  																	 // (with 26 letters we should have 2 chunks)
52  		agent = ChukwaAgent.getAgent(cc);
53  		agent.start();
54  		
55  		ChunkCatcherConnector chunks = new ChunkCatcherConnector();
56  	    chunks.start();
57  
58  	    File baseDir = new File(System.getProperty("test.build.data", "/tmp"));
59  		testFile = makeTestFile("testDontSleepIfHasMoreData", 1, baseDir); // insert 26 letters on file
60  		long startTime = System.currentTimeMillis();
61  		adaptorId = agent.processAddCommand("add adaptor_test ="
62  				+ "filetailer.FileTailingAdaptor testDontSleepIfHasMoreData "
63  				+ testFile.getCanonicalPath() + " 0");
64  
65  		chunks.waitForAChunk();
66  		chunks.waitForAChunk();
67  		
68  		long endTime = System.currentTimeMillis();
69  		assertTrue( endTime - startTime < 300 ); // ensure that everything finishes very fast
70  												 // faster than SAMPLE_PERIOD_MS (ie: we don't sleep)
71  	}
72  
73  }