/
TestCompactedMult.java
198 lines (161 loc) · 6.32 KB
/
TestCompactedMult.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ignite.tcbot.engine.chain;
import org.apache.ignite.tcbot.common.TcBotConst;
import org.apache.ignite.tcbot.persistence.IStringCompactor;
import org.apache.ignite.tcignited.ITeamcityIgnited;
import org.apache.ignite.tcignited.build.ITest;
import org.apache.ignite.tcignited.history.IRunHistSummary;
import org.apache.ignite.tcignited.history.IRunHistory;
import org.apache.ignite.tcignited.history.IRunStat;
import org.apache.ignite.tcignited.history.ISuiteRunHistory;
import org.apache.ignite.tcservice.model.result.tests.TestOccurrence;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;
/**
* Test occurrence merged from several runs.
*/
public class TestCompactedMult {
private final List<ITest> occurrences = new ArrayList<>();
private IStringCompactor compactor;
private MultBuildRunCtx ctx;
private long avgDuration = -1;
/** Status success. */
private static volatile int STATUS_SUCCESS = -1;
public TestCompactedMult(IStringCompactor compactor, MultBuildRunCtx ctx) {
this.compactor = compactor;
this.ctx = ctx;
//Each time compactor should give same result
if (STATUS_SUCCESS == -1)
STATUS_SUCCESS = compactor.getStringId(TestOccurrence.STATUS_SUCCESS);
}
@Nullable public Integer testName() {
return occurrences.isEmpty() ? null : occurrences.iterator().next().testName();
}
public String getName() {
return occurrences.isEmpty() ? "" : occurrences.iterator().next().testName(compactor);
}
public boolean isInvestigated() {
return occurrences.stream().anyMatch(ITest::isInvestigated);
}
/** */
private int getFailedButNotMutedCount() {
return (int)occurrences.stream()
.filter(Objects::nonNull)
.filter(t -> t.isFailedButNotMuted(STATUS_SUCCESS)).count();
}
public int failuresCount() {
return getFailedButNotMutedCount();
}
public long getAvgDurationMs() {
if (avgDuration < 0) {
avgDuration = (long)occurrences.stream()
.map(ITest::getDuration)
.filter(Objects::nonNull)
.mapToInt(i -> i)
.average()
.orElse(0);
}
return avgDuration;
}
/**
*
*/
public Stream<ITest> getInvocationsStream() {
return occurrences.stream();
}
/**
* @param baseBranchStat Base branch statistics.
* @return non null comment in case test failure is a blocker for merge into base branch.
*/
public String getPossibleBlockerComment(IRunHistSummary baseBranchStat) {
if (failuresCount() == 0) {
if (baseBranchStat == null) {
long durationMs = getAvgDurationMs();
if (durationMs > TcBotConst.MAX_NEW_TEST_DURATION_FOR_RUNALL_MS)
return "New test duration " +
TimeUnit.MILLISECONDS.toSeconds(durationMs) + "s" +
" is more that 1 minute";
}
return null;
}
if (baseBranchStat == null)
return "History for base branch is absent.";
boolean flaky = baseBranchStat.isFlaky();
float failRate = baseBranchStat.getFailRate();
boolean lowFailureRate = failRate * 100.0f < TcBotConst.NON_FLAKY_TEST_FAIL_RATE_BLOCKER_BORDER_PERCENTS;
if (lowFailureRate && !flaky) {
String runStatPrintable = IRunStat.getPercentPrintable(failRate * 100.0f);
return "Test has low fail rate in base branch "
+ runStatPrintable
+ "% and is not flaky";
}
return null;
}
public void add(ITest next) {
occurrences.add(next);
}
public IRunHistory history(ITeamcityIgnited ignited, @Nullable Integer baseBranchId) {
return history(ignited, baseBranchId, null);
}
public IRunHistory history(ITeamcityIgnited ignited,
@Nullable Integer baseBranchId,
@Nullable Map<Integer, Integer> requireParameters) {
Integer name = testName();
if (name == null || baseBranchId == null)
return null;
ISuiteRunHistory suiteRunHist = ctx.suiteHist(ignited, baseBranchId, requireParameters);
if (suiteRunHist == null)
return null;
return suiteRunHist.getTestRunHist(name);
}
/**
*/
public boolean isFailedButNotMuted() {
return occurrences.stream().anyMatch(o -> o.isFailedButNotMuted(STATUS_SUCCESS));
}
/**
*
*/
public boolean isMutedOrIgored() {
return occurrences.stream().anyMatch(ITest::isMutedOrIgnored);
}
/**
* Filter to determine if this test execution should be shown in the report of failures.
*
* @param tcIgnited Tc ignited.
* @param baseBranchId Base branch id.
*/
public boolean includeIntoReport(ITeamcityIgnited tcIgnited, Integer baseBranchId) {
if (isFailedButNotMuted())
return true;
boolean longRun = getAvgDurationMs() > TcBotConst.MAX_NEW_TEST_DURATION_FOR_RUNALL_MS;
if (longRun)
return history(tcIgnited, baseBranchId) == null;
return false;
}
public boolean hasLongRunningTest(int sec) {
if (sec < 1)
return false;
return getAvgDurationMs() > TimeUnit.SECONDS.toMillis(sec);
}
}