From 5efab12063eb7765c1beb37367cb9c2159c6507b Mon Sep 17 00:00:00 2001 From: Andreas Huber Date: Fri, 23 Dec 2016 13:04:05 +0100 Subject: [PATCH] test which verifies the dates in each file are monotonically increasing --- .../org/lucares/performance/db/PdbWriter.java | 9 +++- .../performance/db/TagsToFilesTest.java | 44 +++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/performanceDb/src/main/java/org/lucares/performance/db/PdbWriter.java b/performanceDb/src/main/java/org/lucares/performance/db/PdbWriter.java index 30fa6c4..816f3ae 100644 --- a/performanceDb/src/main/java/org/lucares/performance/db/PdbWriter.java +++ b/performanceDb/src/main/java/org/lucares/performance/db/PdbWriter.java @@ -34,7 +34,6 @@ class PdbWriter implements AutoCloseable { } public void write(final Entry entry) throws WriteException { - // System.out.println(entry); write(entry.getEpochMilli(), entry.getValue()); } @@ -81,6 +80,14 @@ class PdbWriter implements AutoCloseable { } } + public static void writeEntry(final PdbFile pdbFile, final Entry... entries) throws IOException { + try (PdbWriter writer = new PdbWriter(pdbFile)) { + for (final Entry entry : entries) { + writer.write(entry); + } + } + } + @Override public void close() throws IOException { outputStream.flush(); diff --git a/performanceDb/src/test/java/org/lucares/performance/db/TagsToFilesTest.java b/performanceDb/src/test/java/org/lucares/performance/db/TagsToFilesTest.java index e71d9a4..782901b 100644 --- a/performanceDb/src/test/java/org/lucares/performance/db/TagsToFilesTest.java +++ b/performanceDb/src/test/java/org/lucares/performance/db/TagsToFilesTest.java @@ -9,6 +9,7 @@ import java.time.ZoneOffset; import org.lucares.ludb.FieldType; import org.lucares.ludb.H2DB; +import org.lucares.pdb.api.Entry; import org.lucares.pdb.api.Tags; import org.testng.Assert; import org.testng.annotations.AfterMethod; @@ -70,4 +71,47 @@ public class TagsToFilesTest { Assert.assertEquals(fileForDay1, existingFileForDay1); } } + + public void testNewFileIfDateIsTooOld() throws Exception { + + try (H2DB db = new H2DB(new File(dataDirectory.toFile(), "lu.db"))) { + db.createField(Fields.DATE_OFFSET, FieldType.STRING); + + final TagsToFile tagsToFile = new TagsToFile(dataDirectory, db); + + final OffsetDateTime afternoon = DateUtils.getDate(2016, 1, 1, 13, 1, 1); + final OffsetDateTime morning = DateUtils.getDate(2016, 1, 1, 12, 1, 1); + + final Tags tags = Tags.create("myKey", "myValue"); + + final PdbFile fileAfternoon = tagsToFile.getFile(afternoon, tags); + PdbWriter.writeEntry(fileAfternoon, new Entry(afternoon, 1, tags)); + + final PdbFile fileMorning = tagsToFile.getFile(morning, tags); + + Assert.assertNotEquals(fileAfternoon, fileMorning); + } + } + + public void testIdenticalDatesGoIntoSameFile() throws Exception { + + try (H2DB db = new H2DB(new File(dataDirectory.toFile(), "lu.db"))) { + db.createField(Fields.DATE_OFFSET, FieldType.STRING); + + final TagsToFile tagsToFile = new TagsToFile(dataDirectory, db); + + final OffsetDateTime timestamp = DateUtils.getDate(2016, 1, 1, 13, 1, 1); + + final Tags tags = Tags.create("myKey", "myValue"); + + final PdbFile fileA = tagsToFile.getFile(timestamp, tags); + PdbWriter.writeEntry(fileA, new Entry(timestamp, 1, tags)); + + final PdbFile fileB = tagsToFile.getFile(timestamp, tags); + PdbWriter.writeEntry(fileA, new Entry(timestamp, 2, tags)); + + Assert.assertEquals(fileA, fileB); + } + } + }