Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 92 additions & 33 deletions java/src/test/java/tests/ReadWriteTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,56 +15,116 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.fail;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

class ReadWriteTest {
@Test
public void testCopy() throws IOException {
private boolean testCopySeed(long seed) throws IOException {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is the seed used in this test? I can't find any usage?

I don't think we need it here. We want seed saving and load tests generated via writerTest.java.tmpl. This should be equivalent of what we do for Go in this PR: https://github.com/splunk/stef/pull/263/files#diff-6cea806d1350c5a7621b9e3b780184637490a125dbc98f0296224c0fdf2e7eeb

Once writerTest.java.tmpl is modified, you can run make all in various directories (e.g. in go/otel and stef) to generate Java code and then run ./gradlew test to see how the tests work.

When you are happy with writerTest.java.tmpl, running make all in root directory will help confirm everything works correctly.

boolean retVal = true;

List<String> files = Arrays.asList(
"hipstershop-otelmetrics.stefz",
"hostandcollector-otelmetrics.stefz",
"astronomy-otelmetrics.stefz"
);

System.out.printf("%-30s %12s\n", "File", "Uncompressed");

for (String file : files) {
Path path = Paths.get("../benchmarks/testdata/generated/" + file);
path = path.toAbsolutePath();
byte[] stefBytes = Files.readAllBytes(path);
assertNotNull(stefBytes);

// Read using MetricsReader
MetricsReader stefReader = new MetricsReader(new ByteArrayInputStream(stefBytes));
MemChunkWriter cw = new MemChunkWriter();
MetricsWriter stefWriter = new MetricsWriter(cw, WriterOptions.builder().build());


int recCount = 0;
while (true) {
try {
ReadResult result = stefReader.read(ReadOptions.none);
assertEquals(ReadResult.Success, result);
} catch (EOFException e) {
break;
try {
Path path = Paths.get("../benchmarks/testdata/generated/" + file);
path = path.toAbsolutePath();
if (!Files.exists(path)) {
System.out.printf("Skipping %s (not found), seed %d\n", file, seed);
continue;
}

byte[] stefBytes = Files.readAllBytes(path);
assertNotNull(stefBytes);

// Read using MetricsReader
MetricsReader stefReader = new MetricsReader(new ByteArrayInputStream(stefBytes));
MemChunkWriter cw = new MemChunkWriter();
MetricsWriter stefWriter = new MetricsWriter(cw, WriterOptions.builder().build());

int recCount = 0;
while (true) {
try {
ReadResult result = stefReader.read(ReadOptions.none);
assertEquals(ReadResult.Success, result, "seed " + seed);
} catch (EOFException e) {
break;
}

copyModified(stefWriter.record, stefReader.record);
stefWriter.write();
recCount++;
}
stefWriter.flush();
System.out.printf("%-30s %12d (seed %d)\n", file, cw.getBytes().length, seed);

} catch (Exception e) {
System.err.printf("Test failed with seed %d: %s\n", seed, e.getMessage());
e.printStackTrace();
retVal = false;
}
}

return retVal;
}

copyModified(stefWriter.record, stefReader.record);

// Optionally, write the record to the writer
stefWriter.write();
recCount++;
@Test
public void testCopy() throws IOException {
Path seedFilePath = Paths.get("src/test/resources/seeds/ReadWriteTest_seeds.txt");

List<Long> seeds = new ArrayList<>();
if (Files.exists(seedFilePath)) {
String content = Files.readString(seedFilePath).trim();
if (!content.isEmpty()) {
seeds = Arrays.stream(content.split("\n"))
.map(String::trim)
.filter(s -> !s.isEmpty())
.map(Long::parseLong)
.collect(Collectors.toList());
}
}

// Test all previously-failing seeds first
for (Long seed : seeds) {
System.out.printf("Testing with seed from file: %d\n", seed);
boolean passed = testCopySeed(seed);
if (!passed) {
fail("Previously-failing seed " + seed + " still fails");
}
}

long seed = System.nanoTime();
System.out.printf("Testing with new random seed: %d\n", seed);

boolean succeeded = testCopySeed(seed);

if (!succeeded) {
System.out.printf("Test failed with seed %d, adding to seed file\n", seed);

Files.createDirectories(seedFilePath.getParent());

String seedLine = seed + "\n";
if (Files.exists(seedFilePath)) {
Files.writeString(seedFilePath, seedLine,
StandardOpenOption.APPEND, StandardOpenOption.CREATE);
} else {
Files.writeString(seedFilePath, seedLine, StandardOpenOption.CREATE);
}
stefWriter.flush();
System.out.printf("%-30s %12d\n", file, cw.getBytes().length);
fail("Test failed with seed " + seed);
}
}

void copyModified(Metrics dst, Metrics src) {
private void copyModified(Metrics dst, Metrics src) {
if (src.isEnvelopeModified()) {
dst.getEnvelope().copyFrom(src.getEnvelope());
}
Expand All @@ -89,6 +149,5 @@ void copyModified(Metrics dst, Metrics src) {
dst.getPoint().copyFrom(src.getPoint());
}
}

}

3 changes: 3 additions & 0 deletions java/src/test/resources/seeds/ReadWriteTest_seeds.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
1134697243567416
1663904464129333
1726451308442583
Loading