3030import io .trino .execution .warnings .WarningCollector ;
3131import io .trino .execution .warnings .WarningCollectorConfig ;
3232import io .trino .metadata .Metadata ;
33+ import io .trino .metadata .TestMetadataManager ;
3334import io .trino .plugin .base .security .AllowAllSystemAccessControl ;
3435import io .trino .plugin .base .security .DefaultSystemAccessControl ;
3536import io .trino .security .AccessControlConfig ;
8990import static io .trino .execution .QueryState .STARTING ;
9091import static io .trino .execution .QueryState .WAITING_FOR_RESOURCES ;
9192import static io .trino .execution .querystats .PlanOptimizersStatsCollector .createPlanOptimizersStatsCollector ;
92- import static io .trino .metadata .TestMetadataManager .createTestMetadataManager ;
9393import static io .trino .spi .StandardErrorCode .GENERIC_INTERNAL_ERROR ;
9494import static io .trino .spi .StandardErrorCode .TYPE_MISMATCH ;
9595import static io .trino .spi .StandardErrorCode .USER_CANCELED ;
@@ -442,15 +442,7 @@ public void testPreserveFirstFailure()
442442 {
443443 CountDownLatch cleanup = new CountDownLatch (1 );
444444 QueryStateMachine queryStateMachine = queryStateMachine ()
445- .withMetadata (new TracingMetadata (noopTracer (), createTestMetadataManager ())
446- {
447- @ Override
448- public void cleanupQuery (Session session )
449- {
450- cleanup .countDown ();
451- super .cleanupQuery (session );
452- }
453- })
445+ .beforeQueryCleanup (cleanup ::countDown )
454446 .build ();
455447
456448 Future <?> anotherThread = executor .submit (() -> {
@@ -479,15 +471,7 @@ public void testPreserveCancellation()
479471 {
480472 CountDownLatch cleanup = new CountDownLatch (1 );
481473 QueryStateMachine queryStateMachine = queryStateMachine ()
482- .withMetadata (new TracingMetadata (noopTracer (), createTestMetadataManager ())
483- {
484- @ Override
485- public void cleanupQuery (Session session )
486- {
487- cleanup .countDown ();
488- super .cleanupQuery (session );
489- }
490- })
474+ .beforeQueryCleanup (cleanup ::countDown )
491475 .build ();
492476
493477 Future <?> anotherThread = executor .submit (() -> {
@@ -761,7 +745,7 @@ private QueryStateMachineBuilder queryStateMachine()
761745 private class QueryStateMachineBuilder
762746 {
763747 private Ticker ticker = Ticker .systemTicker ();
764- private Metadata metadata ;
748+ private Optional < Runnable > beforeQueryCleanup = Optional . empty () ;
765749 private WarningCollector warningCollector = WarningCollector .NOOP ;
766750 private String setCatalog ;
767751 private String setPath ;
@@ -780,9 +764,9 @@ public QueryStateMachineBuilder withTicker(Ticker ticker)
780764 }
781765
782766 @ CanIgnoreReturnValue
783- public QueryStateMachineBuilder withMetadata ( Metadata metadata )
767+ public QueryStateMachineBuilder beforeQueryCleanup ( Runnable runnable )
784768 {
785- this .metadata = metadata ;
769+ this .beforeQueryCleanup = Optional . of ( runnable ) ;
786770 return this ;
787771 }
788772
@@ -842,10 +826,23 @@ public QueryStateMachineBuilder withAddPreparedStatements(Map<String, String> pr
842826
843827 public QueryStateMachine build ()
844828 {
845- if (metadata == null ) {
846- metadata = createTestMetadataManager ();
847- }
848829 TransactionManager transactionManager = createTestTransactionManager ();
830+ Metadata metadata = TestMetadataManager .builder ()
831+ .withTransactionManager (transactionManager )
832+ .build ();
833+ if (beforeQueryCleanup .isPresent ()) {
834+ Runnable beforeQueryCleanupAction = beforeQueryCleanup .get ();
835+ metadata = new TracingMetadata (noopTracer (), metadata )
836+ {
837+ @ Override
838+ public void cleanupQuery (Session session )
839+ {
840+ beforeQueryCleanupAction .run ();
841+ super .cleanupQuery (session );
842+ }
843+ };
844+ }
845+
849846 AccessControlManager accessControl = new AccessControlManager (
850847 NodeVersion .UNKNOWN ,
851848 transactionManager ,
0 commit comments