Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'rspec/given'
- require 'securerandom'
- require './src/subscriber_base_class'
- require_relative 'helpers/connection'
- def install_test_subscriber(parent)
- waiter = Thread.new { sleep 1 while true }
- TestSubscriber.callback do |topic, data|
- @topic = topic
- @data = data
- waiter.kill
- end
- waiter
- end
- describe 'TestSubscriber' do
- Given(:subscriber) { SubscriberBaseClass }
- Given { subscriber.load_subscriber('./src/subscribers/test_subscriber') }
- Then do
- handlers = subscriber.subscribers(TestSubscriber::Topic)
- module_present = handlers.find { |h| h[:subscriber] == TestSubscriber }
- module_present.should be_true
- end
- context 'Running Service' do
- Given(:connection_opts) { amqp_connection_options }
- Given(:service) { SubscriberBaseClass.new(connection_opts) }
- context 'Handler Register' do
- Given { install_test_subscriber(subscriber) }
- context 'Check registered handlers' do
- When(:handlers) do
- subscriber.subscribers(TestSubscriber::Topic).map { |h| h[:handler]}
- end
- Then { handlers.each { |h| h.should be_kind_of Proc } }
- Then do
- arg1 = TestSubscriber::Topic
- handlers.each do |h|
- arg2 = SecureRandom.hex
- # simualate a handler call
- h.call(arg1, arg2)
- @topic.should == arg1
- @data.should == arg2
- end
- end
- end
- end
- context 'Recieve messages' do
- When { service.start }
- context 'Recieve a message' do
- Given(:ping_opts) { amqp_ping_options }
- When do
- @data = @topic = nil
- waiter = install_test_subscriber(SubscriberBaseClass)
- publish_amqp_topic(connection_opts,
- ping_opts[:message],
- 'test.topic')
- waiter.join
- end
- Then do
- @data.should == ping_opts[:message]
- @topic.should == TestSubscriber::Topic
- end
- end
- context 'Recieve a message on a wildcard key' do
- Given(:wildcard_opts) { amqp_wildcard_options }
- When do
- @data = @topic = nil
- waiter = install_test_subscriber(SubscriberBaseClass)
- publish_amqp_topic(connection_opts,
- wildcard_opts[:message],
- wildcard_opts[:topic])
- waiter.join
- end
- Then { @data.should == wildcard_opts[:message] }
- And { @topic.should == wildcard_opts[:topic] }
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment