source: proiecte/HadoopJUnit/hadoop-0.20.1/src/test/org/apache/hadoop/filecache/TestDistributedCache.java @ 120

Last change on this file since 120 was 120, checked in by (none), 14 years ago

Added the mail files for the Hadoop JUNit Project

  • Property svn:executable set to *
File size: 2.8 KB
Line 
1package org.apache.hadoop.filecache;
2
3import java.io.IOException;
4import java.net.URI;
5import java.util.Random;
6
7import org.apache.hadoop.conf.Configuration;
8import org.apache.hadoop.fs.FSDataOutputStream;
9import org.apache.hadoop.fs.FileStatus;
10import org.apache.hadoop.fs.FileSystem;
11import org.apache.hadoop.fs.Path;
12
13import junit.framework.TestCase;
14
15public class TestDistributedCache extends TestCase {
16 
17  static final URI LOCAL_FS = URI.create("file:///");
18  private static String TEST_CACHE_BASE_DIR =
19    new Path(System.getProperty("test.build.data","/tmp/cachebasedir"))
20    .toString().replace(' ', '+');
21  private static String TEST_ROOT_DIR =
22    System.getProperty("test.build.data", "/tmp/distributedcache");
23  private static final int TEST_FILE_SIZE = 4 * 1024; // 4K
24  private static final int LOCAL_CACHE_LIMIT = 5 * 1024; //5K
25  private Configuration conf;
26  private Path firstCacheFile;
27  private Path secondCacheFile;
28  private FileSystem localfs;
29 
30  /**
31   * @see TestCase#setUp()
32   */
33  @Override
34  protected void setUp() throws IOException {
35    conf = new Configuration();
36    conf.setLong("local.cache.size", LOCAL_CACHE_LIMIT);
37    localfs = FileSystem.get(LOCAL_FS, conf);
38    firstCacheFile = new Path(TEST_ROOT_DIR+"/firstcachefile");
39    secondCacheFile = new Path(TEST_ROOT_DIR+"/secondcachefile");
40    createTempFile(localfs, firstCacheFile);
41    createTempFile(localfs, secondCacheFile);
42  }
43 
44  /** test delete cache */
45  public void testDeleteCache() throws Exception {
46    DistributedCache.getLocalCache(firstCacheFile.toUri(), conf, new Path(TEST_CACHE_BASE_DIR), 
47        false, System.currentTimeMillis(), new Path(TEST_ROOT_DIR));
48    DistributedCache.releaseCache(firstCacheFile.toUri(), conf);
49    //in above code,localized a file of size 4K and then release the cache which will cause the cache
50    //be deleted when the limit goes out. The below code localize another cache which's designed to
51    //sweep away the first cache.
52    DistributedCache.getLocalCache(secondCacheFile.toUri(), conf, new Path(TEST_CACHE_BASE_DIR), 
53        false, System.currentTimeMillis(), new Path(TEST_ROOT_DIR));
54    FileStatus[] dirStatuses = localfs.listStatus(new Path(TEST_CACHE_BASE_DIR));
55    assertTrue("DistributedCache failed deleting old cache when the cache store is full.",
56        dirStatuses.length > 1);
57  }
58
59  private void createTempFile(FileSystem fs, Path p) throws IOException {
60    FSDataOutputStream out = fs.create(p);
61    byte[] toWrite = new byte[TEST_FILE_SIZE];
62    new Random().nextBytes(toWrite);
63    out.write(toWrite);
64    out.close();
65    FileSystem.LOG.info("created: " + p + ", size=" + TEST_FILE_SIZE);
66  }
67 
68  /**
69   * @see TestCase#tearDown()
70   */
71  @Override
72  protected void tearDown() throws IOException {
73    localfs.delete(firstCacheFile, true);
74    localfs.delete(secondCacheFile, true);
75    localfs.close();
76  }
77}
Note: See TracBrowser for help on using the repository browser.