Skip to content
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

Fixed a bug in AttributeMap, which causes a ConcurrentModificationException… #5559

Merged
merged 2 commits into from
Sep 10, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -327,17 +327,17 @@ private void internalPut(Key<?> key, Value<?> value) {
*/
private Value<?> internalComputeIfAbsent(Key<?> key, Supplier<Value<?>> value) {
checkCopyOnUpdate();
return attributes.compute(key, (k, v) -> {
if (v == null || resolveValue(v) == null) {
Value<?> newValue = value.get();
Validate.notNull(newValue, "Supplied value must not be null.");
if (v != null) {
dependencyGraph.valueUpdated(v, newValue);
}
return newValue;
Value<?> currentValue = attributes.get(key);
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks good. Is there any way in which we tested this ? If a unit test is difficult, A one-time runnable test that validates these changes would be helpful here. Lmk if its non-trivial to test / validate

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This new behavior is exercised in #5562. Let me see if I can create a standalone test for it, for the purposes of this PR.

if (currentValue == null || resolveValue(currentValue) == null) {
Value<?> newValue = value.get();
Validate.notNull(newValue, "Supplied value must not be null.");
if (currentValue != null) {
dependencyGraph.valueUpdated(currentValue, newValue);
}
return v;
});
attributes.put(key, newValue);
return newValue;
}
return currentValue;
}

private void checkCopyOnUpdate() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.mockito.Mockito;
import org.w3c.dom.Attr;

public class AttributeMapTest {

Expand Down Expand Up @@ -158,6 +157,20 @@ public void lazyAttributes_notReResolvedAfterToBuilderBuild() {
verify(lazyRead, Mockito.times(1)).run();
}

@Test
public void lazyAttributes_canUpdateTheMap_andBeUpdatedWithPutLazyIfAbsent() {
AttributeMap.Builder map = AttributeMap.builder();
map.putLazyIfAbsent(STRING_KEY, c -> {
// Force a modification to the underlying map. We wouldn't usually do this so explicitly, but
// this can happen internally within AttributeMap when resolving uncached lazy values,
// so it needs to be possible.
map.put(INTEGER_KEY, 0);
return "string";
});
map.putLazyIfAbsent(STRING_KEY, c -> "string"); // Force the value to be resolved within the putLazyIfAbsent
assertThat(map.get(STRING_KEY)).isEqualTo("string");
}

@Test
public void changesInBuilder_doNotAffectBuiltMap() {
AttributeMap.Builder builder = mapBuilderWithLazyString();
Expand Down
Loading