1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.util;
19
20 import static org.junit.Assert.assertTrue;
21
22 import java.io.IOException;
23
24 import org.apache.hadoop.conf.Configuration;
25 import org.apache.hadoop.fs.Path;
26 import org.apache.hadoop.hbase.HBaseTestingUtility;
27 import org.apache.hadoop.hbase.MediumTests;
28 import org.apache.hadoop.hdfs.DistributedFileSystem;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.experimental.categories.Category;
32 import org.mockito.Mockito;
33
34
35
36
37 @Category(MediumTests.class)
38 public class TestFSHDFSUtils {
39 private static final HBaseTestingUtility HTU = new HBaseTestingUtility();
40 static {
41 Configuration conf = HTU.getConfiguration();
42 conf.setInt("hbase.lease.recovery.dfs.timeout", 1000);
43 conf.setInt("hbase.lease.recovery.first.pause", 10);
44 conf.setInt("hbase.lease.recovery.pause", 10);
45 };
46 private FSHDFSUtils fsHDFSUtils = new FSHDFSUtils();
47 private static Path FILE = new Path(HTU.getDataTestDir(), "file.txt");
48 long startTime = -1;
49
50 @Before
51 public void setup() {
52 this.startTime = EnvironmentEdgeManager.currentTimeMillis();
53 }
54
55
56
57
58
59 @Test (timeout = 30000)
60 public void testRecoverLease() throws IOException {
61 DistributedFileSystem dfs = Mockito.mock(DistributedFileSystem.class);
62
63 Mockito.when(dfs.recoverLease(FILE)).
64 thenReturn(false).thenReturn(false).thenReturn(false).thenReturn(false).thenReturn(true);
65 assertTrue(this.fsHDFSUtils.recoverDFSFileLease(dfs, FILE, HTU.getConfiguration()));
66 Mockito.verify(dfs, Mockito.times(5)).recoverLease(FILE);
67
68
69 assertTrue((EnvironmentEdgeManager.currentTimeMillis() - this.startTime) >
70 (3 * HTU.getConfiguration().getInt("hbase.lease.recovery.dfs.timeout", 61000)));
71 }
72
73
74
75
76
77 @Test (timeout = 30000)
78 public void testIsFileClosed() throws IOException {
79
80 HTU.getConfiguration().setInt("hbase.lease.recovery.dfs.timeout", 100000);
81 IsFileClosedDistributedFileSystem dfs = Mockito.mock(IsFileClosedDistributedFileSystem.class);
82
83
84
85 Mockito.when(dfs.recoverLease(FILE)).
86 thenReturn(false).thenReturn(false).thenReturn(true);
87 Mockito.when(dfs.isFileClosed(FILE)).thenReturn(true);
88 assertTrue(this.fsHDFSUtils.recoverDFSFileLease(dfs, FILE, HTU.getConfiguration()));
89 Mockito.verify(dfs, Mockito.times(2)).recoverLease(FILE);
90 Mockito.verify(dfs, Mockito.times(1)).isFileClosed(FILE);
91 }
92
93
94
95
96 class IsFileClosedDistributedFileSystem extends DistributedFileSystem {
97
98
99
100
101 public boolean isFileClosed(Path f) throws IOException{
102 return false;
103 }
104 }
105 }