Skip to content

Commit

Permalink
#27 .enter
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Oct 14, 2024
1 parent 399aedb commit 4510038
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 8 deletions.
15 changes: 8 additions & 7 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,9 @@ GEM
others (> 0)
tago (> 0)
yaml (~> 0.3)
faraday (2.11.0)
faraday (2.12.0)
faraday-net_http (>= 2.0, < 3.4)
json
logger
faraday-http-cache (2.5.1)
faraday (>= 0.8)
Expand All @@ -94,10 +95,10 @@ GEM
ffi (1.17.0-x86_64-darwin)
ffi (1.17.0-x86_64-linux-gnu)
hashdiff (1.1.1)
i18n (1.14.5)
i18n (1.14.6)
concurrent-ruby (~> 1.0)
io-console (0.7.2)
irb (1.14.0)
irb (1.14.1)
rdoc (>= 4.0.0)
reline (>= 0.4.2)
iri (0.8.0)
Expand Down Expand Up @@ -135,7 +136,7 @@ GEM
stringio
public_suffix (6.0.1)
racc (1.8.1)
rack (3.1.7)
rack (3.1.8)
rack-session (2.0.0)
rack (>= 3.0.0)
rack-test (2.1.0)
Expand Down Expand Up @@ -173,7 +174,7 @@ GEM
rspec-expectations (3.13.3)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-mocks (3.13.1)
rspec-mocks (3.13.2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-rails (7.0.1)
Expand Down Expand Up @@ -228,10 +229,10 @@ GEM
addressable (>= 2.8.0)
crack (>= 0.3.2)
hashdiff (>= 0.4.0, < 2.0.0)
webrick (1.8.1)
webrick (1.8.2)
yaml (0.3.0)
yard (0.9.37)
zeitwerk (2.6.18)
zeitwerk (2.7.0)

PLATFORMS
arm64-darwin-22
Expand Down
36 changes: 36 additions & 0 deletions lib/baza-rb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,37 @@ def finish(id, zip)
end
end

# Enter a valve.
#
# @param [String] name Name of the job
# @param [String] badge Unique badge of the valve
# @param [String] why The reason
# @param [nil|Integer] job The ID of the job
# @return [String] The result just calculated or retrieved
def enter(name, badge, why, job)
elapsed(@loog, intro: "Entered valve #{badge} to #{name}") do
with_retries(max_tries: @retries, rescue: TimedOut) do
ret = checked(
Typhoeus::Request.get(
home.append('valves').append('result').add(badge:).to_s,
headers:
),
[200, 204]
)
return ret.body if ret.code == 200
r = yield
uri = home.append('valves').append('add')
.add(name:)
.add(badge:)
.add(why:)
.add(result: r.to_s)
uri = uri.add(job:) unless job.nil?
checked(Typhoeus::Request.post(uri.to_s, headers:), 302)
r
end
end
end

private

def headers
Expand Down Expand Up @@ -579,6 +610,11 @@ def home
.scheme(@ssl ? 'https' : 'http')
end

# Check the HTTP response and return it.
#
# @param [Typhoeus::Response] ret The response
# @param [Array<Integer>] allowed List of acceptable HTTP codes
# @return [Typhoeus::Response] The same response
def checked(ret, allowed = [200])
allowed = [allowed] unless allowed.is_a?(Array)
mtd = (ret.request.original_options[:method] || '???').upcase
Expand Down
12 changes: 11 additions & 1 deletion test/test_baza-rb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,16 @@ def test_live_durable_lock_unlock
end
end

def test_live_enter_valve
WebMock.enable_net_connect!
skip unless we_are_online
r = 'something'
n = fake_name
badge = fake_name
assert_equal(r, LIVE.enter(n, badge, 'no reason', nil) { r })
assert_equal(r, LIVE.enter(n, badge, 'no reason', nil) { nil })
end

def test_durable_place
WebMock.disable_net_connect!
stub_request(:post, 'https://example.org/durables/place').to_return(
Expand Down Expand Up @@ -297,7 +307,7 @@ def with_http_server(code, response, opts = {})
end

def fake_name
"fake-#{SecureRandom.hex(8)}"
"fake#{SecureRandom.hex(8)}"
end

def we_are_online
Expand Down

0 comments on commit 4510038

Please sign in to comment.