40
40
41
41
import static java .lang .String .format ;
42
42
43
- public class TestUtils {
43
+ /**
44
+ * Helper functions for writing unit tests.
45
+ * <p>
46
+ * <b>Package-private:</b> Not intended for use outside {@code org.apache.kafka.common.test}.
47
+ */
48
+ class TestUtils {
44
49
private static final Logger log = LoggerFactory .getLogger (TestUtils .class );
45
50
51
+ /* A consistent random number generator to make tests repeatable */
46
52
public static final Random SEEDED_RANDOM = new Random (192348092834L );
47
53
48
54
public static final String LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" ;
@@ -53,20 +59,34 @@ public class TestUtils {
53
59
private static final long DEFAULT_MAX_WAIT_MS = 15_000 ;
54
60
private static final Random RANDOM = new Random ();
55
61
62
+ /**
63
+ * Create an empty file in the default temporary-file directory, using `kafka` as the prefix and `tmp` as the
64
+ * suffix to generate its name.
65
+ */
56
66
public static File tempFile () throws IOException {
57
67
final File file = Files .createTempFile ("kafka" , ".tmp" ).toFile ();
58
68
file .deleteOnExit ();
59
69
return file ;
60
70
}
61
71
72
+ /**
73
+ * Generate a random string of letters and digits of the given length
74
+ *
75
+ * @param len The length of the string
76
+ * @return The random string
77
+ */
62
78
public static String randomString (final int len ) {
63
79
final StringBuilder b = new StringBuilder ();
64
80
for (int i = 0 ; i < len ; i ++)
65
81
b .append (LETTERS_AND_DIGITS .charAt (SEEDED_RANDOM .nextInt (LETTERS_AND_DIGITS .length ())));
66
82
return b .toString ();
67
83
}
68
84
69
- public static File tempDirectory () {
85
+ /**
86
+ * Create a temporary relative directory in the specified parent directory with the given prefix.
87
+ *
88
+ */
89
+ static File tempDirectory () {
70
90
final File file ;
71
91
String prefix = "kafka-" ;
72
92
try {
@@ -86,17 +106,19 @@ public static File tempDirectory() {
86
106
return file ;
87
107
}
88
108
89
- public static File tempRelativeDir (String parent ) {
90
- File file = new File (parent , "kafka-" + SEEDED_RANDOM .nextInt (1000000 ));
91
- file .mkdirs ();
92
- file .deleteOnExit ();
93
- return file ;
94
- }
95
-
109
+ /**
110
+ * uses default value of 15 seconds for timeout
111
+ */
96
112
public static void waitForCondition (final Supplier <Boolean > testCondition , final String conditionDetails ) throws InterruptedException {
97
113
waitForCondition (testCondition , DEFAULT_MAX_WAIT_MS , () -> conditionDetails );
98
114
}
99
115
116
+ /**
117
+ * Wait for condition to be met for at most {@code maxWaitMs} and throw assertion failure otherwise.
118
+ * This should be used instead of {@code Thread.sleep} whenever possible as it allows a longer timeout to be used
119
+ * without unnecessarily increasing test time (as the condition is checked frequently). The longer timeout is needed to
120
+ * avoid transient failures due to slow or overloaded machines.
121
+ */
100
122
public static void waitForCondition (final Supplier <Boolean > testCondition ,
101
123
final long maxWaitMs ,
102
124
final Supplier <String > conditionDetails ) throws InterruptedException {
@@ -122,6 +144,12 @@ public static void waitForCondition(final Supplier<Boolean> testCondition,
122
144
}
123
145
}
124
146
147
+ /**
148
+ * Wait for condition to be met for at most {@code maxWaitMs} and throw assertion failure otherwise.
149
+ * This should be used instead of {@code Thread.sleep} whenever possible as it allows a longer timeout to be used
150
+ * without unnecessarily increasing test time (as the condition is checked frequently). The longer timeout is needed to
151
+ * avoid transient failures due to slow or overloaded machines.
152
+ */
125
153
public static void waitForCondition (final Supplier <Boolean > testCondition ,
126
154
final long maxWaitMs ,
127
155
String conditionDetails ) throws InterruptedException {
0 commit comments