HADOOP-18614. Ensure that the config writer are closed

This commit is contained in:
Steve Vaughan Jr 2022-03-24 10:40:52 -04:00
parent af20841fb1
commit a76b225ec0
1 changed files with 65 additions and 58 deletions

View File

@ -125,18 +125,20 @@ public class TestConfigurationDeprecation {
* @throws IOException * @throws IOException
*/ */
@Test @Test
public void testDeprecation() throws IOException { public void testDeprecation() throws Exception {
addDeprecationToConfiguration(); addDeprecationToConfiguration();
out=new BufferedWriter(new FileWriter(CONFIG)); out=new BufferedWriter(new FileWriter(CONFIG));
startConfig(); try(AutoCloseable closeable = out::close) {
// load an old key and a new key. startConfig();
appendProperty("A", "a"); // load an old key and a new key.
appendProperty("D", "d"); appendProperty("A", "a");
// load an old key with multiple new-key mappings appendProperty("D", "d");
appendProperty("P", "p"); // load an old key with multiple new-key mappings
endConfig(); appendProperty("P", "p");
Path fileResource = new Path(CONFIG); endConfig();
conf.addResource(fileResource); Path fileResource = new Path(CONFIG);
conf.addResource(fileResource);
}
// check if loading of old key with multiple new-key mappings actually loads // check if loading of old key with multiple new-key mappings actually loads
// the corresponding new keys. // the corresponding new keys.
@ -150,11 +152,13 @@ public class TestConfigurationDeprecation {
assertEquals("d", conf.get("D")); assertEquals("d", conf.get("D"));
out=new BufferedWriter(new FileWriter(CONFIG2)); out=new BufferedWriter(new FileWriter(CONFIG2));
startConfig(); try(AutoCloseable closeable = out::close) {
// load the old/new keys corresponding to the keys loaded before. startConfig();
appendProperty("B", "b"); // load the old/new keys corresponding to the keys loaded before.
appendProperty("C", "c"); appendProperty("B", "b");
endConfig(); appendProperty("C", "c");
endConfig();
}
Path fileResource1 = new Path(CONFIG2); Path fileResource1 = new Path(CONFIG2);
conf.addResource(fileResource1); conf.addResource(fileResource1);
@ -191,22 +195,24 @@ public class TestConfigurationDeprecation {
* @throws IOException * @throws IOException
*/ */
@Test @Test
public void testDeprecationForFinalParameters() throws IOException { public void testDeprecationForFinalParameters() throws Exception {
addDeprecationToConfiguration(); addDeprecationToConfiguration();
out=new BufferedWriter(new FileWriter(CONFIG)); out=new BufferedWriter(new FileWriter(CONFIG));
startConfig(); try(AutoCloseable closeable = out::close) {
// set the following keys: startConfig();
// 1.old key and final // set the following keys:
// 2.new key whose corresponding old key is final // 1.old key and final
// 3.old key whose corresponding new key is final // 2.new key whose corresponding old key is final
// 4.new key and final // 3.old key whose corresponding new key is final
// 5.new key which is final and has null value. // 4.new key and final
appendProperty("A", "a", true); // 5.new key which is final and has null value.
appendProperty("D", "d"); appendProperty("A", "a", true);
appendProperty("E", "e"); appendProperty("D", "d");
appendProperty("H", "h", true); appendProperty("E", "e");
appendProperty("J", "", true); appendProperty("H", "h", true);
endConfig(); appendProperty("J", "", true);
endConfig();
}
Path fileResource = new Path(CONFIG); Path fileResource = new Path(CONFIG);
conf.addResource(fileResource); conf.addResource(fileResource);
@ -222,14 +228,16 @@ public class TestConfigurationDeprecation {
assertNull(conf.get("J")); assertNull(conf.get("J"));
out=new BufferedWriter(new FileWriter(CONFIG2)); out=new BufferedWriter(new FileWriter(CONFIG2));
startConfig(); try(AutoCloseable closeable = out::close) {
// add the corresponding old/new keys of those added to CONFIG1 startConfig();
appendProperty("B", "b"); // add the corresponding old/new keys of those added to CONFIG1
appendProperty("C", "c", true); appendProperty("B", "b");
appendProperty("F", "f", true); appendProperty("C", "c", true);
appendProperty("G", "g"); appendProperty("F", "f", true);
appendProperty("I", "i"); appendProperty("G", "g");
endConfig(); appendProperty("I", "i");
endConfig();
}
Path fileResource1 = new Path(CONFIG2); Path fileResource1 = new Path(CONFIG2);
conf.addResource(fileResource1); conf.addResource(fileResource1);
@ -245,20 +253,22 @@ public class TestConfigurationDeprecation {
assertNull(conf.get("J")); assertNull(conf.get("J"));
out=new BufferedWriter(new FileWriter(CONFIG3)); out=new BufferedWriter(new FileWriter(CONFIG3));
startConfig(); try(AutoCloseable closeable = out::close) {
// change the values of all the previously loaded startConfig();
// keys (both deprecated and new) // change the values of all the previously loaded
appendProperty("A", "a1"); // keys (both deprecated and new)
appendProperty("B", "b1"); appendProperty("A", "a1");
appendProperty("C", "c1"); appendProperty("B", "b1");
appendProperty("D", "d1"); appendProperty("C", "c1");
appendProperty("E", "e1"); appendProperty("D", "d1");
appendProperty("F", "f1"); appendProperty("E", "e1");
appendProperty("G", "g1"); appendProperty("F", "f1");
appendProperty("H", "h1"); appendProperty("G", "g1");
appendProperty("I", "i1"); appendProperty("H", "h1");
appendProperty("J", "j1"); appendProperty("I", "i1");
endConfig(); appendProperty("J", "j1");
endConfig();
}
fileResource = new Path(CONFIG); fileResource = new Path(CONFIG);
conf.addResource(fileResource); conf.addResource(fileResource);
@ -440,17 +450,14 @@ public class TestConfigurationDeprecation {
final String newZkAddressKey = CommonConfigurationKeys.ZK_ADDRESS; final String newZkAddressKey = CommonConfigurationKeys.ZK_ADDRESS;
final String zkAddressValue = "dummyZkAddress"; final String zkAddressValue = "dummyZkAddress";
try{ out = new BufferedWriter(new FileWriter(CONFIG4));
out = new BufferedWriter(new FileWriter(CONFIG4)); try(AutoCloseable closeable = out::close){
startConfig(); startConfig();
appendProperty(oldZkAddressKey, zkAddressValue); appendProperty(oldZkAddressKey, zkAddressValue);
endConfig(); endConfig();
Path fileResource = new Path(CONFIG4);
conf.addResource(fileResource);
} finally {
out.close();
} }
Path fileResource = new Path(CONFIG4);
conf.addResource(fileResource);
// ACT // ACT
conf.get(oldZkAddressKey); conf.get(oldZkAddressKey);