Skip to content

Commit

Permalink
GH-2577: No synchronized in spring-rabbit-test
Browse files Browse the repository at this point in the history
Fixes: #2577

* Use `ConcurrentHashMap.newKeySet()` for `exceptions` property in `Answer` impls
  • Loading branch information
artembilan committed Dec 16, 2023
1 parent f4cb1a0 commit 243ae93
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2022 the original author or authors.
* Copyright 2016-2023 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 All @@ -17,9 +17,9 @@
package org.springframework.amqp.rabbit.test.mockito;

import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import org.mockito.internal.stubbing.defaultanswers.ForwardsInvocations;
import org.mockito.invocation.InvocationOnMock;
Expand All @@ -33,6 +33,8 @@
* @param <T> the return type.
*
* @author Gary Russell
* @author Artem Bilan
*
* @since 1.6
*
*/
Expand All @@ -43,7 +45,7 @@ public class LambdaAnswer<T> extends ForwardsInvocations {

private final ValueToReturn<T> callback;

private final Set<Exception> exceptions = Collections.synchronizedSet(new LinkedHashSet<>());
private final Set<Exception> exceptions = ConcurrentHashMap.newKeySet();

private final boolean hasDelegate;

Expand Down Expand Up @@ -88,9 +90,7 @@ public T answer(InvocationOnMock invocation) throws Throwable {
* @since 2.2.3
*/
public Collection<Exception> getExceptions() {
synchronized (this.exceptions) {
return new LinkedHashSet<>(this.exceptions);
}
return new LinkedHashSet<>(this.exceptions);
}

@FunctionalInterface
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2022 the original author or authors.
* Copyright 2016-2023 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 All @@ -17,9 +17,9 @@
package org.springframework.amqp.rabbit.test.mockito;

import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

Expand All @@ -33,6 +33,8 @@
* method and counts down a latch. Captures any exceptions thrown.
*
* @author Gary Russell
* @author Artem Bilan
*
* @since 1.6
*
*/
Expand All @@ -42,7 +44,7 @@ public class LatchCountDownAndCallRealMethodAnswer extends ForwardsInvocations {

private final transient CountDownLatch latch;

private final Set<Exception> exceptions = Collections.synchronizedSet(new LinkedHashSet<>());
private final Set<Exception> exceptions = ConcurrentHashMap.newKeySet();

private final boolean hasDelegate;

Expand Down Expand Up @@ -101,9 +103,7 @@ public CountDownLatch getLatch() {
*/
@Nullable
public Collection<Exception> getExceptions() {
synchronized (this.exceptions) {
return new LinkedHashSet<>(this.exceptions);
}
return new LinkedHashSet<>(this.exceptions);
}

}

0 comments on commit 243ae93

Please sign in to comment.