def test_write_frame(self): w = mock() expect(mock(heartbeat_frame, 'Writer')).args('buffer').returns(w) expect(w.write_octet).args(8).returns(w) expect(w.write_short).args(42).returns(w) expect(w.write_long).args(0).returns(w) expect(w.write_octet).args(0xce) frame = HeartbeatFrame(42) frame.write_frame('buffer')
def dispatch(self, frame): ''' Override the default dispatch since we don't need the rest of the stack. ''' if frame.type() == HeartbeatFrame.type(): self.send_heartbeat() elif frame.type() == MethodFrame.type(): if frame.class_id == 10: cb = self._method_map.get(frame.method_id) if cb: method = self.clear_synchronous_cb(cb) method(frame) else: raise Channel.InvalidMethod( "unsupported method %d on channel %d", frame.method_id, self.channel_id) else: raise Channel.InvalidClass( "class %d is not supported on channel %d", frame.class_id, self.channel_id) else: raise Frame.InvalidFrameType( "frame type %d is not supported on channel %d", frame.type(), self.channel_id)
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 send_heartbeat(self): ''' Send a heartbeat if needed. Tracks last heartbeat send time. ''' # Note that this does not take into account the time that we last # sent a frame. Hearbeats are so small the effect should be quite # limited. Also note that we're looking for something near to our # scheduled interval, because if this is exact, then we'll likely # actually send a heartbeat at twice the period, which could cause # a broker to kill the connection if the period is large enough. The # 90% bound is arbitrary but seems a sensible enough default. if self.connection._heartbeat: if time.time() >= (self._last_heartbeat_send + 0.9 * self.connection._heartbeat): self.send_frame(HeartbeatFrame(self.channel_id)) self._last_heartbeat_send = time.time()
def test_send_frame_when_not_closed_and_flow_control(self): conn = mock() c = Channel(conn, 32, {}) c._active = False method = MethodFrame(1, 2, 3) heartbeat = HeartbeatFrame() header = HeaderFrame(1, 2, 3, 4) content = ContentFrame(1, 'foo') expect(conn.send_frame).args(method) expect(conn.send_frame).args(heartbeat) c.send_frame(method) c.send_frame(heartbeat) assert_raises(Channel.Inactive, c.send_frame, header) assert_raises(Channel.Inactive, c.send_frame, content)
def test_parse(self): frame = HeartbeatFrame.parse(42, 'payload') assert_true(isinstance(frame, HeartbeatFrame)) assert_equals(42, frame.channel_id)
def test_type(self): assert_equals(8, HeartbeatFrame.type())