litmus_paper 0.8.9 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,14 +4,25 @@ module LitmusPaper
4
4
  def initialize(ip, port, options = {})
5
5
  @ip, @port = ip, port
6
6
  @timeout = options.fetch(:timeout_seconds, 5)
7
+ @input_data = options[:input_data]
8
+ @expected_output = options[:expected_output]
7
9
  end
8
10
 
9
11
  def available?
12
+ response = true
13
+
10
14
  Timeout.timeout(@timeout) do
11
15
  socket = TCPSocket.new(@ip, @port)
16
+ if @input_data || @expected_output
17
+ socket.puts(@input_data.to_s)
18
+ data = socket.gets
19
+ response = data.chomp == @expected_output
20
+ LitmusPaper.logger.info("Response (#{response}) does not match expected output (#{@expected_output})")
21
+ end
12
22
  socket.close
13
23
  end
14
- true
24
+
25
+ response
15
26
  rescue Timeout::Error
16
27
  LitmusPaper.logger.info("Timeout connecting #{@ip}:#{@port}")
17
28
  false
@@ -1,3 +1,3 @@
1
1
  module LitmusPaper
2
- VERSION = "0.8.9"
2
+ VERSION = "0.9.0"
3
3
  end
@@ -1,15 +1,47 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe LitmusPaper::Dependency::TCP do
4
- before(:all) do
5
- @server = TCPServer.new 3333
6
- end
4
+ describe "accepts input data and compares result against an expected output " do
5
+ before(:each) do
6
+ @server = TCPServer.new 7333
7
+ @thread = Thread.start do
8
+ s = @server.accept
9
+ s.puts "+PONG"
10
+ end
11
+ end
12
+
13
+ after(:each) do
14
+ @server.close
15
+ @thread.join
16
+ end
17
+
18
+ describe "#available?" do
19
+ it "is true when expected_output equals response from socket" do
20
+ check = LitmusPaper::Dependency::TCP.new("127.0.0.1", 7333, :expected_output => "+PONG", :input_data => "PING")
21
+ check.should be_available
22
+ end
7
23
 
8
- after(:all) do
9
- @server.close
24
+ it "is true when expected_output equals response from socket when no input is supplied" do
25
+ check = LitmusPaper::Dependency::TCP.new("127.0.0.1", 7333, :expected_output => "+PONG")
26
+ check.should be_available
27
+ end
28
+
29
+ it "is false when expected_output does not equal response from socket" do
30
+ check = LitmusPaper::Dependency::TCP.new("127.0.0.1", 7333, :expected_output => "+PANG", :input_data => "PING")
31
+ check.should_not be_available
32
+ end
33
+ end
10
34
  end
11
35
 
12
36
  describe "#available?" do
37
+ before(:all) do
38
+ @server = TCPServer.new 3333
39
+ end
40
+
41
+ after(:all) do
42
+ @server.close
43
+ end
44
+
13
45
  it "is true when it's able to reach the ip and port" do
14
46
  check = LitmusPaper::Dependency::TCP.new("127.0.0.1", 3333)
15
47
  check.should be_available
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: litmus_paper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.9
4
+ version: 0.9.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-07-07 00:00:00.000000000 Z
12
+ date: 2015-08-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: sinatra