-
Notifications
You must be signed in to change notification settings - Fork 4
Seed-Based Regression Testing Implementation in Java #303
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6d8327e
Implement seed storing for Java schema tests . Remember seeds of fail…
b7ee108
Implement seed storing for Java schema tests . Remember seeds of fail…
28dc031
Resolving CoPilot comments
5c98724
Update stefc/generator/testdata/seeds/java/README.md
abhisheksplunk 733c3c3
Merge branch 'main' into abhishek/java-seed-impl
ae80255
Regenerated the files after reverting the changes with protoc version…
8b2a84c
Regenerated the files after reverting the changes with protoc version…
e95c256
Regenerated the files after reverting the changes with protoc version…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| This directory stores **random seeds that previously caused failures in Java-generated schema tests** (e.g. `*WriterTest`). | ||
|
|
||
| ## Why this exists | ||
|
|
||
| - When a randomized Java writer/reader test fails, it prints the seed and **appends it** to the appropriate `*_seeds.txt` file in this directory. | ||
| - On subsequent test runs, the seeds in these files are **replayed first** to ensure the bug does not regress. | ||
|
|
||
| ## File naming convention | ||
| Each file is named: | ||
| `<javaPackage>_<RootStruct>_seeds.txt` | ||
| Example: | ||
| `com.example.gentest.json_like_Record_seeds.txt` | ||
|
|
||
| ## Important | ||
| - Only add seeds that were found by **Java** failures. Go/other-language seeds are not portable because RNGs differ. |
1 change: 1 addition & 0 deletions
1
stefc/generator/testdata/seeds/java/com.example.gentest.json_like_Record_seeds.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| 680413050164375 |
2 changes: 2 additions & 0 deletions
2
...c/generator/testdata/seeds/java/com.example.gentest.optional_reset_fail_Struct1_seeds.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| 1765233907441407000 | ||
| 1765234703981295000 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,10 @@ import static org.junit.jupiter.api.Assertions.*; | |
|
|
||
| import java.io.ByteArrayInputStream; | ||
| import java.io.EOFException; | ||
| 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; | ||
|
|
@@ -35,8 +39,9 @@ class {{.StructName}}WriterTest { | |
| return records; | ||
| } | ||
|
|
||
| @Test | ||
| void test{{.StructName}}WriteRead() throws Exception { | ||
| boolean test{{.StructName}}WriteReadSeed(long seed) { | ||
| boolean retVal = true; | ||
|
|
||
| List<WriterOptions.Builder> opts = Arrays.asList( | ||
| WriterOptions.builder(), | ||
| WriterOptions.builder().compression(Compression.Zstd), | ||
|
|
@@ -61,12 +66,9 @@ class {{.StructName}}WriterTest { | |
| WriterOptions.builder().frameRestartFlags(FrameFlags.RestartCodecs).maxUncompressedFrameByteSize(500) | ||
| ); | ||
|
|
||
| // Choose a seed (non-pseudo) randomly. We will print the seed | ||
| // on failure for easy reproduction. | ||
| long seed1 = System.nanoTime(); | ||
| Random random = new Random(seed1); | ||
| Random random = new Random(seed); | ||
|
|
||
| for (int optIdx=0; optIdx < opts.size(); optIdx++) { | ||
| for (int optIdx = 0; optIdx < opts.size(); optIdx++) { | ||
| WriterOptions.Builder opt = opts.get(optIdx); | ||
| try { | ||
| MemChunkWriter buf = new MemChunkWriter(); | ||
|
|
@@ -85,12 +87,44 @@ class {{.StructName}}WriterTest { | |
| {{.StructName}}Reader reader = new {{.StructName}}Reader(new ByteArrayInputStream(buf.getBytes())); | ||
| for (int i = 0; i < records.size(); i++) { | ||
| assertEquals(ReadResult.Success, reader.read(ReadOptions.none)); | ||
| assertTrue(reader.record.equals(records.get(i)), "record " + i + " seed " + seed1 + " optIdx " + optIdx); | ||
| assertTrue(reader.record.equals(records.get(i)), "record " + i + " seed " + seed + " optIdx " + optIdx); | ||
| } | ||
| assertThrows(EOFException.class, () -> reader.read(ReadOptions.none)); | ||
| } catch (Exception e) { | ||
| fail("seed " + seed1 + " optIdx " + optIdx + ": " + e.getMessage()); | ||
| System.out.printf("Test failed with seed %d optIdx %d: %s%n", seed, optIdx, e.getMessage()); | ||
| e.printStackTrace(); | ||
| retVal = false; | ||
tigrannajaryan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| return retVal; | ||
| } | ||
|
|
||
| @Test | ||
| void test{{.StructName}}WriteRead() throws Exception { | ||
| // Seed files are stored in-repo so previously-failing seeds can be replayed to prevent regressions. | ||
| Path seedFilePath = Paths.get("../stefc/generator/testdata/seeds/java/{{ .PackageName }}_{{.StructName}}_seeds.txt"); | ||
tigrannajaryan marked this conversation as resolved.
Show resolved
Hide resolved
tigrannajaryan marked this conversation as resolved.
Show resolved
Hide resolved
tigrannajaryan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if (Files.exists(seedFilePath)) { | ||
| for (String line : Files.readAllLines(seedFilePath)) { | ||
abhisheksplunk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| String s = line.trim(); | ||
| if (s.isEmpty()) continue; | ||
| long seed = Long.parseLong(s); | ||
tigrannajaryan marked this conversation as resolved.
Show resolved
Hide resolved
tigrannajaryan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| System.out.printf("Testing with seed from file: %d%n", seed); | ||
| boolean passed = test{{.StructName}}WriteReadSeed(seed); | ||
| if (!passed) { | ||
| fail("Previously-failing seed " + seed + " still fails"); | ||
| } | ||
| } | ||
tigrannajaryan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| long seed = System.nanoTime(); | ||
abhisheksplunk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| boolean succeeded = test{{.StructName}}WriteReadSeed(seed); | ||
| if (!succeeded) { | ||
| System.out.printf("Test failed with seed %d, adding to seed file%n", seed); | ||
| Files.createDirectories(seedFilePath.getParent()); | ||
| Files.writeString(seedFilePath, seed + "\n", StandardOpenOption.CREATE, StandardOpenOption.APPEND); | ||
tigrannajaryan marked this conversation as resolved.
Show resolved
Hide resolved
tigrannajaryan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| fail("Test failed with seed " + seed); | ||
tigrannajaryan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.