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.first.pause", 10);
43 conf.setInt("hbase.lease.recovery.pause", 10);
44 };
45 private FSHDFSUtils fsHDFSUtils = new FSHDFSUtils();
46 private static Path FILE = new Path(HTU.getDataTestDir(), "file.txt");
47 long startTime = -1;
48
49 @Before
50 public void setup() {
51 this.startTime = EnvironmentEdgeManager.currentTimeMillis();
52 }
53
54
55
56
57
58 @Test (timeout = 30000)
59 public void testRecoverLease() throws IOException {
60 HTU.getConfiguration().setInt("hbase.lease.recovery.dfs.timeout", 1000);
61 CancelableProgressable reporter = Mockito.mock(CancelableProgressable.class);
62 Mockito.when(reporter.progress()).thenReturn(true);
63 DistributedFileSystem dfs = Mockito.mock(DistributedFileSystem.class);
64
65 Mockito.when(dfs.recoverLease(FILE)).
66 thenReturn(false).thenReturn(false).thenReturn(false).thenReturn(false).thenReturn(true);
67 assertTrue(this.fsHDFSUtils.recoverDFSFileLease(dfs, FILE, HTU.getConfiguration(), reporter));
68 Mockito.verify(dfs, Mockito.times(5)).recoverLease(FILE);
69
70
71 assertTrue((EnvironmentEdgeManager.currentTimeMillis() - this.startTime) >
72 (3 * HTU.getConfiguration().getInt("hbase.lease.recovery.dfs.timeout", 61000)));
73 }
74
75
76
77
78
79 @Test (timeout = 30000)
80 public void testIsFileClosed() throws IOException {
81
82 HTU.getConfiguration().setInt("hbase.lease.recovery.dfs.timeout", 100000);
83 CancelableProgressable reporter = Mockito.mock(CancelableProgressable.class);
84 Mockito.when(reporter.progress()).thenReturn(true);
85 IsFileClosedDistributedFileSystem dfs = Mockito.mock(IsFileClosedDistributedFileSystem.class);
86
87
88
89 Mockito.when(dfs.recoverLease(FILE)).
90 thenReturn(false).thenReturn(false).thenReturn(true);
91 Mockito.when(dfs.isFileClosed(FILE)).thenReturn(true);
92 assertTrue(this.fsHDFSUtils.recoverDFSFileLease(dfs, FILE, HTU.getConfiguration(), reporter));
93 Mockito.verify(dfs, Mockito.times(2)).recoverLease(FILE);
94 Mockito.verify(dfs, Mockito.times(1)).isFileClosed(FILE);
95 }
96
97
98
99
100 class IsFileClosedDistributedFileSystem extends DistributedFileSystem {
101
102
103
104
105 public boolean isFileClosed(Path f) throws IOException{
106 return false;
107 }
108 }
109 }