Skip to content

Commit

Permalink
TEST: getCapturedRequestsAndClear should be atomic (#31312)
Browse files Browse the repository at this point in the history
We might lose messages between getCapturedRequestsAndClear calls.
This commit makes sure that both getCapturedRequestsAndClear and
getCapturedRequestsByTargetNodeAndClear are atomic.
  • Loading branch information
dnhatn committed Jun 15, 2018
1 parent 6f94914 commit bb568ab
Showing 1 changed file with 16 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import java.io.UncheckedIOException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -94,26 +95,25 @@ public CapturedRequest[] capturedRequests() {
* @return the captured requests
*/
public CapturedRequest[] getCapturedRequestsAndClear() {
CapturedRequest[] capturedRequests = capturedRequests();
clear();
return capturedRequests;
List<CapturedRequest> requests = new ArrayList<>(capturedRequests.size());
capturedRequests.drainTo(requests);
return requests.toArray(new CapturedRequest[0]);
}

private Map<String, List<CapturedRequest>> groupRequestsByTargetNode(Collection<CapturedRequest> requests) {
Map<String, List<CapturedRequest>> result = new HashMap<>();
for (CapturedRequest request : requests) {
result.computeIfAbsent(request.node.getId(), node -> new ArrayList<>()).add(request);
}
return result;
}

/**
* returns all requests captured so far, grouped by target node.
* Doesn't clear the captured request list. See {@link #clear()}
*/
public Map<String, List<CapturedRequest>> capturedRequestsByTargetNode() {
Map<String, List<CapturedRequest>> map = new HashMap<>();
for (CapturedRequest request : capturedRequests) {
List<CapturedRequest> nodeList = map.get(request.node.getId());
if (nodeList == null) {
nodeList = new ArrayList<>();
map.put(request.node.getId(), nodeList);
}
nodeList.add(request);
}
return map;
return groupRequestsByTargetNode(capturedRequests);
}

/**
Expand All @@ -125,9 +125,9 @@ public Map<String, List<CapturedRequest>> capturedRequestsByTargetNode() {
* @return the captured requests grouped by target node
*/
public Map<String, List<CapturedRequest>> getCapturedRequestsByTargetNodeAndClear() {
Map<String, List<CapturedRequest>> map = capturedRequestsByTargetNode();
clear();
return map;
List<CapturedRequest> requests = new ArrayList<>(capturedRequests.size());
capturedRequests.drainTo(requests);
return groupRequestsByTargetNode(requests);
}

/** clears captured requests */
Expand Down

0 comments on commit bb568ab

Please sign in to comment.