@@ -123,20 +123,17 @@ public static File tempFile() throws IOException {
123
123
*
124
124
* @param prefix The prefix of the temporary directory, if null using "kafka-" as default prefix
125
125
*/
126
- public static File tempDirectory (String prefix ) throws IOException {
126
+ public static File tempDirectory (String prefix ) {
127
127
return tempDirectory (null , prefix );
128
128
}
129
129
130
130
/**
131
- * Create a temporary directory named "test" under /temp
131
+ * Create a temporary relative directory in the default temporary-file directory with a
132
+ * prefix of "kafka-"
132
133
* @return the temporary directory just created.
133
134
*/
134
- public static File tempDir () {
135
- try {
136
- return tempDirectory (new File ("/tmp" ).toPath (), "test" );
137
- } catch (IOException ex ) {
138
- throw new RuntimeException ("Failed to create a temp dir" , ex );
139
- }
135
+ public static File tempDirectory () {
136
+ return tempDirectory (null );
140
137
}
141
138
142
139
/**
@@ -145,10 +142,15 @@ public static File tempDir() {
145
142
* @param parent The parent folder path name, if null using the default temporary-file directory
146
143
* @param prefix The prefix of the temporary directory, if null using "kafka-" as default prefix
147
144
*/
148
- public static File tempDirectory (Path parent , String prefix ) throws IOException {
149
- final File file = parent == null ?
150
- Files .createTempDirectory (prefix == null ? "kafka-" : prefix ).toFile () :
151
- Files .createTempDirectory (parent , prefix == null ? "kafka-" : prefix ).toFile ();
145
+ public static File tempDirectory (Path parent , String prefix ) {
146
+ final File file ;
147
+ prefix = prefix == null ? "kafka-" : prefix ;
148
+ try {
149
+ file = parent == null ?
150
+ Files .createTempDirectory (prefix ).toFile () : Files .createTempDirectory (parent , prefix ).toFile ();
151
+ } catch (IOException ex ) {
152
+ throw new RuntimeException ("Failed to create a temp dir" , ex );
153
+ }
152
154
file .deleteOnExit ();
153
155
154
156
Runtime .getRuntime ().addShutdownHook (new Thread () {
@@ -225,21 +227,29 @@ public static Properties consumerConfig(final String bootstrapServers, Class key
225
227
}
226
228
227
229
/**
228
- * uses default value of 30 seconds for timeout
230
+ * uses default value of 15 seconds for timeout
229
231
*/
230
- public static void waitForCondition (TestCondition testCondition ) throws InterruptedException {
231
- waitForCondition (testCondition , 30000 );
232
+ public static void waitForCondition (TestCondition testCondition , String conditionDetails ) throws InterruptedException {
233
+ waitForCondition (testCondition , 15000 , conditionDetails );
232
234
}
233
235
234
236
/**
235
- * Used to wait for specific conditions/state to be me during a test
236
- * this is meant to be a replacement for using Thread.sleep
237
+ * Wait for condition to be met for at most {@code maxWaitMs} and throw assertion failure otherwise.
238
+ * This should be used instead of {@code Thread.sleep} whenever possible as it allows a longer timeout to be used
239
+ * without unnecessarily increasing test time (as the condition is checked frequently). The longer timeout is needed to
240
+ * avoid transient failures due to slow or overloaded machines.
237
241
*/
238
- public static void waitForCondition (TestCondition testCondition , long maxTimeMillis ) throws InterruptedException {
242
+ public static void waitForCondition (TestCondition testCondition , long maxWaitMs , String conditionDetails ) throws InterruptedException {
239
243
long startTime = System .currentTimeMillis ();
240
244
241
- while (!testCondition .conditionMet () && ((System .currentTimeMillis () - startTime ) < maxTimeMillis )) {
242
- Thread .sleep (Math .min (maxTimeMillis , 100L ));
245
+
246
+ while (!testCondition .conditionMet () && ((System .currentTimeMillis () - startTime ) < maxWaitMs )) {
247
+ Thread .sleep (Math .min (maxWaitMs , 100L ));
248
+ }
249
+
250
+ if (!testCondition .conditionMet ()) {
251
+ conditionDetails = conditionDetails != null ? conditionDetails : "" ;
252
+ throw new AssertionError ("Condition not met within timeout " + maxWaitMs + ". " + conditionDetails );
243
253
}
244
254
}
245
255
0 commit comments