// handle an incoming PubrelPacket func (c *Client) processPubrel(packetID uint16) error { // get packet from store pkt, err := c.Session.LookupPacket(incoming, packetID) if err != nil { return c.die(err, true) } // get packet from store publish, ok := pkt.(*packet.PublishPacket) if !ok { return nil // ignore a wrongly sent PubrelPacket } // prepare pubcomp packet pubcomp := packet.NewPubcompPacket() pubcomp.PacketID = publish.PacketID // acknowledge PublishPacket err = c.send(pubcomp, true) if err != nil { return c.die(err, false) } // remove packet from store err = c.Session.DeletePacket(incoming, packetID) if err != nil { return c.die(err, true) } // call callback if c.Callback != nil { c.Callback(&publish.Message, nil) } return nil }
func TestClientPublishSubscribeQOS2(t *testing.T) { subscribe := packet.NewSubscribePacket() subscribe.Subscriptions = []packet.Subscription{{Topic: "test", QOS: 2}} subscribe.PacketID = 1 suback := packet.NewSubackPacket() suback.ReturnCodes = []uint8{2} suback.PacketID = 1 publish := packet.NewPublishPacket() publish.Message.Topic = "test" publish.Message.Payload = []byte("test") publish.Message.QOS = 2 publish.PacketID = 2 pubrec := packet.NewPubrecPacket() pubrec.PacketID = 2 pubrel := packet.NewPubrelPacket() pubrel.PacketID = 2 pubcomp := packet.NewPubcompPacket() pubcomp.PacketID = 2 broker := tools.NewFlow(). Receive(connectPacket()). Send(connackPacket()). Receive(subscribe). Send(suback). Receive(publish). Send(pubrec). Receive(pubrel). Send(pubcomp). Send(publish). Receive(pubrec). Send(pubrel). Receive(pubcomp). Receive(disconnectPacket()). End() done, port := fakeBroker(t, broker) wait := make(chan struct{}) c := New() c.Callback = func(msg *packet.Message, err error) { assert.NoError(t, err) assert.Equal(t, "test", msg.Topic) assert.Equal(t, []byte("test"), msg.Payload) assert.Equal(t, uint8(2), msg.QOS) assert.False(t, msg.Retain) close(wait) } future, err := c.Connect(NewConfig("tcp://localhost:" + port)) assert.NoError(t, err) assert.NoError(t, future.Wait()) assert.False(t, future.SessionPresent) assert.Equal(t, packet.ConnectionAccepted, future.ReturnCode) subscribeFuture, err := c.Subscribe("test", 2) assert.NoError(t, err) assert.NoError(t, subscribeFuture.Wait()) assert.Equal(t, []uint8{2}, subscribeFuture.ReturnCodes) publishFuture, err := c.Publish("test", []byte("test"), 2, false) assert.NoError(t, err) assert.NoError(t, publishFuture.Wait()) <-wait err = c.Disconnect() assert.NoError(t, err) <-done in, err := c.Session.AllPackets(incoming) assert.NoError(t, err) assert.Equal(t, 0, len(in)) out, err := c.Session.AllPackets(outgoing) assert.NoError(t, err) assert.Equal(t, 0, len(out)) }