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

TEST: getCapturedRequestsAndClear should be atomic #31312

Merged
merged 3 commits into from
Jun 15, 2018
Merged
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 @@ -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