/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.facebook.presto.tests;

import com.facebook.presto.tests.tpch.TpchQueryRunnerBuilder;
import org.testng.annotations.Test;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.testng.Assert.assertTrue;

public class TestTransactionManagerIntegration
{
    @Test
    public void testTransactionMetadataCleanup()
            throws Exception
    {
        try (DistributedQueryRunner queryRunner = TpchQueryRunnerBuilder.builder().build()) {
            // test valid queries
            queryRunner.execute("select count(*) from nation");
            queryRunner.execute("select count(*) from lineitem");
            assertTrue(queryRunner.getTransactionManager().getAllTransactionInfos().isEmpty());
            // test invalid queries with different types of semantic error
            assertThatThrownBy(() -> queryRunner.execute("SELECT non_existing_column FROM non_existing_table"))
                    .isInstanceOf(RuntimeException.class);
            assertThatThrownBy(() -> queryRunner.execute("select * from unnest('wrong_type')"))
                    .isInstanceOf(RuntimeException.class);
            assertThatThrownBy(() -> queryRunner.execute("select regionkey from nation group by regionkey having 'wrong_condition'"))
                    .isInstanceOf(RuntimeException.class);
            assertTrue(queryRunner.getTransactionManager().getAllTransactionInfos().isEmpty());
        }
    }
}

@@ -591,16 +591,56 @@
 tests;%0A%0A
+import com.facebook.airlift.log.Logger;%0A
 import c
@@ -704,37 +704,281 @@
 ort 
-org.testng.annotations.Test;%0A
+com.facebook.presto.transaction.TransactionInfo;%0Aimport com.facebook.presto.transaction.TransactionManager;%0Aimport org.testng.annotations.Test;%0A%0Aimport static com.google.common.collect.ImmutableList.toImmutableList;%0Aimport static java.util.concurrent.TimeUnit.MILLISECONDS;
 %0Aimp
@@ -1134,16 +1134,108 @@
 ation%0A%7B%0A
+    private static final Logger log = Logger.get(TestTransactionManagerIntegration.class);%0A%0A
     @Tes
@@ -1414,16 +1414,175 @@
 ld()) %7B%0A
+            log.info(%22Start running testTransactionMetadataCleanup%22);%0A            TransactionManager transactionManager = queryRunner.getTransactionManager();%0A
         
@@ -1760,32 +1760,124 @@
 ertTrue(
-queryRunner.getT
+waitForTransactionMetadataCleanup(transactionManager),%0A                    %22Remaining transaction ids: %22 + t
 ransacti
@@ -1877,34 +1877,32 @@
 ansactionManager
-()
 .getAllTransacti
@@ -1907,32 +1907,155 @@
 tionInfos().
-isEmpty(
+stream()%0A                            .map(TransactionInfo::getTransactionId)%0A                            .collect(toImmutableList()
 ));%0A        
@@ -2632,32 +2632,33 @@
 ception.class);%0A
+%0A
             asse
@@ -2668,90 +2668,844 @@
 rue(
-queryRunner.getTransactionManager().getAllTransactionInfos().isEmpty());%0A        %7D
+waitForTransactionMetadataCleanup(transactionManager),%0A                    %22Remaining transaction ids: %22 + transactionManager.getAllTransactionInfos().stream()%0A                            .map(TransactionInfo::getTransactionId)%0A                            .collect(toImmutableList()));%0A            log.info(%22Complete test testTransactionMetadataCleanup%22);%0A        %7D%0A    %7D%0A%0A    private boolean waitForTransactionMetadataCleanup(TransactionManager transactionManager)%0A            throws InterruptedException%0A    %7B%0A        long timeout = 5000;%0A        long retryInterval = 10;%0A        while (timeout %3E 0) %7B%0A            if (transactionManager.getAllTransactionInfos().isEmpty()) %7B%0A                return true;%0A            %7D%0A            MILLISECONDS.sleep(retryInterval);%0A            timeout -= retryInterval;%0A        %7D%0A        return false;
 %0A   

Fix flaky testTransactionMetadataCleanup