Class: Buffet::Master

Inherits:
Object
  • Object
show all
Defined in:
lib/buffet/master.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project, slaves, specs, listener) ⇒ Master

Returns a new instance of Master.



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/buffet/master.rb', line 10

def initialize project, slaves, specs, listener
  @project = project
  @slaves = slaves
  @stats = {:examples => 0, :failures => 0, :pending => 0}
  @slaves_stats = Hash[@slaves.map do |slave|
    [slave.user_at_host, stats.dup.merge!(:slave => slave)]
  end]
  @stats[:slaves] = @slaves_stats
  @lock = Mutex.new
  @failures = []
  @specs = specs.shuffle # Never have the same test distribution
  @listener = listener
end

Instance Attribute Details

#failuresObject (readonly)

Returns the value of attribute failures.



8
9
10
# File 'lib/buffet/master.rb', line 8

def failures
  @failures
end

#statsObject (readonly)

Returns the value of attribute stats.



8
9
10
# File 'lib/buffet/master.rb', line 8

def stats
  @stats
end

Instance Method Details

#example_failed(slave_name, details) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/buffet/master.rb', line 62

def example_failed slave_name, details
  @lock.synchronize do
    @stats[:examples] += 1
    @stats[:failures] += 1
    @slaves_stats[slave_name][:failures] += 1
    @failures << details
  end

  @listener.example_failed
end

#example_passed(slave_name, details) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/buffet/master.rb', line 53

def example_passed slave_name, details
  @lock.synchronize do
    @stats[:examples] += 1
    @slaves_stats[slave_name][:examples] += 1
  end

  @listener.example_passed
end

#example_pending(slave_name, details) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/buffet/master.rb', line 73

def example_pending slave_name, details
 @lock.synchronize do
   @stats[:examples] += 1
   @stats[:pending] += 1
   @slaves_stats[slave_name][:pending] += 1
 end

 @listener.example_pending
end

#next_file_for(slave_name) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/buffet/master.rb', line 44

def next_file_for slave_name
  file = @lock.synchronize { @specs.shift }
  if file
    slave = @slaves_stats[slave_name][:slave]
    @listener.spec_taken slave, file if file
  end
  file
end

#runObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/buffet/master.rb', line 24

def run
  start_service

  @stats[:total_time] = Benchmark.measure do
    threads = @slaves.map do |slave|
      Thread.new do
        time = Benchmark.measure do
          prepare_slave slave
          run_slave slave
        end.real
        @lock.synchronize { @slaves_stats[slave.name][:total_time] = time }
      end
    end

    threads.each { |t| t.join }
  end.real

  stop_service
end