|
@@ -19,14 +19,21 @@ package org.apache.hadoop.hdfs.util;
|
|
|
|
|
|
import java.io.File;
|
|
import java.io.File;
|
|
import java.io.FileWriter;
|
|
import java.io.FileWriter;
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.util.concurrent.Callable;
|
|
|
|
|
|
import org.apache.hadoop.hdfs.protocol.DatanodeAdminProperties;
|
|
import org.apache.hadoop.hdfs.protocol.DatanodeAdminProperties;
|
|
import org.apache.hadoop.test.GenericTestUtils;
|
|
import org.apache.hadoop.test.GenericTestUtils;
|
|
import org.junit.Before;
|
|
import org.junit.Before;
|
|
import org.junit.After;
|
|
import org.junit.After;
|
|
import org.junit.Test;
|
|
import org.junit.Test;
|
|
|
|
+import org.mockito.invocation.InvocationOnMock;
|
|
|
|
+import org.mockito.stubbing.Answer;
|
|
|
|
+import org.mockito.Mock;
|
|
|
|
+import org.mockito.Mockito;
|
|
|
|
|
|
import static org.junit.Assert.assertEquals;
|
|
import static org.junit.Assert.assertEquals;
|
|
|
|
+import static org.mockito.Mockito.when;
|
|
|
|
|
|
/**
|
|
/**
|
|
* Test for JSON based HostsFileReader.
|
|
* Test for JSON based HostsFileReader.
|
|
@@ -44,8 +51,12 @@ public class TestCombinedHostsFileReader {
|
|
private final File legacyFile =
|
|
private final File legacyFile =
|
|
new File(TESTCACHEDATADIR, "legacy.dfs.hosts.json");
|
|
new File(TESTCACHEDATADIR, "legacy.dfs.hosts.json");
|
|
|
|
|
|
|
|
+ @Mock
|
|
|
|
+ private Callable<DatanodeAdminProperties[]> callable;
|
|
|
|
+
|
|
@Before
|
|
@Before
|
|
public void setUp() throws Exception {
|
|
public void setUp() throws Exception {
|
|
|
|
+ callable = Mockito.mock(Callable.class);
|
|
}
|
|
}
|
|
|
|
|
|
@After
|
|
@After
|
|
@@ -87,4 +98,50 @@ public class TestCombinedHostsFileReader {
|
|
CombinedHostsFileReader.readFile(newFile.getAbsolutePath());
|
|
CombinedHostsFileReader.readFile(newFile.getAbsolutePath());
|
|
assertEquals(0, all.length);
|
|
assertEquals(0, all.length);
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * When timeout is enabled, test for success when reading file within timeout
|
|
|
|
+ * limits
|
|
|
|
+ */
|
|
|
|
+ @Test
|
|
|
|
+ public void testReadFileWithTimeoutSuccess() throws Exception {
|
|
|
|
+
|
|
|
|
+ DatanodeAdminProperties[] all = CombinedHostsFileReader.readFileWithTimeout(
|
|
|
|
+ jsonFile.getAbsolutePath(), 1000);
|
|
|
|
+ assertEquals(7, all.length);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * When timeout is enabled, test for IOException when reading file exceeds
|
|
|
|
+ * timeout limits
|
|
|
|
+ */
|
|
|
|
+ @Test(expected = IOException.class)
|
|
|
|
+ public void testReadFileWithTimeoutTimeoutException() throws Exception {
|
|
|
|
+ when(callable.call()).thenAnswer(new Answer<Void>() {
|
|
|
|
+ @Override
|
|
|
|
+ public Void answer(InvocationOnMock invocation) throws Throwable {
|
|
|
|
+ Thread.sleep(2000);
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ CombinedHostsFileReader.readFileWithTimeout(
|
|
|
|
+ jsonFile.getAbsolutePath(), 1);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * When timeout is enabled, test for IOException when execution is interrupted
|
|
|
|
+ */
|
|
|
|
+ @Test(expected = IOException.class)
|
|
|
|
+ public void testReadFileWithTimeoutInterruptedException() throws Exception {
|
|
|
|
+ when(callable.call()).thenAnswer(new Answer<Void>() {
|
|
|
|
+ @Override
|
|
|
|
+ public Void answer(InvocationOnMock invocation) throws Throwable {
|
|
|
|
+ throw new InterruptedException();
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ CombinedHostsFileReader.readFileWithTimeout(
|
|
|
|
+ jsonFile.getAbsolutePath(), 1);
|
|
|
|
+ }
|
|
}
|
|
}
|