private void populate(RollingStorage s, SegmentHandle writeHandle, int writeCount, int smallWriteSize, int largeWriteSize, ByteArrayOutputStream writeStream) throws Exception { final Random rnd = new Random(0); int offset = (int) s.getStreamSegmentInfo(writeHandle.getSegmentName()).getLength(); for (int i = 0; i < writeCount; i++) { byte[] appendData = new byte[i % 2 == 0 ? smallWriteSize : largeWriteSize]; rnd.nextBytes(appendData); s.write(writeHandle, offset, new ByteArrayInputStream(appendData), appendData.length); offset += appendData.length; if (writeStream != null) { writeStream.write(appendData); } } }
/** * Tests the ability to auto-refresh a Write Handle upon offset disagreement. */ @Test public void testRefreshHandleBadOffset() throws Exception { // Write small and large writes, alternatively. @Cleanup val baseStorage = new InMemoryStorage(); @Cleanup val s = new RollingStorage(baseStorage, DEFAULT_ROLLING_POLICY); s.initialize(1); s.create(SEGMENT_NAME); val h1 = s.openWrite(SEGMENT_NAME); val h2 = s.openWrite(SEGMENT_NAME); // Open now, before writing, so we force a refresh. byte[] data = "data".getBytes(); s.write(h1, 0, new ByteArrayInputStream(data), data.length); s.write(h2, data.length, new ByteArrayInputStream(data), data.length); // Check that no file has exceeded its maximum length. byte[] expectedData = new byte[data.length * 2]; System.arraycopy(data, 0, expectedData, 0, data.length); System.arraycopy(data, 0, expectedData, data.length, data.length); checkWrittenData(expectedData, h2, s); }