Skip to content

Commit 964b035

Browse files
pedro-wsimonsteiner1984
authored andcommitted
Fix tests on windows by Peter Hull
Force test resource files to be binary On Windows, git would treat the unencoded files as text and apply CRLF line endings, so their base 64 encodings would not match the reference files. Work around timing issues on Windows On Windows the elapsed time was quite unreliable, causing intermittent fails in the performance tests, presumably made worse because PCs / the JRE are faster now then when this code was written. Also fixed up a few warnings/javadoc issues and replaced a home-grown bubble sort with Arrays.sort.
1 parent 7bd4291 commit 964b035

File tree

4 files changed

+37
-29
lines changed

4 files changed

+37
-29
lines changed

.github/workflows/maven.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
fail-fast: false
1818
matrix:
1919
jdk: ['8', '11', '17']
20-
os: [ubuntu-latest]
20+
os: [ubuntu-latest, windows-latest]
2121

2222
steps:
2323
- uses: actions/checkout@v4

batik-test-old/src/test/java/org/apache/batik/test/xml/JUnitRunnerTestCase.java

+4
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ public class JUnitRunnerTestCase {
5151

5252
@BeforeClass
5353
public static void beforeClass() throws IOException {
54+
if (System.getProperty("os.name").startsWith("Windows")) {
55+
EXCLUDE = new ArrayList<>(EXCLUDE);
56+
EXCLUDE.add("PerformanceTestSanity");
57+
}
5458
String sm = "grant { permission java.security.AllPermission; };";
5559
File tmp = File.createTempFile("batik", "sm");
5660
FileOutputStream fos = new FileOutputStream(tmp);

batik-test/src/main/java/org/apache/batik/test/PerformanceTest.java

+31-28
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
1818
*/
1919
package org.apache.batik.test;
2020

21+
import java.util.Arrays;
2122
import java.util.Vector;
2223

2324
/**
@@ -74,16 +75,20 @@ public void setAllowedScoreDeviation(double allowedScoreDeviation) {
7475
/**
7576
* Force implementations to only implement <code>runOp</code>
7677
* and other performance specific methods.
78+
* @return the results of AbstractTest's run method
7779
*/
80+
@Override
7881
public final TestReport run() {
7982
return super.run();
8083
}
8184

8285
/**
8386
* Force implementations to only implement <code>runOp</code>
8487
* and other performance specific methods.
88+
* @return false always, indicating failure
8589
*/
86-
public final boolean runImplBasic() throws Exception {
90+
@Override
91+
public final boolean runImplBasic() {
8792
// Should never be called for a PerformanceTest
8893
return false;
8994
}
@@ -95,27 +100,31 @@ public final boolean runImplBasic() throws Exception {
95100
* or not the score is within the allowed deviation of the
96101
* reference score.
97102
*
103+
* @return the test report
104+
* @throws java.lang.Exception if an error occurred
98105
* @see #runRef
99106
* @see #runOp
100107
*/
108+
@Override
101109
public final TestReport runImpl() throws Exception {
102110
int iter = 50;
103111

104-
double refUnit = 0;
105-
long refStart = 0;
106-
long refEnd = 0;
107-
long opEnd = 0;
108-
long opStart = 0;
109-
double opLength = 0;
112+
double refUnit;
113+
long refStart;
114+
long refEnd;
115+
long opEnd;
116+
long opStart;
117+
double opLength;
110118

111119
// Run once to remove class load time from timing.
112120
runRef();
113121
runOp();
114122
// System.gc();
115123

116124
double[] scores = new double[iter];
117-
118-
for (int i=0; i<iter; i++) {
125+
// Count bad scores so it doesn't get stuck forever
126+
int badScores = 0;
127+
for (int i=0; i<iter; ) {
119128
if ( i%2 == 0) {
120129
refStart = System.currentTimeMillis();
121130
runRef();
@@ -133,17 +142,26 @@ public final TestReport runImpl() throws Exception {
133142
refUnit = refEnd - opEnd;
134143
opLength = opEnd - opStart;
135144
}
145+
// Only accept finite and non-zero scores
146+
if (opLength > 0.0 && refUnit > 0.0) {
147+
scores[i++] = opLength / refUnit;
148+
System.err.print(".");
149+
} else {
150+
System.err.print("!");
151+
if (++badScores > iter) {
152+
// Too many bad scores
153+
throw new IllegalStateException("Unable to obtain reliable timings");
136154

137-
scores[i] = opLength / refUnit;
138-
System.err.println(".");
155+
}
156+
}
139157
// System.err.println(">>>>>>>> scores[" + i + "] = " + scores[i] + " (" + refUnit + " / " + opLength + ")");
140158
System.gc();
141159
}
142160

143161
System.err.println();
144162

145163
// Now, sort the scores
146-
sort(scores);
164+
Arrays.sort(scores);
147165

148166
// Compute the mean score based on the scores, not accounting
149167
// for the lowest and highest scores
@@ -184,22 +202,6 @@ public final TestReport runImpl() throws Exception {
184202
}
185203
}
186204

187-
protected void sort(double[] a) throws Exception {
188-
for (int i = a.length - 1; i>=0; i--) {
189-
boolean swapped = false;
190-
for (int j = 0; j<i; j++) {
191-
if (a[j] > a[j+1]) {
192-
double d = a[j];
193-
a[j] = a[j+1];
194-
a[j+1] = d;
195-
swapped = true;
196-
}
197-
}
198-
if (!swapped)
199-
return;
200-
}
201-
}
202-
203205
/**
204206
* Runs the reference operation.
205207
* By default, this runs the same BufferedImage drawing
@@ -220,6 +222,7 @@ protected void runRef() {
220222

221223
/**
222224
* Runs the tested operation
225+
* @throws java.lang.Exception if an error occurred
223226
*/
224227
protected abstract void runOp() throws Exception;
225228
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* -text

0 commit comments

Comments
 (0)