Esempio n. 1
0
    def test_init(self):
        mock(connection, 'super')
        with expect(connection, 'super').args(is_arg(ConnectionChannel), ConnectionChannel).returns(mock()) as s:
            expect(s.__init__).args('a', 'b')

        c = ConnectionChannel('a', 'b')
        assert_equals(c._method_map,
                      {
                          10: c._recv_start,
                          20: c._recv_secure,
                          30: c._recv_tune,
                          41: c._recv_open_ok,
                          50: c._recv_close,
                          51: c._recv_close_ok,
                      }
                      )
        assert_equal(0, c._last_heartbeat_send)
Esempio n. 2
0
 def setUp(self):
   super(ConnectionChannelTest,self).setUp()
   self.connection = mock()
   self.ch = ConnectionChannel(self.connection, 0, {})
Esempio n. 3
0
class ConnectionChannelTest(Chai):
  
  def setUp(self):
    super(ConnectionChannelTest,self).setUp()
    self.connection = mock()
    self.ch = ConnectionChannel(self.connection, 0, {})

  def test_init(self):
    mock( connection, 'super' )
    with expect( connection, 'super' ).args( is_arg(ConnectionChannel), ConnectionChannel ).returns(mock()) as s:
      expect( s.__init__ ).args( 'a', 'b' )

    c = ConnectionChannel('a','b')
    assert_equals( c._method_map ,
      {
        10 : c._recv_start,
        20 : c._recv_secure,
        30 : c._recv_tune,
        41 : c._recv_open_ok,
        50 : c._recv_close,
        51 : c._recv_close_ok,
      }
    )
    assert_equal( 0, c._last_heartbeat_send )

  def test_dispatch_on_heartbeat_frame(self):
    frame = mock()

    expect( frame.type ).at_least(1).returns( HeartbeatFrame.type() )
    expect( self.ch.send_heartbeat )

    self.ch.dispatch( frame )

  def test_dispatch_method_frame_class_10(self):
    frame = mock()
    frame.class_id = 10
    frame.method_id = 10
    self.ch._method_map[10] = mock()

    expect( frame.type ).at_least(1).returns( MethodFrame.type() )
    expect( self.ch._method_map[10] ).args( frame )

    self.ch.dispatch( frame )

  def test_dispatch_method_frame_raises_invalidmethod(self):
    frame = mock()
    frame.class_id = 10
    frame.method_id = 500

    expect( frame.type ).at_least(1).returns( MethodFrame.type() )

    with assert_raises( Channel.InvalidMethod ):
      self.ch.dispatch( frame )

  def test_dispatch_method_frame_raises_invalidclass(self):
    frame = mock()
    frame.class_id = 11
    frame.method_id = 10

    expect( frame.type ).at_least(1).returns( MethodFrame.type() )

    with assert_raises( Channel.InvalidClass ):
      self.ch.dispatch( frame )

  def test_dispatch_method_frame_raises_invalidframetype(self):
    frame = mock()

    expect( frame.type ).at_least(1).returns( HeaderFrame.type() )

    with assert_raises( Frame.InvalidFrameType ):
      self.ch.dispatch( frame )

  def test_close(self):
    expect( self.ch._send_close )
    self.ch.close()

  def test_send_heartbeat_when_no_heartbeat(self):
    stub( self.ch.send_frame )
    self.ch.connection._heartbeat = None

    self.ch.send_heartbeat()

  def test_send_heartbeat_when_not_sent_yet(self):
    mock( connection, 'time' )
    self.ch.connection._heartbeat = 3
    self.ch._last_heartbeat_send = 0

    expect( connection.time.time ).returns( 4200.3 ).times(2)
    expect( self.ch.send_frame ).args( HeartbeatFrame )

    self.ch.send_heartbeat()
    assert_equals( 4200.3, self.ch._last_heartbeat_send )

  def test_send_heartbeat_when_sent_long_ago(self):
    mock( connection, 'time' )
    self.ch.connection._heartbeat = 3
    self.ch._last_heartbeat_send = 4196

    expect( connection.time.time ).returns( 4200.3 ).times(2)
    expect( self.ch.send_frame ).args( HeartbeatFrame )

    self.ch.send_heartbeat()
    assert_equals( 4200.3, self.ch._last_heartbeat_send )

  def test_send_heart_when_sent_recently(self):
    mock( connection, 'time' )
    self.ch.connection._heartbeat = 3
    self.ch._last_heartbeat_send = 4199

    expect( connection.time.time ).returns( 4200.3 )
    stub( self.ch.send_frame )

    self.ch.send_heartbeat()
    assert_equals( 4199, self.ch._last_heartbeat_send )

  def test_recv_start(self):
    expect( self.ch._send_start_ok )
    self.ch.connection._closed = 'maybe'

    self.ch._recv_start('frame')
    assert_false( self.ch.connection._closed )

  def test_send_start_ok(self):
    self.ch.connection._properties = 'props'
    self.ch.connection._login_method = 'please'
    self.ch.connection._login_response = 'thanks'
    self.ch.connection._locale = 'home'
    
    with expect(mock(connection,'Writer')).returns(mock()) as writer:
      expect( writer.write_table ).args( 'props' )
      expect( writer.write_shortstr ).args( 'please' )
      expect( writer.write_longstr ).args( 'thanks' )
      expect( writer.write_shortstr ).args( 'home' )

      expect(mock(connection,'MethodFrame')).args(0,10,11,writer).returns('frame')
      expect( self.ch.send_frame ).args( 'frame' )

    self.ch._send_start_ok()

  def test_recv_tune_when_no_broker_max_and_defined_heartbeat(self):
    self.ch.connection._channel_max = 42
    self.ch.connection._frame_max = 43
    self.ch.connection._heartbeat = 8

    frame = mock()
    expect( frame.args.read_short ).returns(0)
    expect( frame.args.read_long ).returns(0)
    
    expect( self.ch._send_tune_ok )
    expect( self.ch._send_open )
    expect( self.ch.send_heartbeat )

    self.ch._recv_tune(frame)
    assert_equals( 42, self.ch.connection._channel_max )
    assert_equals( 43, self.ch.connection._frame_max )
    assert_equals( 8, self.ch.connection._heartbeat )

  def test_recv_tune_when_broker_max_and_undefined_heartbeat(self):
    self.ch.connection._channel_max = 42
    self.ch.connection._frame_max = 43
    self.ch.connection._heartbeat = None

    frame = mock()
    expect( frame.args.read_short ).returns(500)
    expect( frame.args.read_long ).returns(501)
    expect( frame.args.read_short ).returns(7)
    
    expect( self.ch._send_tune_ok )
    expect( self.ch._send_open )
    expect( self.ch.send_heartbeat )

    self.ch._recv_tune(frame)
    assert_equals( 500, self.ch.connection._channel_max )
    assert_equals( 501, self.ch.connection._frame_max )
    assert_equals( 7, self.ch.connection._heartbeat )

  def test_send_tune_ok_when_heartbeat(self):
    self.ch.connection._channel_max = 42
    self.ch.connection._frame_max = 43
    self.ch.connection._heartbeat = 8
    
    with expect(mock(connection,'Writer')).returns(mock()) as writer:
      expect( writer.write_short ).args( 42 )
      expect( writer.write_long ).args( 43 )
      expect( writer.write_short ).args( 8 )

      expect(mock(connection,'MethodFrame')).args(0,10,31,writer).returns('frame')
      expect( self.ch.send_frame ).args( 'frame' )

    self.ch._send_tune_ok()

  def test_send_tune_ok_when_no_heartbeat(self):
    self.ch.connection._channel_max = 42
    self.ch.connection._frame_max = 43
    self.ch.connection._heartbeat = None
    
    with expect(mock(connection,'Writer')).returns(mock()) as writer:
      expect( writer.write_short ).args( 42 )
      expect( writer.write_long ).args( 43 )
      expect( writer.write_short ).args( 0 )

      expect(mock(connection,'MethodFrame')).args(0,10,31,writer).returns('frame')
      expect( self.ch.send_frame ).args( 'frame' )

    self.ch._send_tune_ok()

  def test_recv_secure(self):
    expect( self.ch._send_open )
    self.ch._recv_secure('frame')

  def test_send_open(self):
    self.connection._vhost = '/foo'
    
    with expect(mock(connection,'Writer')).returns(mock()) as writer:
      expect( writer.write_shortstr ).args( '/foo' )
      expect( writer.write_shortstr ).args( '' )
      expect( writer.write_bit ).args( True )

      expect(mock(connection,'MethodFrame')).args(0,10,40,writer).returns('frame')
      expect( self.ch.send_frame ).args( 'frame' )

    self.ch._send_open()

  def test_recv_open_ok(self):
    self.ch.connection._connected = False
    expect( self.ch.connection._flush_buffered_frames )
    expect( self.ch.connection._callback_open )

    self.ch._recv_open_ok('frame')
    assert_true( self.ch.connection._connected )

  def test_send_close(self):
    self.ch.connection._close_info = {
      'reply_code':42,
      'reply_text':'wrong answer'*60,
      'class_id':4,
      'method_id':20,
    }
    
    with expect(mock(connection,'Writer')).returns(mock()) as writer:
      expect( writer.write_short ).args( 42 )
      expect( writer.write_shortstr ).args( ('wrong answer'*60)[:255] )
      expect( writer.write_short ).args( 4 )
      expect( writer.write_short ).args( 20 )

      expect(mock(connection,'MethodFrame')).args(0,10,50,writer).returns('frame')
      expect( self.ch.send_frame ).args( 'frame' )

    self.ch._send_close()

  def test_recv_close(self):
    self.ch.connection._closed = False

    frame = mock()
    expect( frame.args.read_short ).returns( 42 )
    expect( frame.args.read_shortstr ).returns( 'wrong answer' )
    expect( frame.args.read_short ).returns( 4 )
    expect( frame.args.read_short ).returns( 20 )

    expect( self.ch._send_close_ok )
    expect( self.ch.connection.disconnect )
    expect( self.ch.connection._callback_close )

    self.ch._recv_close( frame )
    assert_equals( self.ch.connection._close_info, {
      'reply_code':42,
      'reply_text':'wrong answer',
      'class_id':4,
      'method_id':20,
    })
    assert_true( self.ch.connection._closed )

  def test_send_close_ok(self):
    expect(mock(connection,'MethodFrame')).args(0,10,51).returns('frame')
    expect( self.ch.send_frame ).args( 'frame' )
    self.ch._send_close_ok()

  def test_recv_close_ok(self):
    self.ch.connection._closed = False
    expect( self.ch.connection.disconnect )
    expect( self.ch.connection._callback_close )

    self.ch._recv_close_ok('frame')
    assert_true( self.ch.connection._closed )
Esempio n. 4
0
 def setUp(self):
     super(ConnectionChannelTest, self).setUp()
     self.connection = mock()
     self.ch = ConnectionChannel(self.connection, 0, {})
Esempio n. 5
0
class ConnectionChannelTest(Chai):
    def setUp(self):
        super(ConnectionChannelTest, self).setUp()
        self.connection = mock()
        self.ch = ConnectionChannel(self.connection, 0, {})

    def test_init(self):
        mock(connection, 'super')
        with expect(connection,
                    'super').args(is_arg(ConnectionChannel),
                                  ConnectionChannel).returns(mock()) as s:
            expect(s.__init__).args('a', 'b')

        c = ConnectionChannel('a', 'b')
        assert_equals(
            c._method_map, {
                10: c._recv_start,
                20: c._recv_secure,
                30: c._recv_tune,
                41: c._recv_open_ok,
                50: c._recv_close,
                51: c._recv_close_ok,
            })
        assert_equal(0, c._last_heartbeat_send)

    def test_dispatch_on_heartbeat_frame(self):
        frame = mock()

        expect(frame.type).returns(HeartbeatFrame.type())
        expect(self.ch.send_heartbeat)

        self.ch.dispatch(frame)

    def test_dispatch_method_frame_class_10(self):
        frame = mock()
        frame.class_id = 10
        frame.method_id = 10
        method = self.ch._method_map[10] = mock()

        expect(frame.type).returns(MethodFrame.type())
        expect(method).args(frame)

        self.ch.dispatch(frame)

    def test_dispatch_runs_callbacks(self):
        frame = mock()
        frame.class_id = 10
        frame.method_id = 10
        method = self.ch._method_map[10] = mock()
        cb = mock()

        expect(frame.type).returns(MethodFrame.type())
        expect(self.ch.clear_synchronous_cb).args(method).returns(cb)
        expect(cb).args(frame)

        self.ch.dispatch(frame)

    def test_dispatch_method_frame_raises_invalidmethod(self):
        frame = mock()
        frame.class_id = 10
        frame.method_id = 500

        expect(frame.type).returns(MethodFrame.type())

        with assert_raises(Channel.InvalidMethod):
            self.ch.dispatch(frame)

    def test_dispatch_method_frame_raises_invalidclass(self):
        frame = mock()
        frame.class_id = 11
        frame.method_id = 10

        expect(frame.type).returns(MethodFrame.type())

        with assert_raises(Channel.InvalidClass):
            self.ch.dispatch(frame)

    def test_dispatch_method_frame_raises_invalidframetype(self):
        frame = mock()

        expect(frame.type).returns(HeaderFrame.type())

        with assert_raises(Frame.InvalidFrameType):
            self.ch.dispatch(frame)

    def test_close(self):
        expect(self.ch._send_close)
        self.ch.close()

    def test_send_heartbeat_when_no_heartbeat(self):
        stub(self.ch.send_frame)
        self.ch.connection._heartbeat = None

        self.ch.send_heartbeat()

    def test_send_heartbeat_when_not_sent_yet(self):
        mock(connection, 'time')
        self.ch.connection._heartbeat = 3
        self.ch._last_heartbeat_send = 0

        expect(connection.time.time).returns(4200.3).times(2)
        expect(self.ch.send_frame).args(HeartbeatFrame)

        self.ch.send_heartbeat()
        assert_equals(4200.3, self.ch._last_heartbeat_send)

    def test_send_heartbeat_when_sent_long_ago(self):
        mock(connection, 'time')
        self.ch.connection._heartbeat = 3
        self.ch._last_heartbeat_send = 4196

        expect(connection.time.time).returns(4200.3).times(2)
        expect(self.ch.send_frame).args(HeartbeatFrame)

        self.ch.send_heartbeat()
        assert_equals(4200.3, self.ch._last_heartbeat_send)

    def test_send_heart_when_sent_recently(self):
        mock(connection, 'time')
        self.ch.connection._heartbeat = 3
        self.ch._last_heartbeat_send = 4199

        expect(connection.time.time).returns(4200.3)
        stub(self.ch.send_frame)

        self.ch.send_heartbeat()
        assert_equals(4199, self.ch._last_heartbeat_send)

    def test_recv_start(self):
        expect(self.ch._send_start_ok)
        self.ch.connection._closed = 'maybe'

        self.ch._recv_start('frame')
        assert_false(self.ch.connection._closed)

    def test_send_start_ok(self):
        self.ch.connection._properties = 'props'
        self.ch.connection._login_method = 'please'
        self.ch.connection._login_response = 'thanks'
        self.ch.connection._locale = 'home'

        with expect(mock(connection, 'Writer')).returns(mock()) as writer:
            expect(writer.write_table).args('props')
            expect(writer.write_shortstr).args('please')
            expect(writer.write_longstr).args('thanks')
            expect(writer.write_shortstr).args('home')

            expect(mock(connection,
                        'MethodFrame')).args(0, 10, 11,
                                             writer).returns('frame')
            expect(self.ch.send_frame).args('frame')
        expect(self.ch.add_synchronous_cb).args(self.ch._recv_tune)

        self.ch._send_start_ok()

    def test_recv_tune_when_no_broker_max_and_defined_heartbeat(self):
        self.ch.connection._channel_max = 42
        self.ch.connection._frame_max = 43
        self.ch.connection._heartbeat = 8

        frame = mock()
        expect(frame.args.read_short).returns(0)
        expect(frame.args.read_long).returns(0)

        expect(self.ch._send_tune_ok)
        expect(self.ch._send_open)
        expect(self.ch.send_heartbeat)

        self.ch._recv_tune(frame)
        assert_equals(42, self.ch.connection._channel_max)
        assert_equals(43, self.ch.connection._frame_max)
        assert_equals(8, self.ch.connection._heartbeat)

    def test_recv_tune_when_broker_max_and_undefined_heartbeat(self):
        self.ch.connection._channel_max = 42
        self.ch.connection._frame_max = 43
        self.ch.connection._heartbeat = None

        frame = mock()
        expect(frame.args.read_short).returns(500)
        expect(frame.args.read_long).returns(501)
        expect(frame.args.read_short).returns(7)

        expect(self.ch._send_tune_ok)
        expect(self.ch._send_open)
        expect(self.ch.send_heartbeat)

        self.ch._recv_tune(frame)
        assert_equals(500, self.ch.connection._channel_max)
        assert_equals(501, self.ch.connection._frame_max)
        assert_equals(7, self.ch.connection._heartbeat)

    def test_send_tune_ok_when_heartbeat(self):
        self.ch.connection._channel_max = 42
        self.ch.connection._frame_max = 43
        self.ch.connection._heartbeat = 8

        with expect(mock(connection, 'Writer')).returns(mock()) as writer:
            expect(writer.write_short).args(42)
            expect(writer.write_long).args(43)
            expect(writer.write_short).args(8)

            expect(mock(connection,
                        'MethodFrame')).args(0, 10, 31,
                                             writer).returns('frame')
            expect(self.ch.send_frame).args('frame')

        self.ch._send_tune_ok()

    def test_send_tune_ok_when_no_heartbeat(self):
        self.ch.connection._channel_max = 42
        self.ch.connection._frame_max = 43
        self.ch.connection._heartbeat = None

        with expect(mock(connection, 'Writer')).returns(mock()) as writer:
            expect(writer.write_short).args(42)
            expect(writer.write_long).args(43)
            expect(writer.write_short).args(0)

            expect(mock(connection,
                        'MethodFrame')).args(0, 10, 31,
                                             writer).returns('frame')
            expect(self.ch.send_frame).args('frame')

        self.ch._send_tune_ok()

    def test_recv_secure(self):
        expect(self.ch._send_open)
        self.ch._recv_secure('frame')

    def test_send_open(self):
        self.connection._vhost = '/foo'

        with expect(mock(connection, 'Writer')).returns(mock()) as writer:
            expect(writer.write_shortstr).args('/foo')
            expect(writer.write_shortstr).args('')
            expect(writer.write_bit).args(True)

            expect(mock(connection,
                        'MethodFrame')).args(0, 10, 40,
                                             writer).returns('frame')
            expect(self.ch.send_frame).args('frame')
        expect(self.ch.add_synchronous_cb).args(self.ch._recv_open_ok)

        self.ch._send_open()

    def test_recv_open_ok(self):
        self.ch.connection._connected = False
        expect(self.ch.connection._flush_buffered_frames)
        expect(self.ch.connection._callback_open)

        self.ch._recv_open_ok('frame')
        assert_true(self.ch.connection._connected)

    def test_send_close(self):
        self.ch.connection._close_info = {
            'reply_code': 42,
            'reply_text': 'wrong answer' * 60,
            'class_id': 4,
            'method_id': 20,
        }

        with expect(mock(connection, 'Writer')).returns(mock()) as writer:
            expect(writer.write_short).args(42)
            expect(writer.write_shortstr).args(('wrong answer' * 60)[:255])
            expect(writer.write_short).args(4)
            expect(writer.write_short).args(20)

            expect(mock(connection,
                        'MethodFrame')).args(0, 10, 50,
                                             writer).returns('frame')
            expect(self.ch.send_frame).args('frame')
        expect(self.ch.add_synchronous_cb).args(self.ch._recv_close_ok)

        self.ch._send_close()

    def test_recv_close(self):
        self.ch.connection._closed = False

        frame = mock()
        expect(frame.args.read_short).returns(42)
        expect(frame.args.read_shortstr).returns('wrong answer')
        expect(frame.args.read_short).returns(4)
        expect(frame.args.read_short).returns(20)

        expect(self.ch._send_close_ok)
        expect(self.ch.connection.disconnect)
        expect(self.ch.connection._callback_close)

        self.ch._recv_close(frame)
        assert_equals(
            self.ch.connection._close_info, {
                'reply_code': 42,
                'reply_text': 'wrong answer',
                'class_id': 4,
                'method_id': 20,
            })
        assert_true(self.ch.connection._closed)

    def test_send_close_ok(self):
        expect(mock(connection, 'MethodFrame')).args(0, 10,
                                                     51).returns('frame')
        expect(self.ch.send_frame).args('frame')
        self.ch._send_close_ok()

    def test_recv_close_ok(self):
        self.ch.connection._closed = False
        expect(self.ch.connection.disconnect)
        expect(self.ch.connection._callback_close)

        self.ch._recv_close_ok('frame')
        assert_true(self.ch.connection._closed)