Permalink
Browse files

added capability to close device/interaction to free portmidi resources

  • Loading branch information...
Thomas Jachmann
Thomas Jachmann committed Nov 13, 2009
1 parent 807f2ad commit 492bd7072a20db9736a35cbf66ff0ee69c5b16df
Showing with 60 additions and 0 deletions.
  1. +8 −0 lib/launchpad/device.rb
  2. +4 −0 lib/launchpad/interaction.rb
  3. +30 −0 test/test_device.rb
  4. +18 −0 test/test_interaction.rb
View
@@ -47,6 +47,14 @@ def initialize(opts = nil)
end
end
+ # Closes the device - nothing can be done with the device afterwards
+ def close
+ @input.close unless @input.nil?
+ @input = nil
+ @output.close unless @output.nil?
+ @output = nil
+ end
+
# Resets the launchpad - all settings are reset and all LEDs are switched off
def reset
output(Status::CC, Status::NIL, Status::NIL)
@@ -31,6 +31,10 @@ def start
raise CommunicationError.new(e)
end
+ def close
+ @device.close
+ end
+
# Stops interacting with the launchpad
def stop
@active = false
View
@@ -113,6 +113,36 @@ def stub_input(device, *args)
end
+ context 'close' do
+
+ should 'not fail when neither input nor output are there' do
+ Launchpad::Device.new(:input => false, :output => false).close
+ end
+
+ context 'with input and output devices' do
+
+ setup do
+ Portmidi::Input.stubs(:new).returns(@input = mock('input'))
+ Portmidi::Output.stubs(:new).returns(@output = mock('output', :write => nil))
+ @device = Launchpad::Device.new
+ end
+
+ should 'close input/output and raise NoInputAllowedError/NoOutputAllowedError on subsequent read/write accesses' do
+ @input.expects(:close)
+ @output.expects(:close)
+ @device.close
+ assert_raise Launchpad::NoInputAllowedError do
+ @device.read_pending_actions
+ end
+ assert_raise Launchpad::NoOutputAllowedError do
+ @device.change(:session)
+ end
+ end
+
+ end
+
+ end
+
{
:reset => [0xB0, 0x00, 0x00],
:flashing_on => [0xB0, 0x00, 0x20],
View
@@ -26,6 +26,24 @@ class TestInteraction < Test::Unit::TestCase
end
+ context 'close' do
+
+ should 'close device' do
+ interaction = Launchpad::Interaction.new(:device => device = Launchpad::Device.new)
+ device.expects(:close)
+ interaction.close
+ end
+
+ should 'craise NoInputAllowedError on subsequent accesses' do
+ interaction = Launchpad::Interaction.new(:device => device = Launchpad::Device.new)
+ interaction.close
+ assert_raise Launchpad::NoInputAllowedError do
+ interaction.start
+ end
+ end
+
+ end
+
context 'start' do
# this is kinda greybox tested, since I couldn't come up with another way to test a loop [thomas, 2009-11-11]

0 comments on commit 492bd70

Please sign in to comment.