diff options
| author | Carl Hörberg <carl.hoerberg@gmail.com> | 2013-11-30 15:31:00 +0700 |
|---|---|---|
| committer | Carl Hörberg <carl.hoerberg@gmail.com> | 2013-11-30 15:37:10 +0700 |
| commit | aabd191dfa0342d7fb873c39b4cd97d40b81a9a2 (patch) | |
| tree | 5502425fbe850b18b09ae2fd0eb750854b728ad3 | |
| parent | af447195ba1308b454a30caa4ed58b6f8dd6e627 (diff) | |
| download | net-ssh-aabd191dfa0342d7fb873c39b4cd97d40b81a9a2.tar.gz | |
Tests for close conn on exception
| -rw-r--r-- | test/start/test_connection.rb | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/test/start/test_connection.rb b/test/start/test_connection.rb new file mode 100644 index 0000000..94b68c4 --- /dev/null +++ b/test/start/test_connection.rb @@ -0,0 +1,44 @@ +require 'common' +require 'net/ssh' + +module NetSSH + class TestConnection < Test::Unit::TestCase + attr_reader :connection_session + + def setup + authentication_session = mock('authentication_session') + authentication_session.stubs(:authenticate).returns(true) + Net::SSH::Authentication::Session.stubs(:new).returns(authentication_session) + Net::SSH::Transport::Session.stubs(:new).returns(mock('transport_session')) + @connection_session = mock('connection_session') + Net::SSH::Connection::Session.expects(:new => connection_session) + end + + def test_close_connection_on_exception + @connection_session.expects(:closed?).returns(false) + @connection_session.expects(:close).once + + begin + Net::SSH.start('localhost', 'testuser') { raise "error" } + rescue RuntimeError + # We aren't interested in the exception + end + end + + def test_close_connection_on_exception_only_if_still_open + conn_open = states('conn').starts_as(true) + @connection_session.expects(:close).then(conn_open.is(false)).once + @connection_session.expects(:closed?).when(conn_open.is(false)).returns(true) + + begin + Net::SSH.start('localhost', 'testuser') do |ssh| + ssh.close + raise "error" + end + rescue RuntimeError + # We aren't interested in the exception + end + end + end +end + |
