29
29
import com .apple .foundationdb .test .FDBTestEnvironment ;
30
30
import com .apple .foundationdb .test .TestExecutors ;
31
31
import org .junit .jupiter .api .extension .AfterEachCallback ;
32
+ import org .junit .jupiter .api .extension .BeforeEachCallback ;
32
33
import org .junit .jupiter .api .extension .ExtensionContext ;
33
34
import org .junit .jupiter .api .extension .RegisterExtension ;
34
35
import org .slf4j .Logger ;
59
60
* tests that use this extension are free to modify what would otherwise be global state on the factory or database.
60
61
* </p>
61
62
*/
62
- public class FDBDatabaseExtension implements AfterEachCallback {
63
+ public class FDBDatabaseExtension implements AfterEachCallback , BeforeEachCallback {
63
64
private static final Logger LOGGER = LoggerFactory .getLogger (FDBDatabaseExtension .class );
64
65
public static final String BLOCKING_IN_ASYNC_PROPERTY = "com.apple.foundationdb.record.blockingInAsyncDetection" ;
65
66
public static final String API_VERSION_PROPERTY = "com.apple.foundationdb.apiVersion" ;
@@ -70,6 +71,7 @@ public class FDBDatabaseExtension implements AfterEachCallback {
70
71
private FDBDatabaseFactory databaseFactory ;
71
72
@ Nonnull
72
73
private final Map <String , FDBDatabase > databases = new HashMap <>();
74
+ private String defaultClusterFile ;
73
75
74
76
75
77
public FDBDatabaseExtension () {
@@ -103,7 +105,7 @@ private static FDB getInitedFDB() {
103
105
for (final String clusterFile : FDBTestEnvironment .allClusterFiles ()) {
104
106
FDBDatabase unused = baseFactory .getDatabase (clusterFile );
105
107
unused .performNoOp (); // make sure FDB gets opened
106
- unused .close ();
108
+ unused .close (); // FDBDatabase does not implement AutoCloseable
107
109
}
108
110
fdb = FDB .instance ();
109
111
}
@@ -145,7 +147,7 @@ public FDBDatabaseFactory getDatabaseFactory() {
145
147
146
148
@ Nonnull
147
149
public FDBDatabase getDatabase () {
148
- return getDatabase (FDBTestEnvironment . randomClusterFile () );
150
+ return getDatabase (defaultClusterFile );
149
151
}
150
152
151
153
public FDBDatabase getDatabase (int clusterIndex ) {
@@ -167,6 +169,11 @@ public void checkForOpenContexts() {
167
169
}
168
170
}
169
171
172
+ @ Override
173
+ public void beforeEach (final ExtensionContext context ) {
174
+ defaultClusterFile = FDBTestEnvironment .randomClusterFile ();
175
+ }
176
+
170
177
@ Override
171
178
public void afterEach (final ExtensionContext extensionContext ) {
172
179
// Validate that the test closes all the transactions that it opens
0 commit comments