Class: SauceTunnel::Tunnel

Inherits:
Object
  • Object
show all
Defined in:
lib/sauce_tunnel/tunnel.rb

Defined Under Namespace

Classes: Error

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sc_path: "sc", sc_args: [], quiet: false, timeout: 120, shutdown_timeout: 20) ⇒ Tunnel

Returns a new instance of Tunnel.



7
8
9
10
11
12
13
14
15
16
# File 'lib/sauce_tunnel/tunnel.rb', line 7

def initialize(sc_path: "sc", sc_args: [], quiet: false, timeout: 120, shutdown_timeout: 20)
  @sc_path = sc_path
  @sc_args = sc_args
  @quiet = quiet
  @timeout = timeout
  @shutdown_timeout = shutdown_timeout

  @queue = SauceTunnel::SimpleQueue.new
  @up = false
end

Instance Attribute Details

#sc_argsObject (readonly)

Returns the value of attribute sc_args.



5
6
7
# File 'lib/sauce_tunnel/tunnel.rb', line 5

def sc_args
  @sc_args
end

#sc_pathObject (readonly)

Returns the value of attribute sc_path.



5
6
7
# File 'lib/sauce_tunnel/tunnel.rb', line 5

def sc_path
  @sc_path
end

Instance Method Details

#awaitObject



31
32
33
34
35
36
# File 'lib/sauce_tunnel/tunnel.rb', line 31

def await
  @up ||= @queue.pop(@timeout)
  self
rescue ThreadError
  raise ConnectionError, "timed out trying to establish connection to SauceLabs"
end

#connectObject



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/sauce_tunnel/tunnel.rb', line 18

def connect
  return if @wait_thread # don't start twice

  @stdin, @stdout, @stderr, @wait_thread = Open3.popen3(Shellwords.join([sc_path] + [sc_args].flatten))

  @read_thread = Thread.new do
    while line = @stdout.gets
      puts line unless quiet?
      @queue.push(true) if line =~ /sauce connect is up/i
    end
  end
end

#quiet?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/sauce_tunnel/tunnel.rb', line 47

def quiet?
  @quiet
end

#terminateObject



38
39
40
41
42
43
44
45
# File 'lib/sauce_tunnel/tunnel.rb', line 38

def terminate
  if @wait_thread
    Process.kill("TERM", @wait_thread.pid)
    @wait_thread.join(@shutdown_timeout)
  end
rescue Errno::ESRCH
  # do nothing, tunnel already shut down
end