Module: Borg::AbstractAdapter

Included in:
CucumberRunner, TestUnit
Defined in:
lib/borg/borg_abstract_adapter.rb

Instance Method Summary collapse

Instance Method Details

#add_files_to_redis(files, key) ⇒ Object



107
108
109
110
# File 'lib/borg/borg_abstract_adapter.rb', line 107

def add_files_to_redis(files,key)
  redis.del key
  files.each { |x| redis.rpush(key, x.join(",")) }
end

#configObject



99
100
101
# File 'lib/borg/borg_abstract_adapter.rb', line 99

def config
  ActiveRecord::Base.configurations
end

#create_db_using_raw_sql(db_counter) ⇒ Object



79
80
81
82
83
84
85
86
# File 'lib/borg/borg_abstract_adapter.rb', line 79

def create_db_using_raw_sql(db_counter)
  test_config = config['test']
  sql_connection = Mysql2::Client.new(test_config.symbolize_keys)
  db_config = get_connection_config(db_counter)
  sql_connection.query("DROP DATABASE IF EXISTS #{db_config['database']}")
  sql_connection.query("CREATE DATABASE #{db_config['database']}")
  sql_connection.close()
end

#get_connection_config(db_counter) ⇒ Object



93
94
95
96
97
# File 'lib/borg/borg_abstract_adapter.rb', line 93

def get_connection_config(db_counter)
  default_settings = config["test"].clone()
  default_settings['database'] = "#{default_settings['database']}_#{db_counter}"
  default_settings
end

#load_environment(env_name) ⇒ Object



3
4
5
6
7
8
9
10
11
# File 'lib/borg/borg_abstract_adapter.rb', line 3

def load_environment(env_name)
  puts "Loading Rails.."
  ENV["RAILS_ENV"] = env_name
  Rails.env = env_name
  require(File.join(Rails.root, 'config', 'environment'))
  $: << "#{Rails.root}/test"
  $: << "#{Rails.root}/test/test_helpers"
  require File.join(Rails.root, "test", "test_helper")
end

#migrate_dbObject



88
89
90
91
# File 'lib/borg/borg_abstract_adapter.rb', line 88

def migrate_db
  ENV["VERBOSE"] = "true"
  Rake::Task["db:migrate"].invoke
end

#prepare_databse(db_counter) ⇒ Object



74
75
76
77
# File 'lib/borg/borg_abstract_adapter.rb', line 74

def prepare_databse(db_counter)
  create_db_using_raw_sql(db_counter)
  try_migration_first(db_counter)
end

#redirect_io(logfile_name) ⇒ Object

Free file descriptors and point them somewhere sensible STDOUT/STDERR should go to a logfile



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/borg/borg_abstract_adapter.rb', line 16

def redirect_io(logfile_name)
  begin
    ; STDIN.reopen "/dev/null";
  rescue ::Exception;
  end

  if logfile_name
    begin
      STDOUT.reopen logfile_name, "a"
      STDOUT.sync = true
    rescue ::Exception
      begin
        ; STDOUT.reopen "/dev/null";
      rescue ::Exception;
      end
    end
  else
    begin
      ; STDOUT.reopen "/dev/null";
    rescue ::Exception;
    end
  end

  begin
    ; STDERR.reopen STDOUT;
  rescue ::Exception;
  end
  STDERR.sync = true
end

#redirect_stdoutObject



46
47
48
49
50
51
52
53
# File 'lib/borg/borg_abstract_adapter.rb', line 46

def redirect_stdout
  STDOUT.sync = true
  begin
    STDERR.reopen STDOUT;
  rescue ::Exception;
  end
  STDERR.sync = true
end

#redisObject



103
104
105
# File 'lib/borg/borg_abstract_adapter.rb', line 103

def redis
  Redis.new(:host => Borg::Config.redis_ip,:port => Borg::Config.redis_port)
end

#remove_file_groups_from_redis(key, process_count, &block) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/borg/borg_abstract_adapter.rb', line 112

def remove_file_groups_from_redis(key,process_count,&block)
  redis_has_files = true
  @redis_connection = redis
  all_status = []

  loop do
    local_pids = []
    process_count.times do |index|
      test_files = @redis_connection.rpop(key)
      if(test_files)
        local_pids << Process.fork { block.call(index,test_files) }
      else
        redis_has_files = false
        break
      end
    end

    Signal.trap 'SIGINT', lambda { local_pids.each { |p| Process.kill("KILL", p) }; exit 1 }
    all_status += Process.waitall.map { |pid, status| status.exitstatus }
    break unless redis_has_files
  end #end of loop

  raise "Error running #{key} tests" if (all_status.any? { |x| x != 0 })

end

#try_migration_first(db_counter) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/borg/borg_abstract_adapter.rb', line 55

def try_migration_first(db_counter)
  begin
    db_config = get_connection_config(db_counter)
    ActiveRecord::Base.establish_connection(db_config)
    ActiveRecord::Base.connection()
    migrate_db()
    return true
  rescue Exception => e
    puts e.message
    return false
  rescue StandardError
    puts $!.message
    return false
  rescue Mysql2::Error
    puts $!.message
    return false
  end
end