Пример #1
0
func TestClient_handlePUBCOMP(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.PUBCOMP{
		PacketID: 1,
	}

	pubrel, err := packet.NewPUBREL(&packet.PUBRELOptions{
		PacketID: 1,
	})
	if err != nil {
		nilErrorExpected(t, err)
		return
	}

	cli.sess.sendingPackets[1] = pubrel

	if err := cli.handlePUBCOMP(p); err != nil {
		nilErrorExpected(t, err)
	}
}
Пример #2
0
func TestClient_Connect_restoreSession(t *testing.T) {
	cli := New(&Options{
		ErrorHandler: func(_ error) {},
	})

	cli.sess = newSession(false, []byte("cliendID"))

	publish, err := packet.NewPUBLISH(&packet.PUBLISHOptions{
		PacketID: 1,
	})
	if err != nil {
		nilErrorExpected(t, err)
	}

	cli.sess.sendingPackets[1] = publish

	pubrel, err := packet.NewPUBREL(&packet.PUBRELOptions{
		PacketID: 2,
	})
	if err != nil {
		nilErrorExpected(t, err)
	}

	cli.sess.sendingPackets[2] = pubrel

	cli.sess.sendingPackets[3] = packet.NewPINGREQ()

	err = cli.Connect(&ConnectOptions{
		Network: "tcp",
		Address: testAddress,
	})
	if err != nil {
		nilErrorExpected(t, err)
	}
}
Пример #3
0
// handlePUBREC handles the PUBREC Packet.
func (cli *Client) handlePUBREC(p packet.Packet) error {
	// Lock for update.
	cli.muSess.Lock()

	// Unlock.
	defer cli.muSess.Unlock()

	// Extract the Packet Identifier of the Packet.
	id := p.(*packet.PUBREC).PacketID

	// Validate the Packet Identifier.
	if err := cli.validatePacketID(cli.sess.sendingPackets, id, packet.TypePUBLISH); err != nil {
		return err
	}

	// Create a PUBREL Packet.
	pubrel, err := packet.NewPUBREL(&packet.PUBRELOptions{
		PacketID: id,
	})
	if err != nil {
		return err
	}

	// Set the PUBREL Packet to the Session.
	cli.sess.sendingPackets[id] = pubrel

	// Send the Packet to the Server.
	cli.conn.send <- pubrel

	return nil
}
Пример #4
0
func TestClient_handlePacket_PUBREL(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, err := packet.NewPUBREL(&packet.PUBRELOptions{
		PacketID: 1,
	})

	cli.handlePacket(p)
}