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.



7
8
9
# File 'lib/rspec_queue/server.rb', line 7

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

Instance Method Details

#closeObject



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

def close
  @server.close
  FileUtils.rm socket_path
end

#dispatch(example_group_hash, report) ⇒ Object



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
56
57
58
# File 'lib/rspec_queue/server.rb', line 11

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



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

def register_worker(uid)
  worker_uuids << uid
end

#socket_pathObject



73
74
75
# File 'lib/rspec_queue/server.rb', line 73

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

#worker_uuidsObject



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

def worker_uuids
  @worker_uuids ||= []
end