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.fs; |
---|
19 | |
---|
20 | import junit.framework.TestCase; |
---|
21 | |
---|
22 | import java.io.File; |
---|
23 | import java.io.IOException; |
---|
24 | import java.io.RandomAccessFile; |
---|
25 | import java.util.Random; |
---|
26 | |
---|
27 | /** This test makes sure that "DU" does not get to run on each call to getUsed */ |
---|
28 | public class TestDU extends TestCase { |
---|
29 | final static private File DU_DIR = new File( |
---|
30 | System.getProperty("test.build.data","/tmp"), "dutmp"); |
---|
31 | |
---|
32 | public void setUp() throws IOException { |
---|
33 | FileUtil.fullyDelete(DU_DIR); |
---|
34 | assertTrue(DU_DIR.mkdirs()); |
---|
35 | } |
---|
36 | |
---|
37 | public void tearDown() throws IOException { |
---|
38 | FileUtil.fullyDelete(DU_DIR); |
---|
39 | } |
---|
40 | |
---|
41 | private void createFile(File newFile, int size) throws IOException { |
---|
42 | // write random data so that filesystems with compression enabled (e.g., ZFS) |
---|
43 | // can't compress the file |
---|
44 | Random random = new Random(); |
---|
45 | byte[] data = new byte[size]; |
---|
46 | random.nextBytes(data); |
---|
47 | |
---|
48 | newFile.createNewFile(); |
---|
49 | RandomAccessFile file = new RandomAccessFile(newFile, "rws"); |
---|
50 | |
---|
51 | file.write(data); |
---|
52 | |
---|
53 | file.getFD().sync(); |
---|
54 | file.close(); |
---|
55 | } |
---|
56 | |
---|
57 | /** |
---|
58 | * Verify that du returns expected used space for a file. |
---|
59 | * We assume here that if a file system crates a file of size |
---|
60 | * that is a multiple of the block size in this file system, |
---|
61 | * then the used size for the file will be exactly that size. |
---|
62 | * This is true for most file systems. |
---|
63 | * |
---|
64 | * @throws IOException |
---|
65 | * @throws InterruptedException |
---|
66 | */ |
---|
67 | public void testDU() throws IOException, InterruptedException { |
---|
68 | int writtenSize = 32*1024; // writing 32K |
---|
69 | File file = new File(DU_DIR, "data"); |
---|
70 | createFile(file, writtenSize); |
---|
71 | |
---|
72 | Thread.sleep(5000); // let the metadata updater catch up |
---|
73 | |
---|
74 | DU du = new DU(file, 10000); |
---|
75 | du.start(); |
---|
76 | long duSize = du.getUsed(); |
---|
77 | du.shutdown(); |
---|
78 | |
---|
79 | assertEquals(writtenSize, duSize); |
---|
80 | |
---|
81 | //test with 0 interval, will not launch thread |
---|
82 | du = new DU(file, 0); |
---|
83 | du.start(); |
---|
84 | duSize = du.getUsed(); |
---|
85 | du.shutdown(); |
---|
86 | |
---|
87 | assertEquals(writtenSize, duSize); |
---|
88 | |
---|
89 | //test without launching thread |
---|
90 | du = new DU(file, 10000); |
---|
91 | duSize = du.getUsed(); |
---|
92 | |
---|
93 | assertEquals(writtenSize, duSize); |
---|
94 | } |
---|
95 | } |
---|