mirror of
https://github.com/lucacasonato/mqtt.git
synced 2025-09-26 19:01:12 +08:00
Added tests for auth and connecting
This commit is contained in:
6
mqtt.go
6
mqtt.go
@@ -74,10 +74,8 @@ func (c *Client) Connect(ctx context.Context) error {
|
||||
|
||||
for {
|
||||
select {
|
||||
case _, more := <-ctx.Done():
|
||||
if more {
|
||||
return ctx.Err()
|
||||
}
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
case err := <-completer:
|
||||
return err
|
||||
}
|
||||
|
108
mqtt_test.go
108
mqtt_test.go
@@ -1,8 +1,10 @@
|
||||
package mqtt_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/lucacasonato/mqtt"
|
||||
)
|
||||
@@ -81,5 +83,109 @@ func TestNewClientHasClientID(t *testing.T) {
|
||||
if client.Options.ClientID != "client-id" {
|
||||
t.Fatal("client.Options.ClientID should be 'client-id'")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// check that a client gets created and a client id is not changed when it is set
|
||||
func TestNewClientWithAuthentication(t *testing.T) {
|
||||
client, err := mqtt.NewClient(mqtt.ClientOptions{
|
||||
Servers: []string{
|
||||
"tcp://test.mosquitto.org:1883",
|
||||
},
|
||||
Username: "user",
|
||||
Password: "password",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal("err should be nil")
|
||||
}
|
||||
if client == nil {
|
||||
t.Fatal("client should not be nil")
|
||||
}
|
||||
}
|
||||
|
||||
// check that a client gets created and a client id is not changed when it is set
|
||||
func TestConnectSuccess(t *testing.T) {
|
||||
client, err := mqtt.NewClient(mqtt.ClientOptions{
|
||||
Servers: []string{
|
||||
"tcp://test.mosquitto.org:1883",
|
||||
},
|
||||
AutoReconnect: false,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal("err should be nil")
|
||||
}
|
||||
if client == nil {
|
||||
t.Fatal("client should not be nil")
|
||||
}
|
||||
err = client.Connect(context.Background())
|
||||
if err != nil {
|
||||
t.Fatal("connect should not have failed")
|
||||
}
|
||||
}
|
||||
|
||||
// check that a client gets created and a client id is not changed when it is set
|
||||
func TestConnectContextTimeout(t *testing.T) {
|
||||
client, err := mqtt.NewClient(mqtt.ClientOptions{
|
||||
Servers: []string{
|
||||
"tcp://test.mosquitto.org:1883",
|
||||
},
|
||||
AutoReconnect: false,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal("err should be nil")
|
||||
}
|
||||
if client == nil {
|
||||
t.Fatal("client should not be nil")
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Nanosecond)
|
||||
defer cancel()
|
||||
err = client.Connect(ctx)
|
||||
if !errors.Is(err, context.DeadlineExceeded) {
|
||||
t.Fatal("connect should have failed with error context.DeadlineExceeded")
|
||||
}
|
||||
}
|
||||
|
||||
// check that a client gets created and a client id is not changed when it is set
|
||||
func TestConnectContextCancel(t *testing.T) {
|
||||
client, err := mqtt.NewClient(mqtt.ClientOptions{
|
||||
Servers: []string{
|
||||
"tcp://test.mosquitto.org:1883",
|
||||
},
|
||||
AutoReconnect: false,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal("err should be nil")
|
||||
}
|
||||
if client == nil {
|
||||
t.Fatal("client should not be nil")
|
||||
}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
go func() {
|
||||
time.Sleep(1 * time.Microsecond)
|
||||
cancel()
|
||||
}()
|
||||
defer cancel()
|
||||
err = client.Connect(ctx)
|
||||
if !errors.Is(err, context.Canceled) {
|
||||
t.Fatal("connect should have failed with error context.Canceled")
|
||||
}
|
||||
}
|
||||
|
||||
// check that a client gets created and a client id is not changed when it is set
|
||||
func TestConnectFailed(t *testing.T) {
|
||||
client, err := mqtt.NewClient(mqtt.ClientOptions{
|
||||
Servers: []string{
|
||||
"tcp://test.mosquitto.org:1884", // incorrect port
|
||||
},
|
||||
AutoReconnect: false,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal("err should be nil")
|
||||
}
|
||||
if client == nil {
|
||||
t.Fatal("client should not be nil")
|
||||
}
|
||||
err = client.Connect(context.Background())
|
||||
if err == nil {
|
||||
t.Fatal("connect should have failed")
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user