@Test public void testNegativeSizeInHeaderThrows() throws IOException { RandomAccessFile emptyFile = new RandomAccessFile(file, "rwd"); emptyFile.seek(0); emptyFile.writeInt(-2147483648); emptyFile.setLength(INITIAL_LENGTH); emptyFile.getChannel().force(true); emptyFile.close(); try { newQueueFile(); fail("Should have thrown about bad header length"); } catch (IOException ex) { assertThat(ex.getMessage()).isIn( Arrays.asList("File is corrupt; length stored in header (-2147483648) is invalid.", "Unable to read version 0 format. Supported versions are 1 and legacy.")); } }
@Test public void testSizeLessThanHeaderThrows() throws IOException { RandomAccessFile emptyFile = new RandomAccessFile(file, "rwd"); emptyFile.setLength(INITIAL_LENGTH); if (forceLegacy) { emptyFile.writeInt(headerLength - 1); } else { emptyFile.writeInt(0x80000001); emptyFile.writeLong(headerLength - 1); } emptyFile.getChannel().force(true); emptyFile.close(); try { newQueueFile(); fail(); } catch (IOException ex) { assertThat(ex.getMessage()).isIn( Arrays.asList("File is corrupt; length stored in header (15) is invalid.", "File is corrupt; length stored in header (31) is invalid.")); } }
@Test public void singleMaxStaleness() { History minimum = history.get(2); NavigableMap<Timestamp, String> possibleValues = new TreeMap<>(); for (History item : history.subList(2, history.size())) { possibleValues.put(item.timestamp, item.value); } // Pick a staleness that cannot precede the second write (which, in practice, is the staleness // that exceeds the minimum commit time for the subsequent write). long stalenessNanos = System.nanoTime() - history.get(3).minCommitNanoTime; TimestampBound bound = TimestampBound.ofMaxStaleness(stalenessNanos, TimeUnit.NANOSECONDS); ReadOnlyTransaction readContext = client.singleUseReadOnlyTransaction(bound); Struct row = readRow(readContext); assertThat(row).isNotNull(); assertThat(readContext.getReadTimestamp()).isAtLeast(minimum.timestamp); assertThat(row.getString(0)) .isEqualTo(possibleValues.floorEntry(readContext.getReadTimestamp()).getValue()); row = readRow(client.singleUse(bound)); assertThat(row).isNotNull(); assertThat(row.getString(0)).isIn(possibleValues.values()); }
@Test public void singleMinReadTimestamp() { int minimumIndex = 2; History minimum = history.get(minimumIndex); NavigableMap<Timestamp, String> possibleValues = new TreeMap<>(); for (History item : history.subList(minimumIndex, history.size())) { possibleValues.put(item.timestamp, item.value); } TimestampBound bound = TimestampBound.ofMinReadTimestamp(minimum.timestamp); ReadOnlyTransaction readContext = client.singleUseReadOnlyTransaction(bound); Struct row = readRow(readContext); assertThat(row).isNotNull(); assertThat(readContext.getReadTimestamp()).isAtLeast(minimum.timestamp); assertThat(row.getString(0)) .isEqualTo(possibleValues.floorEntry(readContext.getReadTimestamp()).getValue()); row = readRow(client.singleUse(bound)); assertThat(row).isNotNull(); assertThat(row.getString(0)).isIn(possibleValues.values()); }
private void compareNamespaceContexts(NamespaceContext expected, NamespaceContext actual) { for (String prefix : prefixes) { if (prefix != null) { assertEquals("Namespace URI for prefix '" + prefix + "' (" + getLocation() + ")", expected.getNamespaceURI(prefix), actual.getNamespaceURI(prefix)); } } for (String namespaceURI : namespaceURIs) { if (namespaceURI != null && namespaceURI.length() > 0) { Set<String> prefixes = toPrefixSet(expected.getPrefixes(namespaceURI)); assertEquals( "Prefixes for namespace URI '" + namespaceURI + "' (" + getLocation() + ")", prefixes, toPrefixSet(actual.getPrefixes(namespaceURI))); if (prefixes.size() <= 1) { assertEquals( "Prefix for namespace URI '" + namespaceURI + "' (" + getLocation() + ")", expected.getPrefix(namespaceURI), actual.getPrefix(namespaceURI)); } else { assertThat(actual.getPrefix(namespaceURI)).isIn(prefixes); } } } }