Skip to content

Commit

Permalink
Merge branch '3.2.x' into 3.3.x
Browse files Browse the repository at this point in the history
Closes gh-42107
  • Loading branch information
philwebb committed Sep 3, 2024
2 parents ea246d3 + aeafa20 commit 9f38f2b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,12 @@ private LeakSafeThread getLeakSafeThread() {
}

public Object getOrAddAttribute(String name, final ObjectFactory<?> objectFactory) {
return this.attributes.computeIfAbsent(name, (ignore) -> objectFactory.getObject());
Object value = this.attributes.get(name);
if (value == null) {
value = objectFactory.getObject();
this.attributes.put(name, value);
}
return value;
}

public Object removeAttribute(String name) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -145,15 +145,28 @@ void addClassLoaderFiles() {
}

@Test
@SuppressWarnings("rawtypes")
void getOrAddAttributeWithExistingAttribute() {
Restarter.getInstance().getOrAddAttribute("x", () -> "abc");
ObjectFactory objectFactory = mock(ObjectFactory.class);
ObjectFactory<?> objectFactory = mock(ObjectFactory.class);
Object attribute = Restarter.getInstance().getOrAddAttribute("x", objectFactory);
assertThat(attribute).isEqualTo("abc");
then(objectFactory).shouldHaveNoInteractions();
}

@Test
void getOrAddAttributeWithRecursion() {
Restarter restarter = Restarter.getInstance();
Object added = restarter.getOrAddAttribute("postgresContainer", () -> {
restarter.getOrAddAttribute("rabbitContainer", () -> "def");
return "abc";
});
ObjectFactory<?> objectFactory = mock(ObjectFactory.class);
assertThat(added).isEqualTo("abc");
assertThat(restarter.getOrAddAttribute("postgresContainer", objectFactory)).isEqualTo("abc");
assertThat(restarter.getOrAddAttribute("rabbitContainer", objectFactory)).isEqualTo("def");
then(objectFactory).shouldHaveNoInteractions();
}

@Test
void getThreadFactory() throws Exception {
final ClassLoader parentLoader = Thread.currentThread().getContextClassLoader();
Expand Down

0 comments on commit 9f38f2b

Please sign in to comment.