bunnish 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.3
1
+ 0.0.4
data/bunnish.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "bunnish"
8
- s.version = "0.0.3"
8
+ s.version = "0.0.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Kenji Hara"]
12
- s.date = "2012-12-08"
12
+ s.date = "2012-12-09"
13
13
  s.description = "Command for AMQP access to Message Queue."
14
14
  s.email = "haracane@gmail.com"
15
15
  s.executables = ["bunnish"]
@@ -35,6 +35,10 @@ Gem::Specification.new do |s|
35
35
  "lib/bunnish/command/publish.rb",
36
36
  "lib/bunnish/command/status.rb",
37
37
  "lib/bunnish/command/subscribe.rb",
38
+ "lib/bunnish/core.rb",
39
+ "lib/bunnish/core/common.rb",
40
+ "lib/bunnish/core/publish.rb",
41
+ "lib/bunnish/core/subscribe.rb",
38
42
  "spec/bin/bunnish_spec.rb",
39
43
  "spec/lib/bunnish/command/count_spec.rb",
40
44
  "spec/lib/bunnish/command/delete_spec.rb",
@@ -1,6 +1,6 @@
1
1
  module Bunnish::Command
2
2
  module Count
3
- def self.run(argv, input_stream=$stdin, output_stream=$stdout, error_stream=$stderr)
3
+ def self.run(argv, input_stream=$stdin, output_stream=$stdout)
4
4
 
5
5
  params = Bunnish.parse_opts(argv)
6
6
 
@@ -16,6 +16,8 @@ module Bunnish::Command
16
16
  user = params[:user]
17
17
  password = params[:password]
18
18
  durable = params[:durable]
19
+
20
+ log_label = params[:log_label]
19
21
 
20
22
  queue_name_list = argv.shift
21
23
 
@@ -34,16 +36,16 @@ module Bunnish::Command
34
36
  queue = bunny.queue(queue_name, :durable=>durable)
35
37
 
36
38
  if queue.nil? then
37
- error_stream.puts Time.now.strftime("[%Y-%m-%d %H:%M:%S](INFO) #{queue_name} does not exist")
39
+ Bunnish.logger.info "#{log_label} #{queue_name} does not exist"
38
40
  next
39
41
  end
40
42
 
41
43
  result = queue.delete
42
44
 
43
45
  if result == :delete_ok then
44
- error_stream.puts Time.now.strftime("[%Y-%m-%d %H:%M:%S](INFO) deleted #{queue_name}")
46
+ Bunnish.logger.info "#{log_label} deleted #{queue_name}"
45
47
  else
46
- error_stream.puts Time.now.strftime("[%Y-%m-%d %H:%M:%S](ERROR) failed to #{queue_name}")
48
+ Bunnish.logger.warn "#{log_label} failed to delete #{queue_name}"
47
49
  exit_code = 1
48
50
  end
49
51
  end
@@ -1,11 +1,22 @@
1
1
  module Bunnish::Command
2
2
  module Help
3
- def self.run(argv, input_stream=$stdin, output_stream=$stdout, error_stream=$stderr)
4
- output_stream.puts <<EOF
5
- usage: bunnish COMMAND OPTIONS
6
- EOF
7
-
3
+ def self.run(argv, input_stream=$stdin, output_stream=$stdout)
4
+ output_stream.puts <<-EOF
5
+ usage: bunnish COMMAND [-h HOST] [-p PORT] [-u USER] [-P PASSWORD]
6
+ [--durable FLAG] [--ack FLAG] [--raise-exception]
7
+ [--log-label LABEL] [--log-dir DIR] [--log-file FILE]
8
+ -h HOST message queue server address. default is localhost.
9
+ -p PORT port number. default is 5672.
10
+ -u USER user name. default is 'guest'.
11
+ -P PASSWORD password. default is 'guest'.
12
+ --durable FLAG FLAG=t:disk queue; FLAG=f:memory queue(default).
13
+ --ack FLAG FLAG=t:enable ack(default); FLAG=t:disable ack.
14
+ --raise-exception raise exception.
15
+ --log-label LABEL set log label.
16
+ --log-dir DIR set log directory.
17
+ --log-file FILE set log file.
18
+ EOF
8
19
  return 0
9
20
  end
10
21
  end
11
- end
22
+ end
@@ -1,27 +1,6 @@
1
1
  module Bunnish::Command
2
2
  module Publish
3
- def self.output_log(streams, message)
4
- streams.each do |stream|
5
- if stream then
6
- stream.puts message
7
- stream.flush
8
- end
9
- end
10
- end
11
-
12
- def self.output_publish_log(streams, queue, count, log_label)
13
- message_count = '?'
14
- consumer_count = '?'
15
- begin
16
- message_count = queue.status[:message_count]
17
- consumer_count = queue.status[:consumer_count]
18
- rescue Exception=>e
19
- end
20
-
21
- self.output_log streams, Time.now.strftime("[%Y-%m-%d %H:%M:%S](INFO)#{log_label} published #{count} messages into #{queue.name}(#{message_count} messages, #{consumer_count} consumers)")
22
- end
23
-
24
- def self.run(argv, input_stream=$stdin, output_stream=$stdout, error_stream=$stderr)
3
+ def self.run(argv, input_stream=$stdin, output_stream=$stdout)
25
4
 
26
5
  params = Bunnish.parse_opts(argv)
27
6
 
@@ -55,60 +34,44 @@ module Bunnish::Command
55
34
  if log_path then
56
35
  log_stream = open(log_path, "a")
57
36
  log_streams[queue_name] = log_stream
58
- error_stream.puts Time.now.strftime("[%Y-%m-%d %H:%M:%S](INFO)#{log_label} output log into #{log_path}")
37
+ Bunnish.logger.info "#{log_label} output log into #{log_path}"
59
38
  end
60
39
  end
61
40
 
62
41
  fanout_flag = (exchange_name && exchange_name != '' && 1 < queue_name_list.size)
63
42
 
64
43
  bunny = nil
65
- direct_exchange = nil
66
44
 
67
45
  publish_flag = false
68
46
 
47
+ exchange_list = []
48
+
69
49
  begin
70
- direct_exchange_list = nil
71
-
72
50
  # publish message to exchange
73
51
  count = 0
74
52
 
75
53
  lines = []
76
54
 
77
-
78
55
  while line = input_stream.gets do
79
56
  if bunny == nil then
80
57
  bunny = Bunny.new(:logging => false, :spec => '09', :host=>host, :port=>port, :user=>user, :pass=>password)
81
-
82
58
  # start a communication session with the amqp server
83
59
  bunny.start
84
-
85
60
  # create/get exchange
86
61
  if fanout_flag then
87
- error_stream.puts Time.now.strftime("[%Y-%m-%d %H:%M:%S]") + "(INFO)#{log_label} create fanout exchange '#{exchange_name}'"
88
- fanout_exchange = bunny.exchange(exchange_name, :type=>:fanout, :persistent=>durable)
89
- queue_name_list.each do |queue_name|
90
- # create/get queue
91
- error_stream.puts Time.now.strftime("[%Y-%m-%d %H:%M:%S]") + "(INFO)#{log_label} create queue '#{queue_name}'"
92
- queue = bunny.queue(queue_name, :durable=>durable)
93
- error_stream.puts Time.now.strftime("[%Y-%m-%d %H:%M:%S]") + "(INFO)#{log_label} bind queue '#{queue_name}' to fanout exchange '#{exchange_name}'"
94
- queue.bind(fanout_exchange)
95
- self.output_log [error_stream, log_streams[queue_name]], Time.now.strftime("[%Y-%m-%d %H:%M:%S](INFO)#{log_label} publish to #{queue_name}(#{queue.status[:message_count]} messages, #{queue.status[:consumer_count]} consumers)")
96
- end
62
+ fanout_exchange = Bunnish::Core::Publish.create_fanout_exchange(bunny, queue_name_list, log_streams, params)
63
+ exchange_list.push fanout_exchange
97
64
  else
98
- direct_exchange_list = queue_name_list.map { |queue_name|
99
-
100
- error_stream.puts Time.now.strftime("[%Y-%m-%d %H:%M:%S]") + "(INFO)#{log_label} create direct exchange '#{queue_name}'"
101
- direct_exchange = bunny.exchange(queue_name, :type=>:direct)
102
-
103
- error_stream.puts Time.now.strftime("[%Y-%m-%d %H:%M:%S]") + "(INFO)#{log_label} create queue '#{queue_name}'"
104
- queue = bunny.queue(queue_name, :durable=>durable)
105
-
106
- error_stream.puts Time.now.strftime("[%Y-%m-%d %H:%M:%S]") + "(INFO)#{log_label} bind queue '#{queue_name}' to direct exchange '#{queue_name}'"
107
- queue.bind(direct_exchange)
108
- self.output_log [error_stream, log_streams[queue_name]], Time.now.strftime("[%Y-%m-%d %H:%M:%S](INFO)#{log_label} publish to #{queue_name}(#{queue.status[:message_count]} messages, #{queue.status[:consumer_count]} consumers)")
109
-
110
- direct_exchange
65
+ direct_exchange_list = queue_name_list.map {|queue_name|
66
+ Bunnish::Core::Publish.create_direct_exchange(bunny, queue_name, log_streams, params)
111
67
  }
68
+ exchange_list = direct_exchange_list
69
+ end
70
+
71
+ queue_name_list.each do |queue_name|
72
+ queue = bunny.queue(queue_name, :durable=>durable)
73
+ message = "#{log_label} publish to #{queue_name}(#{queue.status[:message_count]} messages, #{queue.status[:consumer_count]} consumers)"
74
+ Bunnish::Core::Common.output_log [log_streams[queue_name]], "INFO", message
112
75
  end
113
76
  end
114
77
 
@@ -121,13 +84,10 @@ module Bunnish::Command
121
84
  message = line
122
85
  end
123
86
 
124
- if fanout_flag then
125
- fanout_exchange.publish(message)
126
- else
127
- direct_exchange_list.each do |direct_exchange|
128
- direct_exchange.publish(message)
129
- end
87
+ exchange_list.each do |exchange|
88
+ exchange.publish(message)
130
89
  end
90
+
131
91
  count += 1
132
92
 
133
93
  if unit_size <= count then
@@ -135,7 +95,7 @@ module Bunnish::Command
135
95
  queue_name_list.each do |queue_name|
136
96
  queue = bunny.queue(queue_name, :durable=>durable)
137
97
  log_stream = log_streams[queue_name]
138
- self.output_publish_log [error_stream, log_stream], queue, count, log_label
98
+ Bunnish::Core::Publish.output_publish_log [log_stream], queue, count, log_label
139
99
  end
140
100
  count = 0
141
101
  end
@@ -147,9 +107,9 @@ module Bunnish::Command
147
107
  log_stream = log_streams[queue_name]
148
108
  if bunny then
149
109
  queue = bunny.queue(queue_name, :durable=>durable)
150
- self.output_publish_log [error_stream, log_stream], queue, count, log_label if 0 < count || !publish_flag
110
+ Bunnish::Core::Publish.output_publish_log [log_stream], queue, count, log_label if 0 < count || !publish_flag
151
111
  else
152
- self.output_log [error_stream, log_stream], Time.now.strftime("[%Y-%m-%d %H:%M:%S](INFO)#{log_label} no input for #{queue_name}")
112
+ Bunnish::Core::Common.output_log [log_stream], "INFO", "#{log_label} no input for #{queue_name}"
153
113
  end
154
114
  end
155
115
 
@@ -160,8 +120,8 @@ module Bunnish::Command
160
120
  bunny.stop if bunny
161
121
  raise e if raise_exception_flag
162
122
  else
163
- message = Time.now.strftime("[%Y-%m-%d %H:%M:%S](EXCEPTION)#{log_label} #{e.message}(#{e.class.name}): #{e.backtrace.map{|s| " #{s}"}.join("\n")}")
164
- self.output_log(([error_stream] + log_streams.values), message)
123
+ message = "#{log_label} #{e.message}(#{e.class.name}): #{e.backtrace.map{|s| " #{s}"}.join("\n")}"
124
+ Bunnish::Core::Common.output_log(log_streams.values, "EXCEPTION", message)
165
125
  end
166
126
  end
167
127
 
@@ -1,6 +1,6 @@
1
1
  module Bunnish::Command
2
2
  module Status
3
- def self.run(argv, input_stream=$stdin, output_stream=$stdout, error_stream=$stderr)
3
+ def self.run(argv, input_stream=$stdin, output_stream=$stdout)
4
4
 
5
5
  params = Bunnish.parse_opts(argv)
6
6
 
@@ -1,27 +1,6 @@
1
1
  module Bunnish::Command
2
2
  module Subscribe
3
- def self.output_log(streams, message)
4
- streams.each do |stream|
5
- if stream then
6
- stream.puts message
7
- stream.flush
8
- end
9
- end
10
- end
11
-
12
- def self.output_subscribe_log(streams, queue, count, log_label)
13
- message_count = '?'
14
- consumer_count = '?'
15
- begin
16
- message_count = queue.status[:message_count]
17
- consumer_count = queue.status[:consumer_count]
18
- rescue Exception=>e
19
- end
20
-
21
- self.output_log streams, Time.now.strftime("[%Y-%m-%d %H:%M:%S](INFO)#{log_label} subscribed #{count} messages from #{queue.name}(#{message_count} messages, #{consumer_count} consumers)")
22
- end
23
-
24
- def self.run(argv, input_stream=$stdin, output_stream=$stdout, error_stream=$stderr)
3
+ def self.run(argv, input_stream=$stdin, output_stream=$stdout)
25
4
  params = Bunnish.parse_opts(argv)
26
5
 
27
6
  host = params[:host]
@@ -51,7 +30,7 @@ module Bunnish::Command
51
30
 
52
31
  if log_path
53
32
  log_stream = open(log_path, "a")
54
- error_stream.puts Time.now.strftime("[%Y-%m-%d %H:%M:%S](INFO)#{log_label} output log into #{log_path}")
33
+ Bunnish.logger.info "#{log_label} output log into #{log_path}"
55
34
  end
56
35
 
57
36
  exchange_name = queue_name
@@ -79,15 +58,15 @@ module Bunnish::Command
79
58
  end
80
59
 
81
60
  if message_max
82
- self.output_log [error_stream, log_stream], Time.now.strftime("[%Y-%m-%d %H:%M:%S](INFO)#{log_label} subscribe #{message_max} messages from #{queue_name}(#{remain_count} messages, #{consumer_count} consumers)")
61
+ Bunnish::Core::Common.output_log [log_stream], "INFO", "#{log_label} subscribe #{message_max} messages from #{queue_name}(#{remain_count} messages, #{consumer_count} consumers)"
83
62
 
84
63
  if message_max <= 0
85
- self.output_log [error_stream, log_stream], Time.now.strftime("[%Y-%m-%d %H:%M:%S](INFO)#{log_label} finished")
64
+ Bunnish::Core::Common.output_log [log_stream], "INFO", "#{log_label} finished"
86
65
  bunny.stop
87
66
  return 0
88
67
  end
89
68
  else
90
- self.output_log [error_stream, log_stream], Time.now.strftime("[%Y-%m-%d %H:%M:%S](INFO)#{log_label} subscribe from #{queue_name}(#{remain_count} messages, #{consumer_count} consumers)")
69
+ Bunnish::Core::Common.output_log [log_stream], "INFO", "#{log_label} subscribe from #{queue_name}(#{remain_count} messages, #{consumer_count} consumers)"
91
70
  end
92
71
 
93
72
  if !exchange_name.nil? && exchange_name != '' then
@@ -101,7 +80,7 @@ module Bunnish::Command
101
80
  subscribe_flag = false
102
81
 
103
82
  if remain_count == 0 then
104
- self.output_log [error_stream, log_stream], Time.now.strftime("[%Y-%m-%d %H:%M:%S](INFO)#{log_label} no messages in #{queue_name}(#{remain_count} messages, #{consumer_count} consumers)")
83
+ Bunnish::Core::Common.output_log [log_stream], "INFO", "#{log_label} no messages in #{queue_name}(#{remain_count} messages, #{consumer_count} consumers)"
105
84
  else
106
85
  # subscribe to queue
107
86
  begin
@@ -116,7 +95,7 @@ module Bunnish::Command
116
95
  total_count += 1
117
96
  if unit_size <= count then
118
97
  subscribe_flag = true
119
- self.output_log [error_stream, log_stream], Time.now.strftime("[%Y-%m-%d %H:%M:%S](INFO)#{log_label} subscribed #{count} messages from #{queue_name}")
98
+ Bunnish::Core::Common.output_log [log_stream], "INFO", "#{log_label} subscribed #{count} messages from #{queue_name}"
120
99
  count = 0
121
100
  break if min_size && remain_count <= total_count + min_size
122
101
  end
@@ -127,14 +106,14 @@ module Bunnish::Command
127
106
  bunny.stop if bunny
128
107
  raise e if raise_exception_flag
129
108
  else
130
- self.output_log [error_stream, log_stream], Time.now.strftime("[%Y-%m-%d %H:%M:%S](EXCEPTION)#{log_label} #{e.message}(#{e.class.name}): #{e.backtrace.map{|s| " #{s}"}.join("\n")}")
109
+ Bunnish::Core::Common.output_log [log_stream], "EXCEPTION", "#{log_label} #{e.message}(#{e.class.name}): #{e.backtrace.map{|s| " #{s}"}.join("\n")}"
131
110
  end
132
111
  end
133
112
  end
134
113
 
135
114
  subscribe_flag = true if 0 < count
136
115
 
137
- self.output_subscribe_log [error_stream, log_stream], queue, count, log_label # if 0 < count || subscribe_flag
116
+ Bunnish::Core::Subscribe.output_subscribe_log [log_stream], queue, count, log_label # if 0 < count || subscribe_flag
138
117
 
139
118
  if log_stream then
140
119
  log_stream.close
@@ -0,0 +1,19 @@
1
+ module Bunnish::Core
2
+ module Common
3
+ def self.output_log(streams, log_level, message)
4
+ case log_level
5
+ when "INFO"
6
+ Bunnish.logger.info(message)
7
+ when "EXCEPTION"
8
+ Bunnish.logger.warn(message)
9
+ end
10
+ message = Time.now.strftime("[%Y-%m-%d %H:%M:%S](#{log_level})#{message}")
11
+ streams.each do |stream|
12
+ if stream then
13
+ stream.puts message
14
+ stream.flush
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,50 @@
1
+ module Bunnish::Core
2
+ module Publish
3
+ def self.output_publish_log(streams, queue, count, log_label)
4
+ message_count = '?'
5
+ consumer_count = '?'
6
+ begin
7
+ message_count = queue.status[:message_count]
8
+ consumer_count = queue.status[:consumer_count]
9
+ rescue Exception=>e
10
+ end
11
+
12
+ message = "#{log_label} published #{count} messages into #{queue.name}(#{message_count} messages, #{consumer_count} consumers)"
13
+ Bunnish::Core::Common.output_log(streams, "INFO", message)
14
+ end
15
+
16
+ def self.create_fanout_exchange(bunny, queue_name_list, log_streams={}, options={})
17
+ durable = options[:durable]
18
+ log_label = options[:log_label]
19
+ exchange_name = options[:exchange_name]
20
+
21
+ Bunnish.logger.info "#{log_label} create fanout exchange '#{exchange_name}'"
22
+ fanout_exchange = bunny.exchange(exchange_name, :type=>:fanout, :persistent=>durable)
23
+
24
+ queue_name_list.each do |queue_name|
25
+ # create/get queue
26
+ Bunnish.logger.info "#{log_label} create queue '#{queue_name}'"
27
+ queue = bunny.queue(queue_name, :durable=>durable)
28
+ Bunnish.logger.info "#{log_label} bind queue '#{queue_name}' to fanout exchange '#{exchange_name}'"
29
+ queue.bind(fanout_exchange)
30
+ end
31
+ return fanout_exchange
32
+ end
33
+
34
+ def self.create_direct_exchange(bunny, queue_name, log_streams={}, options={})
35
+ durable = options[:durable]
36
+ log_label = options[:log_label]
37
+
38
+ Bunnish.logger.info "#{log_label} create direct exchange '#{queue_name}'"
39
+ direct_exchange = bunny.exchange(queue_name, :type=>:direct)
40
+
41
+ Bunnish.logger.info "#{log_label} create queue '#{queue_name}'"
42
+ queue = bunny.queue(queue_name, :durable=>durable)
43
+
44
+ Bunnish.logger.info "#{log_label} bind queue '#{queue_name}' to direct exchange '#{queue_name}'"
45
+ queue.bind(direct_exchange)
46
+
47
+ return direct_exchange
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,16 @@
1
+ module Bunnish::Core
2
+ module Subscribe
3
+ def self.output_subscribe_log(streams, queue, count, log_label)
4
+ message_count = '?'
5
+ consumer_count = '?'
6
+ begin
7
+ message_count = queue.status[:message_count]
8
+ consumer_count = queue.status[:consumer_count]
9
+ rescue Exception=>e
10
+ end
11
+
12
+ message = "#{log_label} subscribed #{count} messages from #{queue.name}(#{message_count} messages, #{consumer_count} consumers)"
13
+ Bunnish::Core::Common.output_log(streams, "INFO", message)
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,3 @@
1
+ require "bunnish/core/common"
2
+ require "bunnish/core/publish"
3
+ require "bunnish/core/subscribe"
data/lib/bunnish.rb CHANGED
@@ -111,6 +111,27 @@ module Bunnish
111
111
  }
112
112
 
113
113
  end
114
+
115
+ def self.logger
116
+ @logger ||= (rails_logger || default_logger)
117
+ end
118
+
119
+ def self.rails_logger
120
+ (defined?(Rails) && Rails.respond_to?(:logger) && Rails.logger) ||
121
+ (defined?(RAILS_DEFAULT_LOGGER) && RAILS_DEFAULT_LOGGER.respond_to?(:debug) && RAILS_DEFAULT_LOGGER)
122
+ end
123
+
124
+ def self.default_logger
125
+ require 'logger'
126
+ l = Logger.new(STDERR)
127
+ l.level = Logger::INFO
128
+ l
129
+ end
130
+
131
+ def self.logger=(logger)
132
+ @logger = logger
133
+ end
114
134
  end
115
135
 
116
136
  require "bunnish/command"
137
+ require "bunnish/core"
@@ -2,7 +2,7 @@ require "spec_helper"
2
2
 
3
3
  describe "bin/bunnish" do
4
4
  before :all do
5
- @stderr = Bunnish::REDIRECT[:stderr]
5
+ @stderr_dst = Bunnish::REDIRECT[:stderr]
6
6
  @input_file = Tempfile.new("bunnish")
7
7
  @input_file.puts "foo"
8
8
  @input_file.puts "bar"
@@ -17,18 +17,18 @@ describe "bin/bunnish" do
17
17
  `cat #{@input_file.path} | #{Bunnish::RUBY_CMD} #{Bunnish::BIN_DIR}/bunnish publish bunnish-test-queue 2> /dev/null`
18
18
  end
19
19
  after :each do
20
- `#{Bunnish::RUBY_CMD} #{Bunnish::BIN_DIR}/bunnish delete bunnish-test-queue 2> /dev/null`
20
+ `#{Bunnish::RUBY_CMD} #{Bunnish::BIN_DIR}/bunnish delete bunnish-test-queue,bunnish-test-queue-2 2> /dev/null`
21
21
  end
22
22
  describe "count" do
23
23
  it "should output valid queue count" do
24
- result = `#{Bunnish::RUBY_CMD} #{Bunnish::BIN_DIR}/bunnish count bunnish-test-queue 2> #{@stderr}`
24
+ result = `#{Bunnish::RUBY_CMD} #{Bunnish::BIN_DIR}/bunnish count bunnish-test-queue #{@stderr_dst}`
25
25
  result.chomp!
26
26
  result.should == "3"
27
27
  end
28
28
  end
29
29
  describe "delete" do
30
30
  it "should delete queue" do
31
- `#{Bunnish::RUBY_CMD} #{Bunnish::BIN_DIR}/bunnish delete bunnish-test-queue 2> #{@stderr}`
31
+ `#{Bunnish::RUBY_CMD} #{Bunnish::BIN_DIR}/bunnish delete bunnish-test-queue #{@stderr_dst}`
32
32
  result = `#{Bunnish::RUBY_CMD} #{Bunnish::BIN_DIR}/bunnish count bunnish-test-queue 2> /dev/null`
33
33
  result = result.chomp!
34
34
  result.should == "0"
@@ -36,27 +36,61 @@ describe "bin/bunnish" do
36
36
  end
37
37
  describe "help" do
38
38
  it "should delete queue" do
39
- result = `#{Bunnish::RUBY_CMD} #{Bunnish::BIN_DIR}/bunnish help bunnish-test-queue 2> #{@stderr}`
39
+ result = `#{Bunnish::RUBY_CMD} #{Bunnish::BIN_DIR}/bunnish help bunnish-test-queue #{@stderr_dst}`
40
40
  result.split().size.should > 0
41
41
  $?.should == 0
42
42
  end
43
43
  end
44
44
  describe "publish" do
45
- it "should publish valid messages" do
46
- `echo qux | #{Bunnish::RUBY_CMD} #{Bunnish::BIN_DIR}/bunnish publish bunnish-test-queue 2> #{@stderr}`
47
- result = `#{Bunnish::RUBY_CMD} #{Bunnish::BIN_DIR}/bunnish subscribe bunnish-test-queue 2> /dev/null`
48
- result = result.split()
49
- # result.each do |line| STDERR.puts line end
50
- result.shift.should == "foo"
51
- result.shift.should == "bar"
52
- result.shift.should == "baz"
53
- result.shift.should == "qux"
54
- result.size.should == 0
45
+ context "when exchange-name is not set" do
46
+ it "should publish valid messages with direct exchange" do
47
+ `cat #{@input_file.path} | #{Bunnish::RUBY_CMD} #{Bunnish::BIN_DIR}/bunnish publish bunnish-test-queue,bunnish-test-queue-2 #{@stderr_dst}`
48
+ result = `#{Bunnish::RUBY_CMD} #{Bunnish::BIN_DIR}/bunnish subscribe bunnish-test-queue 2> /dev/null`
49
+ result = result.split()
50
+ # result.each do |line| STDERR.puts line end
51
+ result.shift.should == "foo"
52
+ result.shift.should == "bar"
53
+ result.shift.should == "baz"
54
+ result.shift.should == "foo"
55
+ result.shift.should == "bar"
56
+ result.shift.should == "baz"
57
+ result.size.should == 0
58
+
59
+ result = `#{Bunnish::RUBY_CMD} #{Bunnish::BIN_DIR}/bunnish subscribe bunnish-test-queue-2 2> /dev/null`
60
+ result = result.split()
61
+ result.shift.should == "foo"
62
+ result.shift.should == "bar"
63
+ result.shift.should == "baz"
64
+ result.size.should == 0
65
+ end
66
+ end
67
+
68
+ context "when exchange-name is set" do
69
+ it "should publish valid messages with fanout exchange" do
70
+ `cat #{@input_file.path} | #{Bunnish::RUBY_CMD} #{Bunnish::BIN_DIR}/bunnish publish --exchange-name bunnish-test-exchange bunnish-test-queue,bunnish-test-queue-2 #{@stderr_dst}`
71
+ result = `#{Bunnish::RUBY_CMD} #{Bunnish::BIN_DIR}/bunnish subscribe bunnish-test-queue 2> /dev/null`
72
+ result = result.split()
73
+ # result.each do |line| STDERR.puts line end
74
+ result.shift.should == "foo"
75
+ result.shift.should == "bar"
76
+ result.shift.should == "baz"
77
+ result.shift.should == "foo"
78
+ result.shift.should == "bar"
79
+ result.shift.should == "baz"
80
+ result.size.should == 0
81
+
82
+ result = `#{Bunnish::RUBY_CMD} #{Bunnish::BIN_DIR}/bunnish subscribe bunnish-test-queue-2 2> /dev/null`
83
+ result = result.split()
84
+ result.shift.should == "foo"
85
+ result.shift.should == "bar"
86
+ result.shift.should == "baz"
87
+ result.size.should == 0
88
+ end
55
89
  end
56
90
  end
57
91
  describe "status" do
58
92
  it "should print valid status of message queue" do
59
- result = `#{Bunnish::RUBY_CMD} #{Bunnish::BIN_DIR}/bunnish status bunnish-test-queue 2> #{@stderr}`
93
+ result = `#{Bunnish::RUBY_CMD} #{Bunnish::BIN_DIR}/bunnish status bunnish-test-queue #{@stderr_dst}`
60
94
  result = result.split(/\n/)
61
95
  # STDERR.puts result.inspect
62
96
  result.include?("bunnish-test-queue : 3 messages, 0 consumers").should be_true
@@ -64,7 +98,7 @@ describe "bin/bunnish" do
64
98
  end
65
99
  describe "subscribe" do
66
100
  it "should subscribe valid messages" do
67
- result = `#{Bunnish::RUBY_CMD} #{Bunnish::BIN_DIR}/bunnish subscribe bunnish-test-queue 2> #{@stderr}`
101
+ result = `#{Bunnish::RUBY_CMD} #{Bunnish::BIN_DIR}/bunnish subscribe bunnish-test-queue #{@stderr_dst}`
68
102
  result = result.split()
69
103
  # result.each do |line| STDERR.puts line end
70
104
  result.shift.should == "foo"
data/spec/spec_helper.rb CHANGED
@@ -17,5 +17,14 @@ module Bunnish
17
17
  BIN_DIR = "#{BUNNISH_HOME}/bin"
18
18
  LIB_DIR = "#{BUNNISH_HOME}/lib"
19
19
  RUBY_CMD = "/usr/bin/env ruby -I #{LIB_DIR}"
20
- REDIRECT = {:stderr=>"/dev/null"}
20
+ REDIRECT = {:stderr=>"2> /dev/null"}
21
+ end
22
+
23
+ Bunnish.logger = Logger.new(STDERR)
24
+ if File.exist?('/tmp/bunnish.debug') then
25
+ Bunnish.logger.level = Logger::DEBUG
26
+ Bunnish::REDIRECT[:stderr] = nil
27
+ else
28
+ Bunnish.logger.level = Logger::ERROR
29
+ Bunnish::REDIRECT[:stderr] = "2> /dev/null"
21
30
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bunnish
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 3
10
- version: 0.0.3
9
+ - 4
10
+ version: 0.0.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Kenji Hara
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-12-08 00:00:00 Z
18
+ date: 2012-12-09 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: json
@@ -147,6 +147,10 @@ files:
147
147
  - lib/bunnish/command/publish.rb
148
148
  - lib/bunnish/command/status.rb
149
149
  - lib/bunnish/command/subscribe.rb
150
+ - lib/bunnish/core.rb
151
+ - lib/bunnish/core/common.rb
152
+ - lib/bunnish/core/publish.rb
153
+ - lib/bunnish/core/subscribe.rb
150
154
  - spec/bin/bunnish_spec.rb
151
155
  - spec/lib/bunnish/command/count_spec.rb
152
156
  - spec/lib/bunnish/command/delete_spec.rb