tlb-testunit 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,21 @@
1
+ ;; copy me as .emacs_project in project root if using .emacs.d = git://github.com/janmejay/emacs.git
2
+ ;; after copying it across, modify the file to make it relevant to local rvm setup
3
+ (setq rspec-executable "rspec")
4
+ (setq rb_platform "jruby")
5
+ (setq rb_platform "mri")
6
+
7
+ (unless (getenv "ORIG_PATH")
8
+ (setenv "ORIG_PATH" (getenv "PATH")))
9
+
10
+ (defun add-gem-bin (gem-bin)
11
+ (setenv "PATH" (concat gem-bin ":" (getenv "ORIG_PATH"))))
12
+
13
+ (if (equal rb_platform "jruby")
14
+ (progn
15
+ (setenv "GEM_HOME" "/home/janmejay/.rvm/gems/jruby-1.5.6@tlb")
16
+ (setenv "GEM_PATH" "/home/janmejay/.rvm/gems/jruby-1.5.6@tlb:/home/janmejay/.rvm/gems/jruby-1.5.6@global")
17
+ (add-gem-bin "/home/janmejay/.rvm/gems/jruby-1.5.6@tlb/bin"))
18
+ (progn
19
+ (setenv "GEM_HOME" "/home/janmejay/.rvm/gems/ruby-1.8.7-p334@tlb")
20
+ (setenv "GEM_PATH" "/home/janmejay/.rvm/gems/ruby-1.8.7-p334@tlb:/home/janmejay/.rvm/gems/ruby-1.8.7-p334@global")
21
+ (add-gem-bin "/home/janmejay/.rvm/gems/ruby-1.8.7-p334@tlb/bin")))
data/.gitignore CHANGED
@@ -4,3 +4,5 @@ tlb_store
4
4
  tlb-*.jar
5
5
  .yardoc
6
6
  doc
7
+ *.gem
8
+ .emacs_project
data/Rakefile CHANGED
@@ -23,4 +23,5 @@ end
23
23
  task :package do
24
24
  `gem build tlb-rspec2.gemspec`
25
25
  `gem build tlb-testunit.gemspec`
26
+ `gem build tlb-cucumber.gemspec`
26
27
  end
data/lib/tlb.rb CHANGED
@@ -23,7 +23,7 @@ module Tlb
23
23
  end
24
24
 
25
25
  def self.port
26
- ENV[TLB_BALANCER_PORT]
26
+ ENV[TLB_BALANCER_PORT] || '8019'
27
27
  end
28
28
 
29
29
  def self.send path, data
@@ -44,10 +44,13 @@ module Tlb
44
44
  false
45
45
  end
46
46
 
47
+ def self.terminate
48
+ get("/control/suicide")
49
+ end
50
+
47
51
  def self.wait_for_start
48
52
  loop do
49
53
  begin
50
- TCPSocket.new(host, port)
51
54
  break if running?
52
55
  rescue
53
56
  #ignore
@@ -67,6 +70,10 @@ module Tlb
67
70
  rel_file_name = abs_file_name.sub(/^#{Dir.pwd}/, '.')
68
71
  end
69
72
 
73
+ def self.relative_file_paths file_names
74
+ file_names.map { |file_name| relative_file_path(file_name) }
75
+ end
76
+
70
77
  def self.balance_and_order file_set
71
78
  ensure_server_running
72
79
  Balancer.send(Balancer::BALANCE_PATH, file_set.join("\n")).split("\n")
@@ -106,37 +113,126 @@ module Tlb
106
113
  "java -jar #{tlb_jar}"
107
114
  end
108
115
 
109
- def self.write_to_file file_var, clob
110
- File.open(ENV[file_var], 'a') do |h|
111
- h.write(clob)
116
+ def self.can_fork?
117
+ RUBY_PLATFORM != 'java'
118
+ end
119
+
120
+ class BalancerProcess
121
+ class StreamPumper
122
+ def initialize stream, file
123
+ @stream, @file = stream, file
124
+ @thd = Thread.new { pump }
125
+ end
126
+
127
+ def pump
128
+ loop do
129
+ data_available? && flush_stream
130
+ Thread.current[:stop_pumping] && break
131
+ sleep 0.1
132
+ end
133
+ end
134
+
135
+ def flush_stream
136
+ File.open(ENV[@file], 'a') do |h|
137
+ h.write(read)
138
+ end
139
+ end
140
+
141
+ def stop_pumping!
142
+ @thd[:stop_pumping] = true
143
+ @thd.join
144
+ end
145
+ end
146
+
147
+ def initialize server_command
148
+ pumper_type, out, err = start(server_command)
149
+ @out_pumper = pumper_type.new(out, TLB_OUT_FILE)
150
+ @err_pumper = pumper_type.new(err, TLB_ERR_FILE)
151
+ end
152
+
153
+ def stop_pumping
154
+ @out_pumper.stop_pumping!
155
+ @err_pumper.stop_pumping!
156
+ end
157
+
158
+ def die
159
+ Balancer.terminate
160
+ stop_pumping
112
161
  end
113
162
  end
114
163
 
115
- def self.start_server
116
- ENV[TLB_APP] = 'tlb.balancer.BalancerInitializer'
117
- @pid, input, out, err = Open4.popen4(server_command)
118
- @out_pumper = stream_pumper_for(out, TLB_OUT_FILE)
119
- @err_pumper = stream_pumper_for(err, TLB_ERR_FILE)
120
- Balancer.wait_for_start
164
+ class ForkBalancerProcess < BalancerProcess
165
+ def start server_command
166
+ @pid, input, out, err = Open4.popen4(server_command)
167
+ unless (out)
168
+ raise "out was nil"
169
+ end
170
+ return Class.new(StreamPumper) do
171
+ def data_available?
172
+ not @stream.eof?
173
+ end
174
+
175
+ def read
176
+ @stream.read
177
+ end
178
+ end, out, err
179
+ end
180
+
181
+ def die
182
+ super
183
+ @pid = nil
184
+ Process.wait
185
+ end
121
186
  end
122
187
 
123
- def self.stream_pumper_for stream, dump_file
124
- Thread.new do
125
- loop do
126
- stream.eof? || write_to_file(dump_file, stream.read)
127
- Thread.current[:stop_pumping] && break
128
- sleep 1
188
+ class JavaBalancerProcess < BalancerProcess
189
+ def start server_command
190
+ require 'java'
191
+ pb = java.lang.ProcessBuilder.new(server_command.split)
192
+ ENV.each do |key, val|
193
+ pb.environment[key] = val
129
194
  end
195
+ @process = pb.start()
196
+ return Class.new(StreamPumper) do
197
+ def data_available?
198
+ @stream.ready
199
+ end
200
+
201
+ def read
202
+ @stream.read_line
203
+ end
204
+
205
+ def stop_pumping!
206
+ super
207
+ @stream.close
208
+ end
209
+ end, buf_reader(@process.input_stream), buf_reader(@process.error_stream)
210
+ end
211
+
212
+ def buf_reader stream
213
+ java.io.BufferedReader.new(java.io.InputStreamReader.new(stream))
130
214
  end
215
+
216
+ def die
217
+ super
218
+ @process.destroy
219
+ @process.waitFor
220
+ @process = nil
221
+ end
222
+ end
223
+
224
+ def self.balancer_process_type
225
+ can_fork? ? ForkBalancerProcess : JavaBalancerProcess
226
+ end
227
+
228
+ def self.start_server
229
+ ENV[TLB_APP] = 'tlb.balancer.BalancerInitializer'
230
+ bal_klass = balancer_process_type
231
+ @balancer_process = bal_klass.new(server_command)
232
+ Balancer.wait_for_start
131
233
  end
132
234
 
133
235
  def self.stop_server
134
- Process.kill(Signal.list["TERM"], @pid)
135
- @pid = nil
136
- @out_pumper[:stop_pumping] = true
137
- @err_pumper[:stop_pumping] = true
138
- @out_pumper.join
139
- @err_pumper.join
140
- Process.wait
236
+ @balancer_process.die
141
237
  end
142
238
  end
data/spike/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ require 'rubygems'
2
+ require 'cucumber'
3
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'tlb'))
4
+ require 'tlb/cucumber/rake/cucumber_task'
5
+
6
+ Tlb::Cucumber::Rake::CucumberTask.new(:cucumber_tests)
7
+
8
+ load 'tasks/tlb.rake'
9
+ desc "Run Cucumber features in a load-balanced fashion (based on environment variables)"
10
+ task :bal => ['tlb:start', :cucumber_tests]
11
+
12
+
@@ -0,0 +1,18 @@
1
+ require ('test/unit/assertions')
2
+
3
+ World(Test::Unit::Assertions)
4
+
5
+ Given /There are two numbers (\d+) and (\d+)/ do |one, two|
6
+ instance_variable_set("@first", one)
7
+ instance_variable_set("@second", two)
8
+ end
9
+
10
+ When /I add them together/ do
11
+ one = instance_variable_get("@first")
12
+ two = instance_variable_get("@second")
13
+ instance_variable_set("@sum", one.to_i + two.to_i)
14
+ end
15
+
16
+ Then /I should get (\d+)/ do |expected_sum|
17
+ assert_equal expected_sum.to_i, instance_variable_get("@sum")
18
+ end
@@ -0,0 +1,13 @@
1
+ require ('test/unit/assertions')
2
+
3
+ World(Test::Unit::Assertions)
4
+
5
+ Given /I am wondering what the hell is the answer/ do
6
+ end
7
+
8
+ When /I ask Deep thought/ do
9
+ end
10
+
11
+ Then /Deep Thought should say "(\d+)"/ do |answer|
12
+ assert_equal 42, answer.to_i
13
+ end
@@ -0,0 +1,18 @@
1
+ require ('test/unit/assertions')
2
+
3
+ World(Test::Unit::Assertions)
4
+
5
+ Given /There are two numbers for subtraction (\d+) and (\d+)/ do |one, two|
6
+ instance_variable_set("@first", one)
7
+ instance_variable_set("@second", two)
8
+ end
9
+
10
+ When /I subtract second from the first/ do
11
+ one = instance_variable_get("@first")
12
+ two = instance_variable_get("@second")
13
+ instance_variable_set("@diff", one.to_i - two.to_i)
14
+ end
15
+
16
+ Then /I should get the difference (\d+)/ do |expected_difference|
17
+ assert_equal expected_difference.to_i, instance_variable_get("@diff")
18
+ end
@@ -0,0 +1,6 @@
1
+ Feature: Subtraction
2
+
3
+ Scenario: Make sure I can subtract 2 numbers
4
+ Given There are two numbers for subtraction 2 and 1
5
+ When I subtract second from the first
6
+ Then I should get the difference 0
@@ -0,0 +1,6 @@
1
+ Feature: Addition
2
+
3
+ Scenario: Make sure I can add 2 numbers
4
+ Given There are two numbers 1 and 2
5
+ When I add them together
6
+ Then I should get 3
@@ -0,0 +1,6 @@
1
+ Feature: The Answer mate!
2
+
3
+ Scenario: What is the answer mate?
4
+ Given I am wondering what the hell is the answer
5
+ When I ask Deep thought
6
+ Then Deep Thought should say "42"
data/test.sh ADDED
@@ -0,0 +1,42 @@
1
+ #!/bin/bash
2
+
3
+ # this is a smart hack strictly meant for developer convinience, not for CI. -janmejay
4
+
5
+ [[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"
6
+
7
+ sep() {
8
+ in_red "-----------------------------------------------------------------"
9
+ echo
10
+ }
11
+
12
+ function in_red {
13
+ tput sgr0
14
+ tput setaf 1;
15
+ tput setab 7;
16
+ echo -n $1;
17
+ tput sgr0
18
+ }
19
+
20
+ function show_running_with {
21
+ echo -e "\n"
22
+ sep
23
+ in_red "|"
24
+ echo -n " "
25
+ tput setaf 4
26
+ echo -n "Running tests with: "
27
+ tput bold;
28
+ tput setaf 0
29
+ ruby --version
30
+ sep
31
+ echo
32
+ }
33
+
34
+ run_tests_with() {
35
+ rvm use $1
36
+ show_running_with
37
+ rake test
38
+ }
39
+
40
+ run_tests_with ruby-1.8.7-p334@tlb
41
+
42
+ run_tests_with jruby-1.5.6@tlb
@@ -0,0 +1,9 @@
1
+ $name="tlb-cucumber"
2
+ $framework='cucumber'
3
+ require File.join(File.dirname(__FILE__), 'gem_common')
4
+
5
+ Gem::Specification.new do |s|
6
+ configure_tlb(s)
7
+ s.files = files('tests', File.join('lib', 'tlb', 'rspec'), File.join('lib', 'tlb', 'test_unit'))
8
+ s.add_runtime_dependency 'cucumber', '>= 0.10.2'
9
+ end
data/tlb-rspec2.gemspec CHANGED
@@ -4,8 +4,7 @@ require File.join(File.dirname(__FILE__), 'gem_common')
4
4
 
5
5
  Gem::Specification.new do |s|
6
6
  configure_tlb(s)
7
-
8
- s.files = files('tests', File.join('lib', 'tlb', 'test_unit'))
9
-
7
+ s.files = files('tests', File.join('lib', 'tlb', 'test_unit'), File.join('lib', 'tlb', 'cucumber'))
10
8
  s.add_runtime_dependency 'rspec', '>= 2.3.0'
11
9
  end
10
+
data/tlb-testunit.gemspec CHANGED
@@ -4,6 +4,5 @@ require File.join(File.dirname(__FILE__), 'gem_common')
4
4
 
5
5
  Gem::Specification.new do |s|
6
6
  configure_tlb(s)
7
-
8
- s.files = files('tests', File.join('lib', 'tlb', 'rspec'))
7
+ s.files = files('tests', File.join('lib', 'tlb', 'rspec'), File.join('lib', 'tlb', 'cucumber'))
9
8
  end
metadata CHANGED
@@ -1,54 +1,49 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tlb-testunit
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
5
- prerelease:
4
+ prerelease: false
6
5
  segments:
7
- - 0
8
- - 3
9
- - 0
10
- version: 0.3.0
6
+ - 0
7
+ - 3
8
+ - 1
9
+ version: 0.3.1
11
10
  platform: ruby
12
11
  authors:
13
- - Janmejay Singh
14
- - Pavan KS
12
+ - Janmejay Singh
13
+ - Pavan KS
15
14
  autorequire:
16
15
  bindir: bin
17
16
  cert_chain: []
18
17
 
19
- date: 2011-03-05 00:00:00 +05:30
18
+ date: 2011-05-11 00:00:00 +05:30
20
19
  default_executable:
21
20
  dependencies:
22
- - !ruby/object:Gem::Dependency
23
- name: open4
24
- prerelease: false
25
- requirement: &id001 !ruby/object:Gem::Requirement
26
- none: false
27
- requirements:
28
- - - ">="
29
- - !ruby/object:Gem::Version
30
- hash: 21
31
- segments:
32
- - 1
33
- - 0
34
- - 1
35
- version: 1.0.1
36
- type: :runtime
37
- version_requirements: *id001
38
- - !ruby/object:Gem::Dependency
39
- name: rake
40
- prerelease: false
41
- requirement: &id002 !ruby/object:Gem::Requirement
42
- none: false
43
- requirements:
44
- - - ">="
45
- - !ruby/object:Gem::Version
46
- hash: 3
47
- segments:
48
- - 0
49
- version: "0"
50
- type: :runtime
51
- version_requirements: *id002
21
+ - !ruby/object:Gem::Dependency
22
+ name: open4
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 1
30
+ - 0
31
+ - 1
32
+ version: 1.0.1
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: rake
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ segments:
43
+ - 0
44
+ version: "0"
45
+ type: :runtime
46
+ version_requirements: *id002
52
47
  description: |
53
48
  TLB ruby implementation base, which provides support for load balancing tests written in test::unit.
54
49
  TLB.rb test suite is not bundled, please check http://github.com/test-load-balancer/tlb.rb for tests.
@@ -60,25 +55,34 @@ executables: []
60
55
  extensions: []
61
56
 
62
57
  extra_rdoc_files:
63
- - README.markdown
58
+ - README.markdown
64
59
  files:
65
- - .emacs_project
66
- - .gitignore
67
- - .gitmodules
68
- - README.markdown
69
- - Rakefile
70
- - gem_common.rb
71
- - lib/tasks/tlb.rake
72
- - lib/tlb.rb
73
- - lib/tlb/run_data.rb
74
- - lib/tlb/test_unit/mediator_inflection.rb
75
- - lib/tlb/test_unit/test_observer.rb
76
- - lib/tlb/test_unit/test_splitter.rb
77
- - lib/tlb/test_unit/test_task.rb
78
- - lib/tlb/util.rb
79
- - tlb-rspec2.gemspec
80
- - tlb-testunit.gemspec
81
- - tlb-alien-g0.3.0.jar
60
+ - .emacs_project.template
61
+ - .gitignore
62
+ - .gitmodules
63
+ - README.markdown
64
+ - Rakefile
65
+ - gem_common.rb
66
+ - lib/tasks/tlb.rake
67
+ - lib/tlb.rb
68
+ - lib/tlb/run_data.rb
69
+ - lib/tlb/test_unit/mediator_inflection.rb
70
+ - lib/tlb/test_unit/test_observer.rb
71
+ - lib/tlb/test_unit/test_splitter.rb
72
+ - lib/tlb/test_unit/test_task.rb
73
+ - lib/tlb/util.rb
74
+ - spike/Rakefile
75
+ - spike/features/step_definitions/addition.rb
76
+ - spike/features/step_definitions/answer.rb
77
+ - spike/features/step_definitions/sub.rb
78
+ - spike/features/sub.feature
79
+ - spike/features/sum.feature
80
+ - spike/features/the_answer.feature
81
+ - test.sh
82
+ - tlb-cucumber.gemspec
83
+ - tlb-rspec2.gemspec
84
+ - tlb-testunit.gemspec
85
+ - tlb-alien-g0.3.0-4-g1077d1b.jar
82
86
  has_rdoc: true
83
87
  homepage: http://github.com/test-load-balancer/tlb.rb
84
88
  licenses: []
@@ -102,33 +106,29 @@ post_install_message: |
102
106
  -------------------------------------------------------------------------
103
107
 
104
108
  rdoc_options:
105
- - --charset=UTF-8
109
+ - --charset=UTF-8
106
110
  require_paths:
107
- - lib
111
+ - lib
108
112
  required_ruby_version: !ruby/object:Gem::Requirement
109
- none: false
110
113
  requirements:
111
- - - ">="
112
- - !ruby/object:Gem::Version
113
- hash: 3
114
- segments:
115
- - 0
116
- version: "0"
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ segments:
117
+ - 0
118
+ version: "0"
117
119
  required_rubygems_version: !ruby/object:Gem::Requirement
118
- none: false
119
120
  requirements:
120
- - - ">="
121
- - !ruby/object:Gem::Version
122
- hash: 3
123
- segments:
124
- - 0
125
- version: "0"
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ segments:
124
+ - 0
125
+ version: "0"
126
126
  requirements: []
127
127
 
128
128
  rubyforge_project: tlb-rb
129
- rubygems_version: 1.5.2
129
+ rubygems_version: 1.3.6
130
130
  signing_key:
131
131
  specification_version: 3
132
- summary: tlb-testunit-0.3.0
132
+ summary: tlb-testunit-0.3.1
133
133
  test_files: []
134
134
 
data/.emacs_project DELETED
@@ -1,7 +0,0 @@
1
- (setq rspec-executable "rspec")
2
- (setenv "GEM_HOME" "/home/janmejay/.rvm/gems/ruby-1.8.7-head@vanilla")
3
- (setenv "GEM_PATH" "/home/janmejay/.rvm/gems/ruby-1.8.7-head@vanilla:/home/janmejay/.rvm/gems/ruby-1.8.7-head@global")
4
- (let ((gem-bin "/home/janmejay/.rvm/gems/ruby-1.8.7-head@vanilla/bin")
5
- (path (getenv "PATH")))
6
- (unless (string-match-p gem-bin path)
7
- (setenv "PATH" (concat gem-bin ":" path))))