Class: Specwrk::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/specwrk/client.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



49
50
51
52
53
54
# File 'lib/specwrk/client.rb', line 49

def initialize
  @mutex = Mutex.new
  @http = self.class.build_http
  @http.start
  @worker_status = 1
end

Instance Attribute Details

#last_request_atObject (readonly)

Returns the value of attribute last_request_at.



47
48
49
# File 'lib/specwrk/client.rb', line 47

def last_request_at
  @last_request_at
end

#retry_countObject (readonly)

Returns the value of attribute retry_count.



47
48
49
# File 'lib/specwrk/client.rb', line 47

def retry_count
  @retry_count
end

#worker_statusObject (readonly)

Returns the value of attribute worker_status.



47
48
49
# File 'lib/specwrk/client.rb', line 47

def worker_status
  @worker_status
end

Class Method Details

.build_httpObject



25
26
27
28
29
30
31
32
33
# File 'lib/specwrk/client.rb', line 25

def self.build_http
  uri = URI(ENV.fetch("SPECWRK_SRV_URI", "http://localhost:5138"))
  Specwrk.net_http.new(uri.host, uri.port).tap do |http|
    http.use_ssl = uri.scheme == "https"
    http.open_timeout = ENV.fetch("SPECWRK_TIMEOUT", "5").to_i
    http.read_timeout = ENV.fetch("SPECWRK_TIMEOUT", "5").to_i
    http.keep_alive_timeout = 300
  end
end

.connect?Boolean

Returns:

  • (Boolean)


15
16
17
18
19
20
21
22
23
# File 'lib/specwrk/client.rb', line 15

def self.connect?
  http = build_http
  http.start
  http.finish

  true
rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
  false
end

.wait_for_server!Object

Raises:

  • (Errno::ECONNREFUSED)


35
36
37
38
39
40
41
42
43
44
45
# File 'lib/specwrk/client.rb', line 35

def self.wait_for_server!
  timeout = Time.now + ENV.fetch("SPECWRK_TIMEOUT", "5").to_i
  connected = false

  until connected || Time.now > timeout
    connected = connect?
    sleep 0.1 unless connected
  end

  raise Errno::ECONNREFUSED unless connected
end

Instance Method Details

#closeObject



56
57
58
# File 'lib/specwrk/client.rb', line 56

def close
  @mutex.synchronize { @http.finish }
end

#complete_and_fetch_examples(examples) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/specwrk/client.rb', line 103

def complete_and_fetch_examples(examples)
  response = post "/complete_and_pop", body: examples.to_json

  case response.code
  when "200"
    JSON.parse(response.body, symbolize_names: true)
  when "204"
    raise WaitingForSeedError
  when "404"
    raise NoMoreExamplesError
  when "410"
    raise CompletedAllExamplesError
  else
    raise UnhandledResponseError.new("#{response.code}: #{response.body}")
  end
end

#fetch_examplesObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/specwrk/client.rb', line 86

def fetch_examples
  response = post "/pop"

  case response.code
  when "200"
    JSON.parse(response.body, symbolize_names: true)
  when "204"
    raise WaitingForSeedError
  when "404"
    raise NoMoreExamplesError
  when "410"
    raise CompletedAllExamplesError
  else
    raise UnhandledResponseError.new("#{response.code}: #{response.body}")
  end
end

#heartbeatObject



60
61
62
63
64
# File 'lib/specwrk/client.rb', line 60

def heartbeat
  response = get "/heartbeat"

  response.code == "200"
end

#reportObject



66
67
68
69
70
71
72
73
74
# File 'lib/specwrk/client.rb', line 66

def report
  response = get "/report"

  if response.code == "200"
    JSON.parse(response.body, symbolize_names: true)
  else
    raise UnhandledResponseError.new("#{response.code}: #{response.body}")
  end
end

#seed(examples, max_retries) ⇒ Object



120
121
122
123
124
# File 'lib/specwrk/client.rb', line 120

def seed(examples, max_retries)
  response = post "/seed", body: {max_retries: max_retries, examples: examples}.to_json

  (response.code == "200") ? true : raise(UnhandledResponseError.new("#{response.code}: #{response.body}"))
end

#shutdownObject



76
77
78
79
80
81
82
83
84
# File 'lib/specwrk/client.rb', line 76

def shutdown
  response = delete "/shutdown"

  if response.code == "200"
    response.body
  else
    raise UnhandledResponseError.new("#{response.code}: #{response.body}")
  end
end