diff --git a/test/api/test_tools.py b/test/api/test_tools.py
index 53dcbd62a6e..750e538e75f 100644
--- a/test/api/test_tools.py
+++ b/test/api/test_tools.py
@@ -802,6 +802,25 @@ class ToolsTestCase(api.ApiTestCase):
output1_content = self.dataset_populator.get_history_dataset_content(history_id, dataset=output1)
self.assertEqual(output1_content.strip(), "Cat1Test\nCat2Test")
+ @skip_without_tool("mapper_two")
+ @uses_test_history(require_new=False)
+ def test_bam_state_regression(self, history_id):
+ # Test regression of https://github.com/galaxyproject/galaxy/issues/6856. With changes
+ # to metadata file flushing to optimize creating bam outputs and copying bam datasets
+ # we observed very subtle problems with HDA state changes on other files being flushed at
+ # the same time. This tests txt datasets finalized before and after the bam outputs as
+ # well as other bam files all flush properly during job completion.
+ new_dataset1 = self.dataset_populator.new_dataset(history_id, content='123\n456\n789')
+ inputs = {
+ 'input1': dataset_to_param(new_dataset1),
+ 'reference': dataset_to_param(new_dataset1),
+ }
+ outputs = self._run_and_get_outputs('mapper_two', history_id, inputs)
+ assert len(outputs) == 4
+ for output in outputs:
+ details = self.dataset_populator.get_history_dataset_details(history_id, dataset=output)
+ assert details["state"] == "ok"
+
@skip_without_tool("cat1")
@uses_test_history(require_new=False)
def test_multirun_cat1(self, history_id):
diff --git a/test/functional/tools/for_workflows/mapper_two.xml b/test/functional/tools/for_workflows/mapper_two.xml
new file mode 100644
index 00000000000..e26d9685d3a
--- /dev/null
+++ b/test/functional/tools/for_workflows/mapper_two.xml
@@ -0,0 +1,22 @@
+
+
+ echo 'text 0' > '$text0';
+ cp '$__tool_directory__/1.bam' '$out_file1';
+ cp '$__tool_directory__/1.bam' '$out_file2';
+ echo 'text 1' > '$text1'
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/test/functional/tools/samples_tool_conf.xml b/test/functional/tools/samples_tool_conf.xml
index 51673e7a7c4..9cd4062f1f8 100644
--- a/test/functional/tools/samples_tool_conf.xml
+++ b/test/functional/tools/samples_tool_conf.xml
@@ -158,6 +158,7 @@
+