From: Ethan Rowe <[email protected]>

The subscription queue class will be derived from the
subscription class (Puppet::Events::Subscription) and
provide a basic subscription object for queued events;
events will be placed on an internal queue as they are
received by the subscription, and some consumer can
process those events serially at a time of the consumer's
choosing.

The tests are accompanied by some very minimal
implementation as well.
---
 lib/puppet/events/subscription/queue.rb |   17 +++++++
 spec/unit/events/subscription/queue.rb  |   72 +++++++++++++++++++++++++++++++
 2 files changed, 89 insertions(+), 0 deletions(-)
 create mode 100644 lib/puppet/events/subscription/queue.rb
 create mode 100644 spec/unit/events/subscription/queue.rb

diff --git a/lib/puppet/events/subscription/queue.rb 
b/lib/puppet/events/subscription/queue.rb
new file mode 100644
index 0000000..af4b534
--- /dev/null
+++ b/lib/puppet/events/subscription/queue.rb
@@ -0,0 +1,17 @@
+require 'puppet/events/subscription'
+
+class Puppet::Events::Subscription::Queue < Puppet::Events::Subscription
+    def events
+        @events ||= []
+    end
+
+    def has_events?
+        events.size > 0
+    end
+
+    def handle_event(event)
+    end
+
+    def process_events
+    end
+end
diff --git a/spec/unit/events/subscription/queue.rb 
b/spec/unit/events/subscription/queue.rb
new file mode 100644
index 0000000..8d2a2b5
--- /dev/null
+++ b/spec/unit/events/subscription/queue.rb
@@ -0,0 +1,72 @@
+#!/usr/bin/env ruby
+
+require File.dirname(__FILE__) + '/../../../spec_helper'
+require 'puppet/events/subscription/queue'
+
+describe Puppet::Events::Subscription::Queue do
+    before do
+        @class = Puppet::Events::Subscription::Queue
+        @subscription = @class.new
+    end
+
+    it 'should be derived from Puppet::Events::Subscription' do
+        @subscription.kind_of?(Puppet::Events::Subscription).should be_true
+    end
+
+    it 'should have an events array for queuing events' do
+        @subscription.events.should == []
+    end
+
+    describe 'handle_event' do
+        before do
+            @event = stub 'event', :name => :some_event
+        end
+
+        it 'should place the event on the events queue' do
+            @subscription.handle_event(@event)
+            @subscription.events.should == [...@event]
+        end
+    end
+
+    describe 'has_events?' do
+        it 'should return false when the events queue is empty' do
+            @subscription.stubs(:events).returns([])
+            @subscription.has_events?.should be_false
+        end
+
+        it 'should return true when the events queue contains something' do
+            @subscription.stubs(:events).returns([:blah])
+            @subscription.has_events?.should be_true
+        end
+    end
+
+    describe 'process_events' do
+        before do
+            @events = [:a, :b, :c, :d, :e]
+            @internal_events = @events.clone
+            @subscription.stubs(:events).returns(@internal_events)
+        end
+
+        it 'should invoke callback block per queued event in order' do
+            @found = []
+            @subscription.callback = Proc.new {|event| @found << event}
+            @subscription.process_events
+            @found.should == @events
+        end
+
+        it 'should pop the events off the stack along the way' do
+            @sizes = []
+            @subscription.callback = Proc.new {|event| @size << 
@subscription.events.size}
+            @subscription.process_events
+            @sizes.should == ([email protected]).to_a.reverse
+        end
+
+        it 'should use the locally-provided block when given' do
+            call_check = mock 'call_check'
+            call_check.expects(:never).never
+            call_check.expects(:call_me).times(@events.size)
+            @subscription.callback = Proc.new {|e| call_check.never}
+            @subscription.process_events {|e| call_check.call_me}
+        end
+    end
+end
-- 
1.6.3.3


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Puppet Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/puppet-dev?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to