mirror of
https://github.com/snapp-incubator/pakhshi.git
synced 2025-09-26 20:21:13 +08:00
feat: Add Test for Client
This commit is contained in:
70
test/client_test.go
Normal file
70
test/client_test.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package main_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/1995parham/pakhshi/pkg/client"
|
||||
mqtt "github.com/eclipse/paho.mqtt.golang"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
// nolint: cyclop
|
||||
func TestMultiSubscriberSinglePublisher(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ch := make(chan string)
|
||||
|
||||
{
|
||||
opts := mqtt.NewClientOptions()
|
||||
opts.AddBroker("tcp://127.0.0.1:1883")
|
||||
opts.AddBroker("tcp://127.0.0.1:1884")
|
||||
|
||||
c := client.NewClient(opts)
|
||||
|
||||
if token := c.Connect(); token.Wait() && token.Error() != nil {
|
||||
assert.NoError(t, token.Error())
|
||||
}
|
||||
|
||||
if token := c.Subscribe("hello", 0, func(c mqtt.Client, m mqtt.Message) {
|
||||
ch <- string(m.Payload())
|
||||
}); token.Wait() && token.Error() != nil {
|
||||
assert.NoError(t, token.Error())
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
opts := mqtt.NewClientOptions()
|
||||
opts.AddBroker("tcp://127.0.0.1:1883")
|
||||
|
||||
c := mqtt.NewClient(opts)
|
||||
|
||||
if token := c.Connect(); token.Wait() && token.Error() != nil {
|
||||
assert.NoError(t, token.Error())
|
||||
}
|
||||
|
||||
msg := "b1"
|
||||
if token := c.Publish("hello", 0, false, msg); token.Wait() && token.Error() != nil {
|
||||
assert.NoError(t, token.Error())
|
||||
}
|
||||
|
||||
assert.Equal(t, msg, <-ch)
|
||||
}
|
||||
|
||||
{
|
||||
opts := mqtt.NewClientOptions()
|
||||
opts.AddBroker("tcp://127.0.0.1:1884")
|
||||
|
||||
c := mqtt.NewClient(opts)
|
||||
|
||||
if token := c.Connect(); token.Wait() && token.Error() != nil {
|
||||
assert.NoError(t, token.Error())
|
||||
}
|
||||
|
||||
msg := "b2"
|
||||
if token := c.Publish("hello", 0, false, msg); token.Wait() && token.Error() != nil {
|
||||
assert.NoError(t, token.Error())
|
||||
}
|
||||
|
||||
assert.Equal(t, msg, <-ch)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user