Skip to content

Commit

Permalink
Add sleep and debug actions; upd rubocop
Browse files Browse the repository at this point in the history
  • Loading branch information
palkan committed May 2, 2018
1 parent 1f32cc2 commit cd2b863
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 5 deletions.
4 changes: 3 additions & 1 deletion lib/wsdirector/client.rb
@@ -1,13 +1,14 @@
# frozen_string_literal: true

require "websocket-client-simple"
require "securerandom"

module WSDirector
# WebSocket client
class Client
WAIT_WHEN_EXPECTING_EVENT = 5

attr_reader :ws
attr_reader :ws, :id

# Create new WebSocket client and connect to WSDirector
# ws URL.
Expand All @@ -22,6 +23,7 @@ def initialize(ignore: nil)
open = Concurrent::Promise.new
client = self

@id = SecureRandom.hex(6)
@ws = WebSocket::Client::Simple.connect(path) do |ws|
ws.on(:open) do |_event|
open.set(true)
Expand Down
28 changes: 28 additions & 0 deletions lib/wsdirector/protocols/base.rb
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require "time"

module WSDirector
module Protocols
# Base protocol describes basic actions
Expand All @@ -20,6 +22,28 @@ def handle_step(step)
public_send(type, step)
end

# Sleeps for a specified number of seconds.
#
# If "shift" is provided than the initial value is
# shifted by random number from (-shift, shift).
#
# Set "debug" to true to print the delay time.
def sleep(step)
delay = step.fetch("time").to_f
shift = step.fetch("shift", 0).to_f

delay = delay - shift * rand + shift * rand

print("Sleep for #{delay}s") if step.fetch("debug", false)

Kernel.sleep delay if delay > 0
end

# Prints provided message
def debug(step)
print(step.fetch("message"))
end

def receive(step)
expected = step.fetch("data")
received = client.receive
Expand Down Expand Up @@ -99,6 +123,10 @@ def prepare_receive_error(expected, received)
++ got: #{received}
MSG
end

def print(msg)
$stdout.puts "DEBUG #{Time.now.iso8601} client=#{client.id} #{msg}\n"
end
end
end
end
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
@@ -1,6 +1,6 @@
# frozen_string_literal: true

$LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
$LOAD_PATH.unshift File.expand_path("../lib", __dir__)

require "wsdirector"

Expand Down
2 changes: 1 addition & 1 deletion spec/support/fixture_helper.rb
@@ -1,5 +1,5 @@
module FixtureHelper
def fixture_path(path)
File.join(File.expand_path("../../fixtures", __FILE__), path)
File.join(File.expand_path("../fixtures", __dir__), path)
end
end
4 changes: 2 additions & 2 deletions spec/support/integration_helper.rb
Expand Up @@ -2,12 +2,12 @@

module IntegrationHelpers
def run_wsdirector(scenario_path, chdir: nil, success: true, failure: false, options: "", env: {})
cli_path = File.expand_path("../../../bin/wsdirector", __FILE__)
cli_path = File.expand_path("../../bin/wsdirector", __dir__)

output, err, status = Open3.capture3(
env,
"bundle exec #{cli_path} #{scenario_path} #{WSDirector.config.ws_url} #{options}",
chdir: chdir || File.expand_path("../../fixtures", __FILE__)
chdir: chdir || File.expand_path("../fixtures", __dir__)
)
expect(status).to be_success, "Test #{scenario_path} #{options} failed with: #{err}" if success && !failure
expect(status).not_to be_success, "Test #{scenario_path} #{options} succeed with: #{output}" if failure
Expand Down

0 comments on commit cd2b863

Please sign in to comment.