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.util;
19  
20  import java.io.File;
21  import java.io.IOException;
22  import java.util.ArrayList;
23  import java.util.List;
24  import org.apache.hadoop.chukwa.*;
25  import org.apache.hadoop.conf.Configuration;
26  import org.apache.hadoop.fs.FileSystem;
27  import org.apache.hadoop.fs.Path;
28  import org.apache.hadoop.io.SequenceFile;
29  import org.apache.hadoop.io.Text;
30  import junit.framework.TestCase;
31  import static org.apache.hadoop.chukwa.util.ConstRateValidator.ByteRange;
32  import static org.apache.hadoop.chukwa.util.ConstRateValidator.ValidatorSM;
33  import static org.apache.hadoop.chukwa.util.TempFileUtil.writeASinkFile;
34  
35  public class TestCRValidator extends TestCase{
36    
37    public void testCRchunks() {
38      ConstRateAdaptor adaptor = new ConstRateAdaptor();
39      adaptor.parseArgs("500  200 ");
40      adaptor.test_init("testdata");
41      Chunk c = adaptor.nextChunk(100);
42      assertTrue(ConstRateAdaptor.checkChunk(c));
43      c = adaptor.nextChunk(102);
44      assertTrue(ConstRateAdaptor.checkChunk(c));
45    }
46    
47    public void testBasicSM() throws Exception {
48      ValidatorSM sm = new ValidatorSM();
49      byte[] dat = "test".getBytes();    
50      ChunkImpl c = new ChunkImpl("Data", "aname", dat.length, dat, null);
51      ByteRange b = new ByteRange(c);
52      assertEquals(4, b.len);
53      assertEquals(0, b.start);
54      String t = sm.advanceSM(b);
55      assertNull(t);
56      if(t != null)
57        System.out.println(t);
58  
59      dat = "ing".getBytes();
60      c = new ChunkImpl("Data", "aname", dat.length+4, dat, null);
61      b = new ByteRange(c);
62      assertEquals(4, b.start);
63      t = sm.advanceSM(b);
64      assertNull(t);
65      if(t != null)
66        System.out.println(t);
67      
68     b = new ByteRange(new ChunkImpl("Data", "aname", 12, "more".getBytes(), null));
69     t= sm.advanceSM(b);
70     System.out.println(t);
71    }
72    
73    public void testSlurping() throws Exception {
74      int NUM_CHUNKS = 10;
75      Configuration conf = new Configuration();
76      FileSystem localfs = FileSystem.getLocal(conf);
77      String baseDir = System.getProperty("test.build.data", "/tmp");
78      Path tmpFile = new Path(baseDir+"/tmpSeqFile.seq");
79      writeASinkFile(conf, localfs, tmpFile, NUM_CHUNKS);
80       
81      ValidatorSM sm = new ValidatorSM();
82      
83      try {
84        SequenceFile.Reader reader = new SequenceFile.Reader(localfs, tmpFile, conf);
85  
86        ChukwaArchiveKey key = new ChukwaArchiveKey();
87        ChunkImpl chunk = ChunkImpl.getBlankChunk();
88  
89        while (reader.next(key, chunk)) {
90            String s = sm.advanceSM(new ByteRange(chunk));
91            assertNull(s);
92        }
93        reader.close();
94        assertEquals(NUM_CHUNKS, sm.chunks);      
95        localfs.delete(tmpFile);
96      } catch(IOException e) {
97        e.printStackTrace();
98      }
99      
100   }
101 
102 }