Skip to content

Commit 16ab5d8

Browse files
committed
Sort threads in timeline lexicographically
Ideally they'd be sorted by id, but we don't have access to that anymore. So we have to sort by their name. Which can only be done lexicographically.
1 parent 573e96d commit 16ab5d8

File tree

2 files changed

+27
-27
lines changed

2 files changed

+27
-27
lines changed

cucumber-core/src/main/java/io/cucumber/core/plugin/TimelineFormatter.java

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,12 @@
3333
import java.util.List;
3434
import java.util.Map;
3535
import java.util.Optional;
36-
import java.util.TreeMap;
37-
import java.util.concurrent.atomic.AtomicInteger;
3836
import java.util.function.Function;
39-
import java.util.function.Supplier;
4037
import java.util.stream.Collectors;
4138

4239
import static com.fasterxml.jackson.annotation.JsonInclude.Value.construct;
4340
import static io.cucumber.query.Repository.RepositoryFeature.INCLUDE_GHERKIN_DOCUMENTS;
41+
import static java.util.Comparator.comparing;
4442
import static java.util.Locale.ROOT;
4543

4644
/**
@@ -106,35 +104,37 @@ private void write(Envelope event) {
106104
}
107105

108106
private void writeTimeLineReport() throws IOException {
109-
Map<String, TimeLineGroup> timeLineGroups = new HashMap<>();
110-
AtomicInteger nextGroupId = new AtomicInteger();
107+
Map<String, TimeLineGroup> timeLineGroupsById = new HashMap<>();
111108
List<TimeLineItem> timeLineItems = query.findAllTestCaseFinished().stream()
112109
.map(testCaseFinished -> query.findTestCaseStartedBy(testCaseFinished)
113110
.map(testCaseStarted -> createTestData(
114111
testCaseFinished, //
115112
testCaseStarted, //
116-
createTimeLineGroup(timeLineGroups, nextGroupId) //
113+
createTimeLineGroup(timeLineGroupsById) //
117114
)))
118115
.filter(Optional::isPresent)
119116
.map(Optional::get)
120117
.collect(Collectors.toList());
121118

119+
List<TimeLineGroup> timeLineGroups = timeLineGroupsById.values().stream()
120+
.sorted(comparing(TimeLineGroup::getId))
121+
.collect(Collectors.toList());
122+
122123
writeTimeLineReport(timeLineGroups, timeLineItems);
123124
}
124125

125126
private Function<String, TimeLineGroup> createTimeLineGroup(
126-
Map<String, TimeLineGroup> timeLineGroups,
127-
AtomicInteger nextGroupId
127+
Map<String, TimeLineGroup> timeLineGroups
128128
) {
129129

130-
return workerId -> timeLineGroups.computeIfAbsent(workerId, createTimeLineGroup(nextGroupId::incrementAndGet));
130+
return workerId -> timeLineGroups.computeIfAbsent(workerId, createTimeLineGroup());
131131
}
132132

133-
private Function<String, TimeLineGroup> createTimeLineGroup(Supplier<Integer> nextGroupId) {
133+
private Function<String, TimeLineGroup> createTimeLineGroup() {
134134
return workerId -> {
135135
TimeLineGroup timeLineGroup = new TimeLineGroup();
136136
timeLineGroup.setContent(workerId);
137-
timeLineGroup.setId(nextGroupId.get());
137+
timeLineGroup.setId(workerId);
138138
return timeLineGroup;
139139
};
140140
}
@@ -196,13 +196,13 @@ private String getTagsValue(TestCaseStarted testCaseStarted) {
196196
}).orElse("");
197197
}
198198

199-
private void writeTimeLineReport(Map<String, TimeLineGroup> timeLineGroups, List<TimeLineItem> timeLineItems)
199+
private void writeTimeLineReport(List<TimeLineGroup> timeLineGroups, List<TimeLineItem> timeLineItems)
200200
throws IOException {
201201
writeReportJs(timeLineGroups, timeLineItems);
202202
copyReportFiles();
203203
}
204204

205-
private void writeReportJs(Map<String, TimeLineGroup> timeLineGroups, List<TimeLineItem> timeLineItems)
205+
private void writeReportJs(List<TimeLineGroup> timeLineGroups, List<TimeLineItem> timeLineItems)
206206
throws IOException {
207207
File reportJsFile = new File(reportDir, "report.js");
208208
try (BufferedWriter reportJs = Files.newBufferedWriter(reportJsFile.toPath(), StandardCharsets.UTF_8)) {
@@ -212,7 +212,7 @@ private void writeReportJs(Map<String, TimeLineGroup> timeLineGroups, List<TimeL
212212
reportJs.append("\n");
213213
// Need to sort groups by id, so can guarantee output of order in
214214
// rendered timeline
215-
appendAsJsonToJs(reportJs, "timelineGroups", new TreeMap<>(timeLineGroups).values());
215+
appendAsJsonToJs(reportJs, "timelineGroups", timeLineGroups);
216216
reportJs.append("\n");
217217
reportJs.append("});");
218218
}
@@ -254,18 +254,18 @@ private static void copyFile(InputStream source, File dest) throws IOException {
254254

255255
static class TimeLineGroup {
256256

257-
private long id;
257+
private String id;
258258
private String content;
259259

260-
public void setId(long id) {
260+
public void setId(String id) {
261261
this.id = id;
262262
}
263263

264264
public void setContent(String content) {
265265
this.content = content;
266266
}
267267

268-
public long getId() {
268+
public String getId() {
269269
return id;
270270
}
271271

@@ -281,7 +281,7 @@ static class TimeLineItem {
281281
private String feature;
282282
private String scenario;
283283
private long start;
284-
private long group;
284+
private String group;
285285
private String content = ""; // Replaced in JS file
286286
private String tags;
287287
private long end;
@@ -303,7 +303,7 @@ public void setStart(long start) {
303303
this.start = start;
304304
}
305305

306-
public void setGroup(long group) {
306+
public void setGroup(String group) {
307307
this.group = group;
308308
}
309309

@@ -339,7 +339,7 @@ public long getStart() {
339339
return start;
340340
}
341341

342-
public long getGroup() {
342+
public String getGroup() {
343343
return group;
344344
}
345345

cucumber-core/src/test/java/io/cucumber/core/plugin/TimelineFormatterTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,8 @@ void shouldWriteItemsCorrectlyToReportJsWhenRunInParallel() throws Throwable {
180180
final int idx = i;
181181
assertAll(
182182
() -> assertThat(String.format("id on group %s, was not as expected", idx),
183-
actual.getId() > 0,
184-
is(equalTo(true))),
183+
actual.getId(),
184+
is(notNullValue())),
185185
() -> assertThat(String.format("content on group %s, was not as expected",
186186
idx), actual.getContent(),
187187
is(notNullValue())));
@@ -201,7 +201,7 @@ private TimeLineItem[] getExpectedTestData() throws JsonProcessingException {
201201
" \"scenario\": \"Scenario 1\",\n" +
202202
" \"start\": 0,\n" +
203203
" \"end\": 6000,\n" +
204-
" \"group\": 1,\n" +
204+
" \"group\": \"main\",\n" +
205205
" \"content\": \"\",\n" +
206206
" \"tags\": \"@taga,\",\n" +
207207
" \"className\": \"failed\"\n" +
@@ -211,7 +211,7 @@ private TimeLineItem[] getExpectedTestData() throws JsonProcessingException {
211211
" \"scenario\": \"Scenario 2\",\n" +
212212
" \"start\": 6000,\n" +
213213
" \"end\": 12000,\n" +
214-
" \"group\": 1,\n" +
214+
" \"group\": \"main\",\n" +
215215
" \"content\": \"\",\n" +
216216
" \"tags\": \"\",\n" +
217217
" \"className\": \"failed\"\n" +
@@ -221,7 +221,7 @@ private TimeLineItem[] getExpectedTestData() throws JsonProcessingException {
221221
" \"scenario\": \"Scenario 3\",\n" +
222222
" \"start\": 18000,\n" +
223223
" \"end\": 24000,\n" +
224-
" \"group\": 1,\n" +
224+
" \"group\": \"main\",\n" +
225225
" \"content\": \"\",\n" +
226226
" \"tags\": \"@tagb,@tagc,\",\n" +
227227
" \"className\": \"passed\"\n" +
@@ -231,7 +231,7 @@ private TimeLineItem[] getExpectedTestData() throws JsonProcessingException {
231231
" \"feature\": \"Pending Feature\",\n" +
232232
" \"start\": 12000,\n" +
233233
" \"end\": 18000,\n" +
234-
" \"group\": 1,\n" +
234+
" \"group\": \"main\",\n" +
235235
" \"content\": \"\",\n" +
236236
" \"tags\": \"\",\n" +
237237
" \"className\": \"undefined\"\n" +
@@ -325,7 +325,7 @@ void shouldWriteItemsAndGroupsCorrectlyToReportJs() throws Throwable {
325325
TimeLineGroup[] expectedGroups = objectMapper.readValue(
326326
("[\n" +
327327
" {\n" +
328-
" \"id\": 1,\n" +
328+
" \"id\": \"main\",\n" +
329329
" \"content\": \"groupName\"\n" +
330330
" }\n" +
331331
"]")

0 commit comments

Comments
 (0)