bane 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,31 +1,33 @@
1
- require File.dirname(__FILE__) + '/../test_helper'
1
+ require File.expand_path(File.dirname(__FILE__)) + '/../test_helper'
2
2
  require 'mocha'
3
3
 
4
4
  class ConfigurationParserTest < Test::Unit::TestCase
5
5
 
6
6
  include Bane
7
7
 
8
- IRRELEVANT_BEHAVIOR = Module.new
8
+ IRRELEVANT_BEHAVIOR = Bane::Behaviors::CloseImmediately
9
+
10
+ def setup
11
+ ConfigurationParser.any_instance.stubs(:warn_about_deprecation)
12
+ end
9
13
 
10
14
  def test_should_map_single_port_and_server_name
11
- parser = ConfigurationParser.new(3000, "CloseAfterPause")
12
- assert_matches_configuration([
13
- {:port => 3000, :behavior => Behaviors::CloseAfterPause}
14
- ], parser)
15
+ expect_server_created_with :port => 3000, :behavior => Behaviors::CloseAfterPause
16
+
17
+ ConfigurationParser.new(3000, "CloseAfterPause")
15
18
  end
16
19
 
17
20
  def test_should_map_multiple_servers_given_one_starting_port
18
- parser = ConfigurationParser.new(3000, "CloseImmediately", "CloseAfterPause")
19
- assert_matches_configuration([
20
- {:port => 3000, :behavior => Behaviors::CloseImmediately},
21
- {:port => 3001, :behavior => Behaviors::CloseAfterPause}
22
- ], parser)
21
+ expect_server_created_with :port => 3000, :behavior => Behaviors::CloseImmediately
22
+ expect_server_created_with :port => 3001, :behavior => Behaviors::CloseAfterPause
23
+
24
+ ConfigurationParser.new(3000, "CloseImmediately", "CloseAfterPause")
23
25
  end
24
26
 
25
27
  def test_should_map_string_port
26
- parser = Bane::ConfigurationParser.new("3000", IRRELEVANT_BEHAVIOR)
27
- actual = parser.configurations[0]
28
- assert_equal 3000, actual.instance_variable_get(:@port), "Should have mapped port given a String"
28
+ expect_server_created_with :port => 3000, :behavior => IRRELEVANT_BEHAVIOR
29
+
30
+ Bane::ConfigurationParser.new("3000", IRRELEVANT_BEHAVIOR)
29
31
  end
30
32
 
31
33
  def test_should_raise_if_unknown_server_name
@@ -35,21 +37,20 @@ class ConfigurationParserTest < Test::Unit::TestCase
35
37
  end
36
38
 
37
39
  def test_should_map_server_when_given_class
38
- parser = ConfigurationParser.new(IRRELEVANT_PORT, Behaviors::CloseAfterPause)
39
- actual = parser.configurations[0]
40
- assert_equal Behaviors::CloseAfterPause, actual.instance_variable_get(:@behavior), "Wrong behavior"
40
+ expect_server_created_with :port => anything(), :behavior => Behaviors::CloseAfterPause
41
+
42
+ ConfigurationParser.new(IRRELEVANT_PORT, Behaviors::CloseAfterPause)
41
43
  end
42
44
 
43
45
  def test_should_ask_service_registry_for_all_behaviors_if_none_specified
44
46
  fake_behavior = unique_behavior
45
47
  another_fake_behavior = unique_behavior
46
48
 
49
+ expect_server_created_with :port => anything(), :behavior => fake_behavior
50
+ expect_server_created_with :port => anything(), :behavior => another_fake_behavior
51
+
47
52
  ServiceRegistry.expects(:all_servers).returns([fake_behavior, another_fake_behavior])
48
- parser = ConfigurationParser.new(4000)
49
- assert_matches_configuration([
50
- {:port => 4000, :behavior => fake_behavior},
51
- {:port => 4001, :behavior => another_fake_behavior}
52
- ], parser)
53
+ ConfigurationParser.new(4000)
53
54
  end
54
55
 
55
56
  def test_should_raise_exception_if_no_arguments
@@ -65,60 +66,46 @@ class ConfigurationParserTest < Test::Unit::TestCase
65
66
  end
66
67
 
67
68
  def test_should_map_single_hash_entry_as_port_and_behavior
68
- parser = ConfigurationParser.new(
69
+ expect_server_created_with :port => 10256, :behavior => Behaviors::CloseAfterPause
70
+
71
+ ConfigurationParser.new(
69
72
  10256 => Behaviors::CloseAfterPause
70
73
  )
71
-
72
- assert_matches_configuration([
73
- {:port => 10256, :behavior => Behaviors::CloseAfterPause}
74
- ], parser)
75
74
  end
76
75
 
77
76
  def test_should_map_multiple_hash_entries_as_port_and_behavior
78
- parser = ConfigurationParser.new(
77
+ expect_server_created_with :port => 10256, :behavior => Behaviors::CloseAfterPause
78
+ expect_server_created_with :port => 6450, :behavior => Behaviors::CloseImmediately
79
+
80
+ ConfigurationParser.new(
79
81
  10256 => Behaviors::CloseAfterPause,
80
82
  6450 => Behaviors::CloseImmediately
81
83
  )
82
-
83
- assert_matches_configuration([
84
- {:port => 10256, :behavior => Behaviors::CloseAfterPause},
85
- {:port => 6450, :behavior => Behaviors::CloseImmediately}
86
- ], parser)
87
84
  end
88
85
 
89
86
  def test_should_map_hash_with_options
90
- parser = ConfigurationParser.new(
87
+ expect_server_created_with :port => 10256, :behavior => Behaviors::CloseAfterPause, :options => { :duration => 3 }
88
+ expect_server_created_with :port => 11239, :behavior => Behaviors::CloseImmediately
89
+
90
+ ConfigurationParser.new(
91
91
  10256 => {:behavior => Behaviors::CloseAfterPause, :duration => 3},
92
92
  11239 => Behaviors::CloseImmediately
93
93
  )
94
-
95
- assert_matches_configuration([
96
- {:port => 10256, :behavior => Behaviors::CloseAfterPause},
97
- {:port => 11239, :behavior => Behaviors::CloseImmediately}
98
- ], parser)
99
94
  end
100
95
 
101
96
  private
102
97
 
103
- def assert_matches_configuration(expected_config, actual_config)
104
- actual_elements = actual_config.configurations
105
- assert_equal expected_config.size, actual_elements.size, "Did not create correct number of configurations. Actual: #{actual_elements}, expected #{expected_config}"
106
-
107
- expected_config.each do |expected|
108
- # We make no guarantee on the order of the configurations
109
- assert_includes_configuration(actual_elements, expected)
110
- end
111
- end
112
-
113
- def assert_includes_configuration(actual_elements, expected)
114
- expected_port = expected[:port]
115
- matching_config = actual_elements.detect { |actual| actual.instance_variable_get(:@port) == expected_port }
116
- assert_not_nil matching_config, "Should have found a configuration with port #{expected_port}"
117
- assert_equal expected[:behavior], matching_config.instance_variable_get(:@behavior), "Wrong behavior for port #{expected_port}"
98
+ def expect_server_created_with(arguments)
99
+ arguments = { :options => anything() }.merge(arguments)
100
+ a_behavior_instance = "an instance of #{arguments[:behavior]}.to_s"
101
+ behavior = arguments[:behavior]
102
+ behavior.expects(:new).with(arguments[:options]).returns(a_behavior_instance)
103
+ BehaviorServer.expects(:new).with(arguments[:port], a_behavior_instance, BehaviorServer::DEFAULT_HOST)
118
104
  end
119
105
 
106
+
120
107
  def unique_behavior
121
- Module.new
108
+ Class.new
122
109
  end
123
110
 
124
111
  end
@@ -1,4 +1,4 @@
1
- require File.dirname(__FILE__) + '/../test_helper'
1
+ require File.expand_path(File.dirname(__FILE__)) + '/../test_helper'
2
2
 
3
3
  class FakeConnectionTest < Test::Unit::TestCase
4
4
 
@@ -1,33 +1,34 @@
1
- require File.dirname(__FILE__) + '/../test_helper'
2
- require 'net/telnet'
1
+ require File.expand_path(File.dirname(__FILE__)) + '/../test_helper'
3
2
  require 'open-uri'
3
+ require 'mocha'
4
4
 
5
5
  class BaneIntegrationTest < Test::Unit::TestCase
6
6
 
7
7
  TEST_PORT = 4000
8
8
 
9
9
  def test_uses_specified_port_and_server
10
- run_server_with(TEST_PORT, "FixedResponse") do
11
- telnet_to TEST_PORT do |response|
10
+ run_server_with(TEST_PORT, Bane::Behaviors::FixedResponse) do
11
+ connect_to TEST_PORT do |response|
12
12
  assert !response.empty?, "Should have had a non-empty response"
13
13
  end
14
14
  end
15
15
  end
16
16
 
17
- def test_uses_behavior_options
17
+ def test_supports_deprecated_configuration
18
+ silence_deprecation_warning
18
19
  expected_message = "Expected test message"
19
20
  options = {TEST_PORT => {:behavior => Bane::Behaviors::FixedResponse,
20
21
  :message => expected_message}}
21
22
 
22
- run_server_with(options) do
23
- telnet_to TEST_PORT do |response|
23
+ run_server_with_deprecated(options) do
24
+ connect_to TEST_PORT do |response|
24
25
  assert_equal expected_message, response, "Wrong response from server"
25
26
  end
26
27
  end
27
28
  end
28
29
 
29
30
  def test_serves_http_requests
30
- run_server_with(TEST_PORT, "HttpRefuseAllCredentials") do
31
+ run_server_with(TEST_PORT, Bane::Behaviors::HttpRefuseAllCredentials) do
31
32
  begin
32
33
  open("http://localhost:#{TEST_PORT}/some/url").read
33
34
  flunk "Should have refused access"
@@ -35,33 +36,57 @@ class BaneIntegrationTest < Test::Unit::TestCase
35
36
  assert_match /401/, e.message
36
37
  end
37
38
  end
39
+ end
38
40
 
41
+ def test_supports_command_line_interface
42
+ run_server_with_cli_arguments(["--listen-on-localhost", TEST_PORT, "FixedResponse"]) do
43
+ connect_to TEST_PORT do |response|
44
+ assert !response.empty?, "Should have had a non-empty response"
45
+ end
46
+ end
39
47
  end
40
48
 
41
49
  private
42
50
 
43
- def run_server_with(*options)
44
- begin
45
- launcher = Bane::Launcher.new(Configuration(*options), quiet_logger)
46
- launcher.start
47
- yield
51
+ def run_server_with(port, behavior, &block)
52
+ behavior = Bane::BehaviorServer.new(port, behavior.new)
53
+ launcher = Bane::Launcher.new([behavior], quiet_logger)
54
+ launch_and_stop_safely(launcher, &block)
55
+ end
56
+
57
+ def run_server_with_deprecated(*options, &block)
58
+ launcher = Bane::Launcher.new(Configuration(*options), quiet_logger)
59
+ launch_and_stop_safely(launcher, &block)
60
+ end
61
+
62
+ def run_server_with_cli_arguments(arguments, &block)
63
+ config = Bane::CommandLineConfiguration.new()
64
+ launcher = Bane::Launcher.new(config.parse(arguments), quiet_logger)
65
+ launch_and_stop_safely(launcher, &block)
66
+ end
67
+
68
+ def launch_and_stop_safely(launcher, &block)
69
+ launcher.start
70
+ block.call
48
71
  ensure
49
72
  launcher.stop if launcher
50
- end
73
+ sleep 0.1 # Until we can fix the GServer stopping race condition (Issue #7)
51
74
  end
52
75
 
53
76
  def quiet_logger
54
77
  StringIO.new
55
78
  end
56
79
 
57
- def telnet_to(port)
80
+ def connect_to(port)
58
81
  begin
59
- telnet = Net::Telnet::new("Host" => "localhost",
60
- "Port" => port,
61
- "Timeout" => 5)
62
- yield telnet.read
82
+ connection = TCPSocket.new "localhost", port
83
+ yield connection.read
63
84
  ensure
64
- telnet.close
85
+ connection.close if connection
65
86
  end
66
87
  end
88
+
89
+ def silence_deprecation_warning
90
+ Bane::ConfigurationParser.any_instance.stubs(:warn_about_deprecation)
91
+ end
67
92
  end
@@ -1,4 +1,4 @@
1
- require File.dirname(__FILE__) + '/../test_helper'
1
+ require File.expand_path(File.dirname(__FILE__)) + '/../test_helper'
2
2
 
3
3
  class NaiveHttpResponseTest < Test::Unit::TestCase
4
4
 
@@ -1,4 +1,4 @@
1
- require File.dirname(__FILE__) + '/../test_helper'
1
+ require File.expand_path(File.dirname(__FILE__)) + '/../test_helper'
2
2
 
3
3
  class ServiceRegistryTest < Test::Unit::TestCase
4
4
 
metadata CHANGED
@@ -1,13 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bane
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
5
4
  prerelease: false
6
5
  segments:
7
6
  - 0
8
- - 2
7
+ - 3
9
8
  - 0
10
- version: 0.2.0
9
+ version: 0.3.0
11
10
  platform: ruby
12
11
  authors:
13
12
  - Daniel Wellman
@@ -15,26 +14,65 @@ autorequire:
15
14
  bindir: bin
16
15
  cert_chain: []
17
16
 
18
- date: 2010-07-02 00:00:00 -04:00
17
+ date: 2012-08-19 00:00:00 -04:00
19
18
  default_executable: bane
20
19
  dependencies:
21
20
  - !ruby/object:Gem::Dependency
22
- name: mocha
23
21
  prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
25
- none: false
22
+ type: :development
23
+ name: rdoc
24
+ version_requirements: &id001 !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 3
30
+ - 12
31
+ version: "3.12"
32
+ requirement: *id001
33
+ - !ruby/object:Gem::Dependency
34
+ prerelease: false
35
+ type: :development
36
+ name: bundler
37
+ version_requirements: &id002 !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ~>
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 1
43
+ - 1
44
+ - 5
45
+ version: 1.1.5
46
+ requirement: *id002
47
+ - !ruby/object:Gem::Dependency
48
+ prerelease: false
49
+ type: :development
50
+ name: jeweler
51
+ version_requirements: &id003 !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ~>
54
+ - !ruby/object:Gem::Version
55
+ segments:
56
+ - 1
57
+ - 8
58
+ - 4
59
+ version: 1.8.4
60
+ requirement: *id003
61
+ - !ruby/object:Gem::Dependency
62
+ prerelease: false
63
+ type: :development
64
+ name: mocha
65
+ version_requirements: &id004 !ruby/object:Gem::Requirement
26
66
  requirements:
27
67
  - - ">="
28
68
  - !ruby/object:Gem::Version
29
- hash: 43
30
69
  segments:
31
70
  - 0
32
71
  - 9
33
72
  - 8
34
73
  version: 0.9.8
35
- type: :development
36
- version_requirements: *id001
37
- description: " Bane is a test harness used to test your application's interaction with\n other servers. It is based upon the material from Michael Nygard's \"Release\n It!\" book as described in the \"Test Harness\" chapter.\n"
74
+ requirement: *id004
75
+ description: " Bane is a test harness used to test your application's interaction with\n other servers. It is based upon the material from Michael Nygard's \"Release\n It!\" book as described in the \"Test Harness\" chapter.\n"
38
76
  email: dan@danielwellman.com
39
77
  executables:
40
78
  - bane
@@ -47,26 +85,24 @@ extra_rdoc_files:
47
85
  files:
48
86
  - Rakefile
49
87
  - bin/bane
50
- - examples/simple_port_and_class_as_constant.rb
51
- - examples/simple_port_and_class_as_string.rb
52
- - examples/specify_behavior_options.rb
53
- - examples/specify_ports.rb
88
+ - examples/multiple_behaviors.rb
89
+ - examples/readme_example.rb
90
+ - examples/single_behavior.rb
54
91
  - lib/bane.rb
92
+ - lib/bane/behavior_server.rb
55
93
  - lib/bane/behaviors.rb
94
+ - lib/bane/command_line_configuration.rb
56
95
  - lib/bane/compatibility.rb
57
- - lib/bane/configuration.rb
58
96
  - lib/bane/configuration_parser.rb
59
- - lib/bane/delegating_gserver.rb
60
97
  - lib/bane/launcher.rb
61
98
  - lib/bane/naive_http_response.rb
62
99
  - lib/bane/service_registry.rb
100
+ - test/bane/behavior_server_test.rb
63
101
  - test/bane/behaviors_test.rb
102
+ - test/bane/command_line_configuration_test.rb
64
103
  - test/bane/configuration_parser_test.rb
65
- - test/bane/configuration_test.rb
66
- - test/bane/delegating_gserver_test.rb
67
104
  - test/bane/fake_connection_test.rb
68
105
  - test/bane/integration_test.rb
69
- - test/bane/launcher_test.rb
70
106
  - test/bane/naive_http_response_test.rb
71
107
  - test/bane/service_registry_test.rb
72
108
  - test/test_helper.rb
@@ -75,50 +111,33 @@ files:
75
111
  - TODO
76
112
  has_rdoc: true
77
113
  homepage: http://github.com/danielwellman/bane
78
- licenses: []
79
-
114
+ licenses:
115
+ - BSD
80
116
  post_install_message:
81
- rdoc_options:
82
- - --charset=UTF-8
117
+ rdoc_options: []
118
+
83
119
  require_paths:
84
120
  - lib
85
121
  required_ruby_version: !ruby/object:Gem::Requirement
86
- none: false
87
122
  requirements:
88
123
  - - ">="
89
124
  - !ruby/object:Gem::Version
90
- hash: 3
91
125
  segments:
92
126
  - 0
93
127
  version: "0"
94
128
  required_rubygems_version: !ruby/object:Gem::Requirement
95
- none: false
96
129
  requirements:
97
130
  - - ">="
98
131
  - !ruby/object:Gem::Version
99
- hash: 3
100
132
  segments:
101
133
  - 0
102
134
  version: "0"
103
135
  requirements: []
104
136
 
105
137
  rubyforge_project:
106
- rubygems_version: 1.3.7
138
+ rubygems_version: 1.3.6
107
139
  signing_key:
108
140
  specification_version: 3
109
141
  summary: A test harness for socket connections based upon ideas from Michael Nygard's 'Release It!'
110
- test_files:
111
- - test/bane/behaviors_test.rb
112
- - test/bane/configuration_parser_test.rb
113
- - test/bane/configuration_test.rb
114
- - test/bane/delegating_gserver_test.rb
115
- - test/bane/fake_connection_test.rb
116
- - test/bane/integration_test.rb
117
- - test/bane/launcher_test.rb
118
- - test/bane/naive_http_response_test.rb
119
- - test/bane/service_registry_test.rb
120
- - test/test_helper.rb
121
- - examples/simple_port_and_class_as_constant.rb
122
- - examples/simple_port_and_class_as_string.rb
123
- - examples/specify_behavior_options.rb
124
- - examples/specify_ports.rb
142
+ test_files: []
143
+