diff options
author | Arne Juul <arnej@yahoo-inc.com> | 2019-05-23 08:38:28 +0000 |
---|---|---|
committer | Arne Juul <arnej@yahoo-inc.com> | 2019-05-23 08:39:03 +0000 |
commit | a7b7721e1a2e9d0f18b1a5b89c2ca9e312bee940 (patch) | |
tree | b2074ec405512f13893ae82152d0155d0d619ff6 /logserver | |
parent | 5400cc9c394115da77c45bb5ca468f714f40aa96 (diff) |
drop manual cleaning of temporary folder
Diffstat (limited to 'logserver')
2 files changed, 139 insertions, 153 deletions
diff --git a/logserver/src/test/java/com/yahoo/logserver/handlers/archive/ArchiverHandlerTestCase.java b/logserver/src/test/java/com/yahoo/logserver/handlers/archive/ArchiverHandlerTestCase.java index 6fe8b3f0436..1c6b3e2a6ad 100644 --- a/logserver/src/test/java/com/yahoo/logserver/handlers/archive/ArchiverHandlerTestCase.java +++ b/logserver/src/test/java/com/yahoo/logserver/handlers/archive/ArchiverHandlerTestCase.java @@ -1,7 +1,6 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.logserver.handlers.archive; -import com.yahoo.io.IOUtils; import com.yahoo.log.InvalidLogFormatException; import com.yahoo.log.LogMessage; import com.yahoo.plugin.SystemPropertyConfig; @@ -28,10 +27,10 @@ import static org.junit.Assert.fail; public class ArchiverHandlerTestCase { private static final String[] mStrings = { - "1095159244.095000\thost\t1/2\tservice\tcomponent\tinfo\tpayload1", - "1095206399.000000\thost\t1/2\tservice\tcomponent\tinfo\tpayload2", - "1095206400.000000\thost\t1/2\tservice\tcomponent\tinfo\tpayload3", - "1095206401.000000\thost\t1/2\tservice\tcomponent\tinfo\tpayload4", + "1095159244.095000\thost\t1/2\tservice\tcomponent\tinfo\tpayload1", + "1095206399.000000\thost\t1/2\tservice\tcomponent\tinfo\tpayload2", + "1095206400.000000\thost\t1/2\tservice\tcomponent\tinfo\tpayload3", + "1095206401.000000\thost\t1/2\tservice\tcomponent\tinfo\tpayload4", }; private static final LogMessage[] msg = new LogMessage[mStrings.length]; @@ -63,19 +62,16 @@ public class ArchiverHandlerTestCase { @Test public void testDateHash() throws IOException { File tmpDir = temporaryFolder.newFolder(); - try { - ArchiverHandler a = new ArchiverHandler(tmpDir.getAbsolutePath(), - 1024); - long now = 1095159244095L; - long midnight = 1095206400000L; - assertEquals(2004091410, a.dateHash(now)); - assertEquals(2004091423, a.dateHash(midnight - 1)); - assertEquals(2004091500, a.dateHash(midnight)); - assertEquals(2004091500, a.dateHash(midnight + 1)); - a.close(); - } finally { - IOUtils.recursiveDeleteDir(tmpDir); - } + + ArchiverHandler a = new ArchiverHandler(tmpDir.getAbsolutePath(), + 1024); + long now = 1095159244095L; + long midnight = 1095206400000L; + assertEquals(2004091410, a.dateHash(now)); + assertEquals(2004091423, a.dateHash(midnight - 1)); + assertEquals(2004091500, a.dateHash(midnight)); + assertEquals(2004091500, a.dateHash(midnight + 1)); + a.close(); } /** @@ -95,8 +91,6 @@ public class ArchiverHandlerTestCase { a.close(); } catch (InvalidLogFormatException e) { fail(e.toString()); - } finally { - IOUtils.recursiveDeleteDir(tmpDir); } } @@ -107,66 +101,63 @@ public class ArchiverHandlerTestCase { @Test public void testLogging() throws java.io.IOException, InvalidLogFormatException { File tmpDir = temporaryFolder.newFolder(); - try { - ArchiverHandler a = new ArchiverHandler(tmpDir.getAbsolutePath(), - 1024); - for (int i = 0; i < msg.length; i++) { - a.handle(msg[i]); - } - a.close(); + ArchiverHandler a = new ArchiverHandler(tmpDir.getAbsolutePath(), + 1024); + + for (int i = 0; i < msg.length; i++) { + a.handle(msg[i]); + } + a.close(); - // make sure all the log files are there, that all log entries - // are accounted for and that they match what we logged. - int messageCount = 0; + // make sure all the log files are there, that all log entries + // are accounted for and that they match what we logged. + int messageCount = 0; - // map of files we've already inspected. this we need to ensure - // that we are not inspecting the same files over and over again - // so the counts get messed up - Set<String> inspectedFiles = new HashSet<String>(); + // map of files we've already inspected. this we need to ensure + // that we are not inspecting the same files over and over again + // so the counts get messed up + Set<String> inspectedFiles = new HashSet<String>(); - for (int i = 0; i < msg.length; i++) { - String name = a.getPrefix(msg[i]) + "-0"; + for (int i = 0; i < msg.length; i++) { + String name = a.getPrefix(msg[i]) + "-0"; + + // have we inspected this file before? + if (! inspectedFiles.add(name)) { + continue; + } - // have we inspected this file before? - if (! inspectedFiles.add(name)) { - continue; - } + File f = new File(name); + assertTrue(f.exists()); - File f = new File(name); - assertTrue(f.exists()); - - BufferedReader br = new BufferedReader(new FileReader(f)); - for (String line = br.readLine(); - line != null; - line = br.readLine()) { - // primitive check if the messages match - boolean foundMatch = false; - for (int k = 0; k < mStrings.length; k++) { - if (mStrings[k].equals(line)) { - foundMatch = true; - break; - } + BufferedReader br = new BufferedReader(new FileReader(f)); + for (String line = br.readLine(); + line != null; + line = br.readLine()) { + // primitive check if the messages match + boolean foundMatch = false; + for (int k = 0; k < mStrings.length; k++) { + if (mStrings[k].equals(line)) { + foundMatch = true; + break; } - assertTrue(foundMatch); + } + assertTrue(foundMatch); - // try to instantiate messages to ensure that they - // are parseable - @SuppressWarnings("unused") + // try to instantiate messages to ensure that they + // are parseable + @SuppressWarnings("unused") LogMessage m = LogMessage.parseNativeFormat(line); - messageCount++; - } - br.close(); + messageCount++; } - - // verify that the number of log messages written equals - // the number of log messages we have in our test - assertEquals(mStrings.length, messageCount); - } finally { - IOUtils.recursiveDeleteDir(tmpDir); + br.close(); } + + // verify that the number of log messages written equals + // the number of log messages we have in our test + assertEquals(mStrings.length, messageCount); } /** @@ -175,39 +166,39 @@ public class ArchiverHandlerTestCase { @Test public void testRotation() throws IOException { File tmpDir = temporaryFolder.newFolder(); - try { - ArchiverHandler a = new ArchiverHandler(tmpDir.getAbsolutePath(), - msg[1].toString().length() + 1); - // log the same message 4 times - for (int i = 0; i < 4; i++) { - a.handle(msg[1]); - } - a.close(); - // we should now have 3 files - String prefix = a.getPrefix(msg[1]); - int msgCount = 0; - for (int i = 0; i < 3; i++) { - File f = new File(prefix + "-" + i); - assertTrue(f.exists()); - - // ensure there's the same log message in all files - BufferedReader br = new BufferedReader(new FileReader(f)); - for (String line = br.readLine(); - line != null; - line = br.readLine()) { - assertTrue(msg[1].toString().equals((line + "\n"))); - msgCount++; - } - br.close(); + ArchiverHandler a = new ArchiverHandler(tmpDir.getAbsolutePath(), + msg[1].toString().length() + 1); + // log the same message 4 times + for (int i = 0; i < 4; i++) { + a.handle(msg[1]); + } + a.close(); + + // we should now have 3 files + String prefix = a.getPrefix(msg[1]); + int msgCount = 0; + for (int i = 0; i < 3; i++) { + File f = new File(prefix + "-" + i); + assertTrue(f.exists()); + + // ensure there's the same log message in all files + BufferedReader br = new BufferedReader(new FileReader(f)); + for (String line = br.readLine(); + line != null; + line = br.readLine()) { + assertTrue(msg[1].toString().equals((line + "\n"))); + msgCount++; } - assertEquals(4, msgCount); - - // make sure we have no more than 3 files - assertTrue(! (new File(prefix + "-3").exists())); - } finally { - IOUtils.recursiveDeleteDir(tmpDir); + br.close(); } + assertEquals(4, msgCount); + + // make sure we have no more than 3 files + assertTrue(! (new File(prefix + "-3").exists())); + + + } @Test diff --git a/logserver/src/test/java/com/yahoo/logserver/handlers/archive/FilesArchivedTestCase.java b/logserver/src/test/java/com/yahoo/logserver/handlers/archive/FilesArchivedTestCase.java index 053e8613937..b04354d0b67 100644 --- a/logserver/src/test/java/com/yahoo/logserver/handlers/archive/FilesArchivedTestCase.java +++ b/logserver/src/test/java/com/yahoo/logserver/handlers/archive/FilesArchivedTestCase.java @@ -2,8 +2,6 @@ package com.yahoo.logserver.handlers.archive; -import com.yahoo.io.IOUtils; - import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; @@ -43,60 +41,57 @@ public class FilesArchivedTestCase { @Test public void testMaintenance() throws java.io.IOException { File tmpDir = temporaryFolder.newFolder(); - try { - makeLogfile(tmpDir, "foo/bar", 35*24); // non-matching file - - makeLogfile(tmpDir, "2018/11/20/13-0", 35*24); - makeLogfile(tmpDir, "2018/11/21/13-0", 34*24); - makeLogfile(tmpDir, "2018/12/28/13-0", 3*24); - makeLogfile(tmpDir, "2018/12/29/13-0", 2*24); - makeLogfile(tmpDir, "2018/12/30/13-0", 1*24); - makeLogfile(tmpDir, "2018/12/31/14-0", 3); - makeLogfile(tmpDir, "2018/12/31/16-0", 1); - makeLogfile(tmpDir, "2018/12/31/17-0", 0); - dumpFiles(tmpDir, "before archive maintenance"); - FilesArchived a = new FilesArchived(tmpDir); - dumpFiles(tmpDir, "after archive maintenance"); - checkExist(tmpDir, "foo/bar"); - checkExist(tmpDir, "2018/12/31/17-0"); - checkExist(tmpDir, "2018/12/31/16-0"); - checkExist(tmpDir, "2018/12/31/14-0.gz"); - checkExist(tmpDir, "2018/12/28/13-0.gz"); - checkExist(tmpDir, "2018/12/29/13-0.gz"); - checkExist(tmpDir, "2018/12/30/13-0.gz"); - - checkNoExist(tmpDir, "2018/12/31/17-0.gz"); - checkNoExist(tmpDir, "2018/12/31/16-0.gz"); - checkNoExist(tmpDir, "2018/12/31/14-0"); - checkNoExist(tmpDir, "2018/12/28/13-0"); - checkNoExist(tmpDir, "2018/12/29/13-0"); - checkNoExist(tmpDir, "2018/12/30/13-0"); - - checkNoExist(tmpDir, "2018/11/20/13-0"); - checkNoExist(tmpDir, "2018/11/20/13-0.gz"); - checkNoExist(tmpDir, "2018/11/21/13-0"); - checkNoExist(tmpDir, "2018/11/21/13-0.gz"); - - makeLogfile(tmpDir, "2018/12/31/16-0", 3); - makeLogfile(tmpDir, "2018/12/31/17-0", 3); - makeLogfile(tmpDir, "2018/12/31/17-1", 1); - makeLogfile(tmpDir, "2018/12/31/17-2", 0); - - dumpFiles(tmpDir, "before second archive maintenance"); - a.maintenance(); - dumpFiles(tmpDir, "after second archive maintenance"); - - checkExist(tmpDir, "2018/12/31/17-2"); - checkExist(tmpDir, "2018/12/31/17-1"); - checkExist(tmpDir, "2018/12/31/16-0.gz"); - checkExist(tmpDir, "2018/12/31/17-0.gz"); - - checkNoExist(tmpDir, "2018/12/31/16-0"); - checkNoExist(tmpDir, "2018/12/31/17-0"); - checkExist(tmpDir, "foo/bar"); - } finally { - IOUtils.recursiveDeleteDir(tmpDir); - } + + makeLogfile(tmpDir, "foo/bar", 35*24); // non-matching file + + makeLogfile(tmpDir, "2018/11/20/13-0", 35*24); + makeLogfile(tmpDir, "2018/11/21/13-0", 34*24); + makeLogfile(tmpDir, "2018/12/28/13-0", 3*24); + makeLogfile(tmpDir, "2018/12/29/13-0", 2*24); + makeLogfile(tmpDir, "2018/12/30/13-0", 1*24); + makeLogfile(tmpDir, "2018/12/31/14-0", 3); + makeLogfile(tmpDir, "2018/12/31/16-0", 1); + makeLogfile(tmpDir, "2018/12/31/17-0", 0); + dumpFiles(tmpDir, "before archive maintenance"); + FilesArchived a = new FilesArchived(tmpDir); + dumpFiles(tmpDir, "after archive maintenance"); + checkExist(tmpDir, "foo/bar"); + checkExist(tmpDir, "2018/12/31/17-0"); + checkExist(tmpDir, "2018/12/31/16-0"); + checkExist(tmpDir, "2018/12/31/14-0.gz"); + checkExist(tmpDir, "2018/12/28/13-0.gz"); + checkExist(tmpDir, "2018/12/29/13-0.gz"); + checkExist(tmpDir, "2018/12/30/13-0.gz"); + + checkNoExist(tmpDir, "2018/12/31/17-0.gz"); + checkNoExist(tmpDir, "2018/12/31/16-0.gz"); + checkNoExist(tmpDir, "2018/12/31/14-0"); + checkNoExist(tmpDir, "2018/12/28/13-0"); + checkNoExist(tmpDir, "2018/12/29/13-0"); + checkNoExist(tmpDir, "2018/12/30/13-0"); + + checkNoExist(tmpDir, "2018/11/20/13-0"); + checkNoExist(tmpDir, "2018/11/20/13-0.gz"); + checkNoExist(tmpDir, "2018/11/21/13-0"); + checkNoExist(tmpDir, "2018/11/21/13-0.gz"); + + makeLogfile(tmpDir, "2018/12/31/16-0", 3); + makeLogfile(tmpDir, "2018/12/31/17-0", 3); + makeLogfile(tmpDir, "2018/12/31/17-1", 1); + makeLogfile(tmpDir, "2018/12/31/17-2", 0); + + dumpFiles(tmpDir, "before second archive maintenance"); + a.maintenance(); + dumpFiles(tmpDir, "after second archive maintenance"); + + checkExist(tmpDir, "2018/12/31/17-2"); + checkExist(tmpDir, "2018/12/31/17-1"); + checkExist(tmpDir, "2018/12/31/16-0.gz"); + checkExist(tmpDir, "2018/12/31/17-0.gz"); + + checkNoExist(tmpDir, "2018/12/31/16-0"); + checkNoExist(tmpDir, "2018/12/31/17-0"); + checkExist(tmpDir, "foo/bar"); } private void dumpFiles(File dir, String header) { |