-
Notifications
You must be signed in to change notification settings - Fork 426
fix(java): reproduce silent Set child-loss for package-private nodes #3902
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
147 changes: 147 additions & 0 deletions
147
...rc/test/java/org/apache/fory/codegen/pkgprivate/PackagePrivateCyclicSetChildLossTest.java
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,147 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.apache.fory.codegen.pkgprivate; | ||
|
|
||
| import static org.testng.Assert.assertEquals; | ||
| import static org.testng.Assert.assertTrue; | ||
|
|
||
| import java.io.Serializable; | ||
| import java.util.ArrayList; | ||
| import java.util.EnumMap; | ||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
| import java.util.LinkedHashSet; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
| import java.util.Set; | ||
| import org.apache.fory.Fory; | ||
| import org.apache.fory.ThreadSafeFory; | ||
| import org.testng.annotations.Test; | ||
|
|
||
| /** | ||
| * Regression test: a wide fan-out (one parent, many children) cyclic graph of package-private | ||
| * nodes silently loses entries from a {@code Set<PackagePrivateType>} field during | ||
| * deserialization - no exception is thrown, so unlike {@link PackagePrivateMapKeyTest} (which | ||
| * catches the codegen CompileException for this same field shape) this defect only surfaces as | ||
| * missing data. | ||
| * | ||
| * <p>Reproduced from a production graph with 100+ children under one parent node; the | ||
| * child->parent edge (an EnumMap-keyed back-reference) survives the round trip intact, but the | ||
| * parent->child edge (a plain HashSet) loses a handful of entries. Both edges are set together, | ||
| * atomically, at construction time, so the source object graph itself is never inconsistent - | ||
| * the asymmetry is introduced purely by fory's serialize/deserialize round trip. | ||
| */ | ||
| public class PackagePrivateCyclicSetChildLossTest { | ||
|
|
||
| private static final int CHILD_COUNT = 200; | ||
|
|
||
| @Test | ||
| public void testWideFanOutDoesNotLoseChildrenFromPackagePrivateSet() { | ||
| ThreadSafeFory fury = | ||
| Fory.builder() | ||
| .withXlang(false) | ||
| .requireClassRegistration(false) | ||
| .withRefTracking(true) | ||
| .withCompatible(false) | ||
| .buildThreadSafeFory(); | ||
|
|
||
| FanOutContainer container = new FanOutContainer("v1"); | ||
| FanOutNode parent = new FanOutNode(FanOutType.TYPE_A, "parent"); | ||
| container.nodes.computeIfAbsent(FanOutType.TYPE_A, k -> new HashMap<>()).put(parent.id, parent); | ||
|
|
||
| List<String> expectedChildIds = new ArrayList<>(); | ||
| for (int i = 0; i < CHILD_COUNT; i++) { | ||
| FanOutNode child = new FanOutNode(FanOutType.TYPE_B, "child-" + i); | ||
| parent.children.add(child); | ||
| child.parents.computeIfAbsent(parent.type, k -> new LinkedHashSet<>()).add(parent); | ||
| container.nodes.computeIfAbsent(FanOutType.TYPE_B, k -> new HashMap<>()).put(child.id, child); | ||
| expectedChildIds.add(child.id); | ||
| } | ||
|
|
||
| byte[] bytes = fury.serialize(container); | ||
| FanOutContainer result = (FanOutContainer) fury.deserialize(bytes); | ||
|
|
||
| FanOutNode resultParent = result.nodes.get(FanOutType.TYPE_A).get("parent"); | ||
| assertEquals(resultParent.children.size(), CHILD_COUNT, "parent lost children from its Set field"); | ||
|
|
||
| // Every child must still be reachable BOTH ways: down (parent.children) and up | ||
| // (child.parents), and both directions must point at the exact same object (fory's | ||
| // refTracking should unify identical (type,id) instances, not duplicate them). | ||
| List<String> asymmetric = new ArrayList<>(); | ||
| for (String childId : expectedChildIds) { | ||
| FanOutNode resultChild = result.nodes.get(FanOutType.TYPE_B).get(childId); | ||
| boolean parentListsChild = resultParent.children.contains(resultChild); | ||
| boolean childListsParent = | ||
| resultChild.parents.getOrDefault(FanOutType.TYPE_A, Set.of()).contains(resultParent); | ||
|
chaokunyang marked this conversation as resolved.
Outdated
|
||
| if (!parentListsChild || !childListsParent) { | ||
| asymmetric.add( | ||
| childId + " (parentListsChild=" + parentListsChild + ", childListsParent=" + childListsParent + ")"); | ||
| } | ||
| } | ||
| assertTrue( | ||
| asymmetric.isEmpty(), | ||
| "asymmetric parent<->child edges after round trip (should be empty): " + asymmetric); | ||
| } | ||
| } | ||
|
|
||
| // All package-private — this triggers the bug | ||
| enum FanOutType implements Serializable { | ||
| TYPE_A, | ||
| TYPE_B | ||
| } | ||
|
|
||
| class FanOutNode implements Serializable { | ||
| final FanOutType type; | ||
| final String id; | ||
| final Set<FanOutNode> children = new HashSet<>(); | ||
| final Map<FanOutType, Set<FanOutNode>> parents = new EnumMap<>(FanOutType.class); | ||
|
|
||
| FanOutNode(FanOutType type, String id) { | ||
| this.type = type; | ||
| this.id = id; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) { | ||
| return true; | ||
| } | ||
| if (!(o instanceof FanOutNode)) { | ||
| return false; | ||
| } | ||
| FanOutNode other = (FanOutNode) o; | ||
| return type == other.type && id.equals(other.id); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(type, id); | ||
| } | ||
| } | ||
|
|
||
| class FanOutContainer implements Serializable { | ||
| final Map<FanOutType, Map<String, FanOutNode>> nodes = new EnumMap<>(FanOutType.class); | ||
| final String version; | ||
|
|
||
| FanOutContainer(String version) { | ||
| this.version = version; | ||
| } | ||
| } | ||
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.