From 9b6d93fe73c4f5356abb2f4310f71c15437ab710 Mon Sep 17 00:00:00 2001
From: Michael Kozono <mkozono@gmail.com>
Date: Thu, 10 May 2018 17:13:04 -0700
Subject: [PATCH] Fix wait strategies for slow replication

---
 qa/qa/ee.rb                                     |  6 ++++++
 qa/qa/ee/page/dashboard/projects.rb             | 17 +++++++++++++++++
 qa/qa/ee/page/project/show.rb                   | 15 +++++++++++++++
 qa/qa/page/dashboard/projects.rb                |  2 ++
 qa/qa/page/project/show.rb                      |  2 ++
 .../ee/geo/attachment_replication_spec.rb       |  8 ++++++++
 .../features/ee/geo/rename_replication_spec.rb  | 10 +++++-----
 .../ee/geo/repository_replication_spec.rb       | 10 +++++-----
 8 files changed, 60 insertions(+), 10 deletions(-)
 create mode 100644 qa/qa/ee/page/dashboard/projects.rb
 create mode 100644 qa/qa/ee/page/project/show.rb

diff --git a/qa/qa/ee.rb b/qa/qa/ee.rb
index 322dc6a81a9..e0db850292c 100644
--- a/qa/qa/ee.rb
+++ b/qa/qa/ee.rb
@@ -4,6 +4,10 @@ module QA
   #
   module EE
     module Page
+      module Dashboard
+        autoload :Projects, 'qa/ee/page/dashboard/projects'
+      end
+
       module Main
         autoload :Banner, 'qa/ee/page/main/banner'
       end
@@ -24,6 +28,8 @@ module QA
       end
 
       module Project
+        autoload :Show, 'qa/ee/page/project/show'
+
         module Settings
           autoload :ProtectedBranches, 'qa/ee/page/project/settings/protected_branches'
         end
diff --git a/qa/qa/ee/page/dashboard/projects.rb b/qa/qa/ee/page/dashboard/projects.rb
new file mode 100644
index 00000000000..695f5fd130c
--- /dev/null
+++ b/qa/qa/ee/page/dashboard/projects.rb
@@ -0,0 +1,17 @@
+module QA
+  module EE
+    module Page
+      module Dashboard
+        module Projects
+          def wait_for_project_replication(project_name)
+            wait(max: 180) do
+              filter_by_name(project_name)
+
+              page.has_content?(project_name)
+            end
+          end
+        end
+      end
+    end
+  end
+end
diff --git a/qa/qa/ee/page/project/show.rb b/qa/qa/ee/page/project/show.rb
new file mode 100644
index 00000000000..e09ab0d4e68
--- /dev/null
+++ b/qa/qa/ee/page/project/show.rb
@@ -0,0 +1,15 @@
+module QA
+  module EE
+    module Page
+      module Project
+        module Show
+          def wait_for_repository_replication
+            wait(max: 180) do
+              !page.has_content?('The repository for this project is empty')
+            end
+          end
+        end
+      end
+    end
+  end
+end
diff --git a/qa/qa/page/dashboard/projects.rb b/qa/qa/page/dashboard/projects.rb
index 73942cb856a..d06bbb1c803 100644
--- a/qa/qa/page/dashboard/projects.rb
+++ b/qa/qa/page/dashboard/projects.rb
@@ -2,6 +2,8 @@ module QA
   module Page
     module Dashboard
       class Projects < Page::Base
+        prepend QA::EE::Page::Dashboard::Projects
+
         view 'app/views/dashboard/projects/index.html.haml'
         view 'app/views/shared/projects/_search_form.html.haml' do
           element :form_filter_by_name, /form_tag.+id: 'project-filter-form'/
diff --git a/qa/qa/page/project/show.rb b/qa/qa/page/project/show.rb
index 1406edece17..9283ecc721a 100644
--- a/qa/qa/page/project/show.rb
+++ b/qa/qa/page/project/show.rb
@@ -2,6 +2,8 @@ module QA
   module Page
     module Project
       class Show < Page::Base
+        prepend QA::EE::Page::Project::Show
+
         include Page::Shared::ClonePanel
 
         view 'app/views/projects/_last_push.html.haml' do
diff --git a/qa/qa/specs/features/ee/geo/attachment_replication_spec.rb b/qa/qa/specs/features/ee/geo/attachment_replication_spec.rb
index 0b782b71f79..dd13390a584 100644
--- a/qa/qa/specs/features/ee/geo/attachment_replication_spec.rb
+++ b/qa/qa/specs/features/ee/geo/attachment_replication_spec.rb
@@ -36,12 +36,20 @@ module QA
           end
 
           Page::Dashboard::Projects.perform do |dashboard|
+            dashboard.wait_for_project_replication(project.name)
+
             dashboard.go_to_project(project.name)
           end
 
           Page::Menu::Side.act { click_issues }
 
           Page::Project::Issue::Index.perform do |index|
+            # wait for DB replication of issue
+            index.wait do
+              # we may want to filter by title here if the project is reused by other specs
+              page.has_content?(issue.title)
+            end
+
             index.go_to_issue(issue.title)
           end
 
diff --git a/qa/qa/specs/features/ee/geo/rename_replication_spec.rb b/qa/qa/specs/features/ee/geo/rename_replication_spec.rb
index 61fd8922bf8..9b042f03e56 100644
--- a/qa/qa/specs/features/ee/geo/rename_replication_spec.rb
+++ b/qa/qa/specs/features/ee/geo/rename_replication_spec.rb
@@ -36,8 +36,6 @@ module QA
           end
         end
 
-        sleep 2 # wait for replication
-
         # check renamed project exist on secondary node
         Runtime::Browser.visit(:geo_secondary, QA::Page::Main::Login) do
           Page::Main::OAuth.act do
@@ -50,15 +48,17 @@ module QA
 
           Page::Menu::Main.perform do |menu|
             menu.go_to_projects
-
-            expect(page).to have_content(geo_project_renamed)
           end
 
           Page::Dashboard::Projects.perform do |dashboard|
+            dashboard.wait_for_project_replication(geo_project_renamed)
+
             dashboard.go_to_project(geo_project_renamed)
           end
 
-          Page::Project::Show.perform do
+          Page::Project::Show.perform do |show|
+            show.wait_for_repository_replication
+
             expect(page).to have_content 'README.md'
             expect(page).to have_content 'This is Geo project!'
           end
diff --git a/qa/qa/specs/features/ee/geo/repository_replication_spec.rb b/qa/qa/specs/features/ee/geo/repository_replication_spec.rb
index 7ebdec4ee02..913a9f4fd08 100644
--- a/qa/qa/specs/features/ee/geo/repository_replication_spec.rb
+++ b/qa/qa/specs/features/ee/geo/repository_replication_spec.rb
@@ -30,17 +30,17 @@ module QA
 
           Page::Menu::Main.perform do |menu|
             menu.go_to_projects
-
-            expect(page).to have_content(geo_project_name)
           end
 
-          sleep 10 # wait for repository replication
-
           Page::Dashboard::Projects.perform do |dashboard|
+            dashboard.wait_for_project_replication(geo_project_name)
+
             dashboard.go_to_project(geo_project_name)
           end
 
-          Page::Project::Show.perform do
+          Page::Project::Show.perform do |show|
+            show.wait_for_repository_replication
+
             expect(page).to have_content 'README.md'
             expect(page).to have_content 'This is Geo project!'
           end
-- 
2.30.9