From 2e96f0121e315c834ad678e2e27b467da7e4b311 Mon Sep 17 00:00:00 2001
From: Raphael Sofaer <raphael@joindiaspora.com>
Date: Thu, 22 Sep 2011 00:24:01 -0400
Subject: [PATCH] simplify multi-server spec running with foreman.  couldn't
 really get it automated tonight, not sure why.

---
 config/application.yml.example    |  2 --
 multi-server-spec-Procfile        |  2 ++
 spec/multi_server/comment_spec.rb | 13 +++----------
 spec/multi_server/post_spec.rb    | 13 +++----------
 spec/multi_server/reshare_spec.rb | 13 +++----------
 spec/multi_server/server_spec.rb  | 11 +++--------
 spec/support/server.rb            | 15 +++++++++++++++
 7 files changed, 29 insertions(+), 40 deletions(-)
 create mode 100644 multi-server-spec-Procfile

diff --git a/config/application.yml.example b/config/application.yml.example
index e0bd14e2fb..4c55885763 100644
--- a/config/application.yml.example
+++ b/config/application.yml.example
@@ -180,7 +180,6 @@ production:
   <<: *defaults
   mount_resque_web: false
 
-
 #
 # Do not touch unless you know what you're doing
 #
@@ -192,7 +191,6 @@ test:
   enable_splunk_logging: false
   open_invitations: true
 
-
 integration_1:
   <<: *defaults
   pod_url: "http://localhost:45789"
diff --git a/multi-server-spec-Procfile b/multi-server-spec-Procfile
new file mode 100644
index 0000000000..b75b3c7a5d
--- /dev/null
+++ b/multi-server-spec-Procfile
@@ -0,0 +1,2 @@
+integration_1: bundle exec thin start -e integration_1 -p 45789
+integration_2: bundle exec thin start -e integration_2 -p 34658
diff --git a/spec/multi_server/comment_spec.rb b/spec/multi_server/comment_spec.rb
index 0b351b51f1..56ca77e240 100644
--- a/spec/multi_server/comment_spec.rb
+++ b/spec/multi_server/comment_spec.rb
@@ -5,21 +5,14 @@ require 'spec_helper'
 unless Server.all.empty?
   describe "commenting" do
     before(:all) do
-      WebMock::Config.instance.allow_localhost = true
-      enable_typhoeus
-      #Server.all.each{|s| s.kill if s.running?}
-      #Server.all.each{|s| s.run}
+      Server.start
     end
 
     after(:all) do
-      disable_typhoeus
-      #Server.all.each{|s| s.kill if s.running?}
-      #sleep(1)
-      #Server.all.each{|s| puts "Server at port #{s.port} still running." if s.running?}
-      WebMock::Config.instance.allow_localhost = false
+      Server.stop
     end
     before do
-      Server.all.each{|s| s.truncate_database; }
+      Server.truncate_databases
       @post = nil
       Server[0].in_scope do
         poster = Factory.create(:user_with_aspect, :username => "poster")
diff --git a/spec/multi_server/post_spec.rb b/spec/multi_server/post_spec.rb
index 2511126842..70116a4d0c 100644
--- a/spec/multi_server/post_spec.rb
+++ b/spec/multi_server/post_spec.rb
@@ -3,21 +3,14 @@ require 'spec_helper'
 unless Server.all.empty?
   describe "commenting" do
     before(:all) do
-      WebMock::Config.instance.allow_localhost = true
-      enable_typhoeus
-      #Server.all.each{|s| s.kill if s.running?}
-      #Server.all.each{|s| s.run}
+      Server.start
     end
 
     after(:all) do
-      disable_typhoeus
-      #Server.all.each{|s| s.kill if s.running?}
-      #sleep(1)
-      #Server.all.each{|s| puts "Server at port #{s.port} still running." if s.running?}
-      WebMock::Config.instance.allow_localhost = false
+      Server.stop
     end
     before do
-      Server.all.each{|s| s.truncate_database; }
+      Server.truncate_databases
       @post = nil
       Server[0].in_scope do
         Factory.create(:user_with_aspect, :username => "poster")
diff --git a/spec/multi_server/reshare_spec.rb b/spec/multi_server/reshare_spec.rb
index 81e873e02d..14f7f3f761 100644
--- a/spec/multi_server/reshare_spec.rb
+++ b/spec/multi_server/reshare_spec.rb
@@ -5,21 +5,14 @@ require 'spec_helper'
 unless Server.all.empty?
   describe "reposting" do
     before(:all) do
-      WebMock::Config.instance.allow_localhost = true
-      enable_typhoeus
-      #Server.all.each{|s| s.kill if s.running?}
-      #Server.all.each{|s| s.run}
+      Server.start
     end
 
     after(:all) do
-      disable_typhoeus
-      #Server.all.each{|s| s.kill if s.running?}
-      #sleep(1)
-      #Server.all.each{|s| puts "Server at port #{s.port} still running." if s.running?}
-      WebMock::Config.instance.allow_localhost = false
+      Server.stop
     end
     before do
-      Server.all.each{|s| s.truncate_database; }
+      Server.truncate_databases
       @original_post = nil
       Server[0].in_scope do
         original_poster = Factory.create(:user_with_aspect, :username => "original_poster")
diff --git a/spec/multi_server/server_spec.rb b/spec/multi_server/server_spec.rb
index 35f046af3c..ea8dc9cfa3 100644
--- a/spec/multi_server/server_spec.rb
+++ b/spec/multi_server/server_spec.rb
@@ -4,20 +4,15 @@ require 'spec_helper'
 unless Server.all.empty?
   describe Server do
     before(:all) do
-      WebMock::Config.instance.allow_localhost = true
-      #Server.all.each{|s| s.kill if s.running?}
-      #Server.all.each{|s| s.run}
+      Server.start
     end
 
     after(:all) do
-      #Server.all.each{|s| s.kill if s.running?}
-      #sleep(1)
-      #Server.all.each{|s| puts "Server at port #{s.port} still running." if s.running?}
-      WebMock::Config.instance.allow_localhost = false
+      Server.stop
     end
 
     before do
-      Server.all.each{|s| s.truncate_database }
+      Server.truncate_databases
     end
     describe '.all' do
       it 'returns a server object for each server' do
diff --git a/spec/support/server.rb b/spec/support/server.rb
index 0432247526..d9cbaecb16 100644
--- a/spec/support/server.rb
+++ b/spec/support/server.rb
@@ -1,5 +1,18 @@
 #This class is for running servers in the background during integration testing.  This will not run on Windows.
 class Server
+  def self.start
+    WebMock::Config.instance.allow_localhost = true
+    enable_typhoeus
+  end
+
+  def self.stop
+    disable_typhoeus
+    WebMock::Config.instance.allow_localhost = false
+  end
+
+  def self.truncate_databases
+    all.each{|s| s.truncate_database }
+  end
 
   def self.[] index
     self.all[index]
@@ -21,6 +34,7 @@ class Server
   end
 
   def ensure_database_setup
+    @@databases_setup = lambda {
     `cd #{Rails.root} && RAILS_ENV=#{@env} bundle exec rake db:create`
     tables_exist = self.db do
       ActiveRecord::Base.connection.tables.include?("users")
@@ -30,6 +44,7 @@ class Server
     else
       `cd #{Rails.root} && RAILS_ENV=#{@env} bundle exec rake db:schema:load`
     end
+    true}.call
   end
 
   def run
-- 
GitLab