func TestClient_handleUNSUBACK(t *testing.T) { cli := New(&Options{ ErrorHandler: func(_ error) {}, }) err := cli.Connect(&ConnectOptions{ Network: "tcp", Address: testAddress, ClientID: []byte("clientID"), }) if err != nil { nilErrorExpected(t, err) } defer cli.Disconnect() p := &packet.UNSUBACK{ PacketID: 1, } unsubscribe, err := packet.NewUNSUBSCRIBE(&packet.UNSUBSCRIBEOptions{ PacketID: 1, TopicFilters: [][]byte{[]byte("test")}, }) if err != nil { nilErrorExpected(t, err) } cli.sess.sendingPackets[1] = unsubscribe if err := cli.handleUNSUBACK(p); err != nil { nilErrorExpected(t, err) } }
// Unsubscribe sends an UNSUBSCRIBE Packet to the Server. func (cli *Client) Unsubscribe(opts *UnsubscribeOptions) error { // Lock for reading and updating. cli.muConn.Lock() // Unlock. defer cli.muConn.Unlock() // Check the Network Connection. if cli.conn == nil { return ErrNotYetConnected } // Check the existence of the options. if opts == nil || len(opts.TopicFilters) == 0 { return packet.ErrNoTopicFilter } // Define a Packet Identifier. var packetID uint16 // Define an error. var err error // Lock for updating the Session. cli.muSess.Lock() defer cli.muSess.Unlock() // Generate a Packet Identifer. if packetID, err = cli.generatePacketID(); err != nil { return err } // Create an UNSUBSCRIBE Packet. p, err := packet.NewUNSUBSCRIBE(&packet.UNSUBSCRIBEOptions{ PacketID: packetID, TopicFilters: opts.TopicFilters, }) if err != nil { return err } // Set the Packet to the Session. cli.sess.sendingPackets[packetID] = p // Send the Packet to the Server. cli.conn.send <- p return nil }