Class: RSpecQueue::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec_queue/server.rb

Instance Method Summary collapse

Constructor Details

#initializeServer

Returns a new instance of Server.



4
5
6
# File 'lib/rspec_queue/server.rb', line 4

def initialize
  @server = UNIXServer.open(socket_path)
end

Instance Method Details

#closeObject



57
58
59
60
# File 'lib/rspec_queue/server.rb', line 57

def close
  @server.close
  FileUtils.rm socket_path
end

#dispatch(example_group_hash, report) ⇒ Object



8
9
10
11
12
13
14
15
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
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rspec_queue/server.rb', line 8

def dispatch(example_group_hash, report)
  example_group_keys = example_group_hash.keys

  while (example_group_keys.count > 0 || worker_uuids.count > 0) do
    begin
      socket = @server.accept
      message = socket.gets.to_s.strip

      case message
      when "REGISTER"
        worker_uid = generate_worker_uid
        register_worker(worker_uid)
        socket.puts worker_uid

      when "GET_WORK"
        if example_group_keys.count > 0
          socket.puts example_group_keys.shift
        else
          socket.puts "SHUT_DOWN"
        end

      when "FINISH"
        socket.puts "GET_UUID"
        uuid = socket.gets.to_s.strip

        worker_uuids.delete(uuid)

        socket.puts "GET_RESULTS"
        results = socket.gets.to_s.strip

        json_results = JSON.parse(results, symbolize_names: true)

        examples = json_results.select { |e| e[:status] == "passed" }
        failed_examples = json_results.select { |e| e[:status] == "failed" }
        pending_examples = json_results.select { |e| e[:status] == "pending" }

        report.examples.push(*examples)
        report.failed_examples.push(*failed_examples)
        report.pending_examples.push(*pending_examples)

      else
        puts("unsupported: #{message}")
      end
    ensure
      socket.close
    end
  end
end

#register_worker(uid) ⇒ Object



62
63
64
# File 'lib/rspec_queue/server.rb', line 62

def register_worker(uid)
  worker_uuids << uid
end

#socket_pathObject



70
71
72
# File 'lib/rspec_queue/server.rb', line 70

def socket_path
  "/tmp/rspec-queue-server-#{Process.pid}.sock"
end

#worker_uuidsObject



66
67
68
# File 'lib/rspec_queue/server.rb', line 66

def worker_uuids
  @worker_uuids ||= []
end