maestro_common 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,8 +3,10 @@ require 'onstomp'
3
3
  require 'onstomp/failover'
4
4
 
5
5
  module Maestro
6
-
7
6
  module MQHelper
7
+ class MQError < StandardError
8
+ end
9
+
8
10
  @@INFINITE_RETRIES = 2**32-1
9
11
  @@stomp = nil
10
12
  @@subs = {}
@@ -102,7 +104,7 @@ module Maestro
102
104
 
103
105
  def MQHelper.reconnect
104
106
  if !@@stomp
105
- raise Maestro::MQError, "Cannot reconnect, no existing MQ instance. Call 'connect' to create initial connection, then use 'reconnect' if connection fails"
107
+ raise Maestro::MQHelper::MQError, "Cannot reconnect, no existing MQ instance. Call 'connect' to create initial connection, then use 'reconnect' if connection fails"
106
108
  end
107
109
 
108
110
  if !@@stomp.connected?
@@ -6,7 +6,7 @@ module Maestro
6
6
  end
7
7
 
8
8
  class Messenger
9
- VALID_QUEUE_REGEX = "^/(?:queue|topic)/[A-Za-z0-9_]*$"
9
+ VALID_QUEUE_REGEX = "^/(?:queue|topic)/[A-Za-z0-9_\.-]*$"
10
10
 
11
11
  DEFAULT_SEND_OPTIONS = { :persistent => true, :content_type => 'application/json' }
12
12
  DEFAULT_SYNC_TIMEOUT = 5
@@ -1,5 +1,5 @@
1
1
  module Maestro
2
2
  module Common
3
- VERSION = '0.0.4'
3
+ VERSION = '0.0.5'
4
4
  end
5
5
  end
@@ -20,7 +20,7 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_dependency 'onstomp'
22
22
  spec.add_dependency 'logging', '>= 1.8.0'
23
- spec.add_dependency 'rubyzip', '>= 0.9.8'
23
+ spec.add_dependency 'rubyzip', '>= 0.9.8', "< 1.0.0" # 1.0 requires newer ruby/jruby
24
24
  spec.add_dependency 'json', '>= 1.4.6'
25
25
  spec.add_dependency 'eventmachine', ">=0.12.10"
26
26
 
@@ -28,5 +28,7 @@ Gem::Specification.new do |spec|
28
28
  spec.add_development_dependency 'rake'
29
29
  spec.add_development_dependency 'jruby-openssl'
30
30
  spec.add_development_dependency 'rspec', '>= 2.13.0'
31
+ spec.add_development_dependency 'stompserver_ng'
32
+ spec.add_development_dependency "mocha", '>=0.10.0'
31
33
 
32
34
  end
@@ -0,0 +1,100 @@
1
+ require 'spec_helper'
2
+ require 'tmpdir'
3
+
4
+ describe Maestro::MQHelper do
5
+ after :each do
6
+ Maestro::MQHelper.disconnect
7
+ end
8
+
9
+ before :each do
10
+ Maestro::MQHelper.reset
11
+ end
12
+
13
+ describe 'connection' do
14
+
15
+ it 'should initially be nil' do
16
+ conn = Maestro::MQHelper.connection
17
+ conn.should be_nil
18
+ end
19
+
20
+ it 'should be non-nil after connect has been called' do
21
+ conn = Maestro::MQHelper.connect
22
+ conn.should_not be_nil
23
+ end
24
+ end
25
+
26
+ describe 'connect' do
27
+
28
+ it 'should create a new connection' do
29
+ conn = Maestro::MQHelper.connect
30
+ conn.should_not be_nil
31
+ end
32
+
33
+ it 'should return existing connection if already connected' do
34
+ conn = Maestro::MQHelper.connect
35
+ conn2 = Maestro::MQHelper.connect
36
+ conn2.should == conn
37
+ end
38
+ end
39
+
40
+ describe 'reconnect' do
41
+ # Testing in order
42
+ it 'should raise an error if connect has not been called' do
43
+ expect { Maestro::MQHelper.reconnect }.to raise_error(Maestro::MQHelper::MQError)
44
+ end
45
+
46
+ it 'should reconnect and re-subscribe to previously subscribed connections' do
47
+ # Ensure we have a connection
48
+ Maestro::MQHelper.connect
49
+ Maestro::MQHelper.disconnect
50
+ conn = Maestro::MQHelper.reconnect
51
+ conn.should_not be_nil
52
+ end
53
+ end
54
+
55
+ describe 'disconnect' do
56
+ it 'should close connection, but leave object instantiated' do
57
+ conn = Maestro::MQHelper.connect
58
+ conn.connected?.should be_true
59
+ Maestro::MQHelper.disconnect
60
+ conn.connected?.should be_false
61
+ end
62
+ end
63
+
64
+ describe 'subscribe' do
65
+ it 'should set up a subscription' do
66
+ conn = Maestro::MQHelper.connect
67
+ sub = Maestro::MQHelper.subscribe('queue/test')
68
+ sub.should_not be_nil
69
+ end
70
+ end
71
+
72
+ describe 'unsubscribe' do
73
+ it 'should clear a subscription when unsubscribed' do
74
+ conn = Maestro::MQHelper.connect
75
+ sub_done = false
76
+ sub = Maestro::MQHelper.subscribe('queue/test') { |r| sub_done = true }
77
+ max_wait=5
78
+ while !sub_done && max_wait > 0 do
79
+ sleep 0.5
80
+ max_wait -= 1
81
+ end
82
+ sub.should_not be_nil
83
+
84
+ # This requires some internals info, which I'm reluctant to put here
85
+ conn.subscriptions.length.should == 1
86
+
87
+ unsub_done = false
88
+ Maestro::MQHelper.unsubscribe('queue/test') { |r| unsub_done = true }
89
+ max_wait=5
90
+ while !unsub_done && max_wait > 0 do
91
+ sleep 0.5
92
+ max_wait -= 1
93
+ end
94
+
95
+ # This requires some internals info, which I'm reluctant to put here
96
+ conn.subscriptions.length.should == 0
97
+ end
98
+
99
+ end
100
+ end
data/spec/spec_helper.rb CHANGED
@@ -3,11 +3,49 @@ require 'rspec'
3
3
  require 'logger'
4
4
 
5
5
  $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib') unless $LOAD_PATH.include?(File.dirname(__FILE__) + '/../lib')
6
+ unless defined?(SPEC_ROOT)
7
+ SPEC_ROOT = File.expand_path("../", __FILE__)
8
+ end
6
9
 
7
10
  require File.join('maestro_common', 'common')
11
+ Dir[File.join(SPEC_ROOT, 'support/**/*.rb')].each { |f| require(f) }
12
+ include Maestro::Support::Stomp
13
+
14
+ module Maestro
15
+
16
+ unless Maestro.const_defined?('Logging')
17
+
18
+ module Logging
19
+ require 'logging'
20
+
21
+ def log
22
+ ::Logging::Logger.new(STDOUT)
23
+ end
24
+
25
+ end
26
+ end
27
+
28
+ class << self
29
+ include Maestro::Logging unless Maestro.include?(Maestro::Logging)
30
+
31
+ def stomp_config
32
+ {
33
+ 'url' => 'stomp://127.0.0.1:61613'
34
+ }
35
+ end
36
+ end
37
+ end
8
38
 
9
39
  RSpec.configure do |config|
40
+ config.mock_with :mocha
41
+
42
+ config.before(:suite) do
43
+ start_stomp_server(File.join(File.dirname(__FILE__), "../log")) unless ENV['START_STOMP']=='false'
44
+ end
10
45
 
46
+ config.after(:suite) do
47
+ stop_stomp_server
48
+ end
11
49
  end
12
50
 
13
51
 
@@ -0,0 +1,56 @@
1
+ require 'pathname'
2
+
3
+ module Maestro
4
+ module Support
5
+ module Stomp
6
+ @@stomp = nil
7
+ @@pidfile = nil
8
+
9
+ def start_stomp_server(dir)
10
+ # No Stomp auth is configured/required.
11
+ require 'stomp_server_ng'
12
+
13
+ # just because Maestro.log_dir is not defined this early
14
+ FileUtils.mkdir_p(dir)
15
+ log_dir = Pathname.new(dir).realpath.to_s
16
+ puts "Log dir: #{log_dir}"
17
+
18
+ puts "Starting StompServer"
19
+
20
+ test = Thread.new do
21
+ EventMachine.run do
22
+ opts = StompServer::Configurator.new.opts # get default config options
23
+ opts[:port] = 61613
24
+ # Get the logs and pid file in the same log dir as Maestro
25
+ opts[:logdir] = log_dir
26
+ opts[:logfile] = File.join(opts[:logdir], File.basename(opts[:logfile]))
27
+ @@pidfile = File.join(opts[:logdir], File.basename(opts[:pidfile]))
28
+ opts[:pidfile] = @@pidfile
29
+ @@stomp = StompServer::Run.new(opts)
30
+ @@stomp.start
31
+ puts "StompServer running at port: #{opts[:port]}"
32
+ EventMachine.start_server(
33
+ opts[:host],
34
+ opts[:port],
35
+ StompServer::Protocols::Stomp,
36
+ @@stomp.auth_required, # *args: arg[0]
37
+ @@stomp.queue_manager, # *args: arg[1]
38
+ @@stomp.topic_manager, # *args: arg[2]
39
+ @@stomp.stompauth, # *args: arg[3]
40
+ opts # Options hash
41
+ )
42
+ end
43
+ end
44
+ end
45
+
46
+ def stop_stomp_server
47
+ puts "Stopping STOMP server"
48
+ if @@stomp
49
+ @@stomp.stop(@@pidfile)
50
+ puts "Stopped STOMP server"
51
+ end
52
+ end
53
+
54
+ end
55
+ end
56
+ end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: maestro_common
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.4
5
+ version: 0.0.5
6
6
  platform: ruby
7
7
  authors:
8
8
  - Doug Henderson
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2013-08-21 00:00:00 Z
13
+ date: 2013-09-17 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: onstomp
@@ -42,6 +42,9 @@ dependencies:
42
42
  - - ">="
43
43
  - !ruby/object:Gem::Version
44
44
  version: 0.9.8
45
+ - - <
46
+ - !ruby/object:Gem::Version
47
+ version: 1.0.0
45
48
  requirement: *id003
46
49
  prerelease: false
47
50
  type: :runtime
@@ -111,6 +114,28 @@ dependencies:
111
114
  requirement: *id009
112
115
  prerelease: false
113
116
  type: :development
117
+ - !ruby/object:Gem::Dependency
118
+ name: stompserver_ng
119
+ version_requirements: &id010 !ruby/object:Gem::Requirement
120
+ none: false
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: "0"
125
+ requirement: *id010
126
+ prerelease: false
127
+ type: :development
128
+ - !ruby/object:Gem::Dependency
129
+ name: mocha
130
+ version_requirements: &id011 !ruby/object:Gem::Requirement
131
+ none: false
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ version: 0.10.0
136
+ requirement: *id011
137
+ prerelease: false
138
+ type: :development
114
139
  description: A bunch of utility classes that are used in multiple places
115
140
  email:
116
141
  - dhenderson@maestrodev.com
@@ -133,8 +158,10 @@ files:
133
158
  - lib/maestro_common/utils/retryable.rb
134
159
  - lib/maestro_common/version.rb
135
160
  - maestro_common.gemspec
161
+ - spec/mqhelper_spec.rb
136
162
  - spec/retryable_spec.rb
137
163
  - spec/spec_helper.rb
164
+ - spec/support/stomp.rb
138
165
  homepage: https://github.com/maestrodev/maestro-ruby-common
139
166
  licenses:
140
167
  - Apache 2.0
@@ -169,5 +196,7 @@ signing_key:
169
196
  specification_version: 3
170
197
  summary: Maestro common classes
171
198
  test_files:
199
+ - spec/mqhelper_spec.rb
172
200
  - spec/retryable_spec.rb
173
201
  - spec/spec_helper.rb
202
+ - spec/support/stomp.rb