115 lines
3.2 KiB
Go
115 lines
3.2 KiB
Go
package wsconn
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"src.dualinventive.com/go/dinet/rpc"
|
|
wsclnt "src.dualinventive.com/go/websocketserver/internal/wsconn/testutil"
|
|
)
|
|
|
|
// Test if device update publishes are filtered and send to the websocket client
|
|
func TestPublishDeviceUIDUpdate(t *testing.T) {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
|
|
// newMockServer
|
|
srv := newWsconnMockServer(NewWsHandlerContext(ctx, nil))
|
|
defer srv.Close()
|
|
|
|
// Connect with a websocket client
|
|
clnt, err := wsclnt.NewClient(srv.URL())
|
|
require.Nil(t, err)
|
|
defer clnt.Close()
|
|
|
|
// Get the connect client from the list
|
|
clients.mu.RLock()
|
|
li := clients.list.Front()
|
|
clients.mu.RUnlock()
|
|
require.NotNil(t, li)
|
|
require.NotNil(t, li.Value)
|
|
c := li.Value.(*client)
|
|
|
|
// Set device field subscriptions
|
|
clients.mu.Lock()
|
|
c.deviceFields = map[string][]string{
|
|
"001fc23154ff65065182565553470187": {"device:info", "sensor:3:data"},
|
|
}
|
|
clients.mu.Unlock()
|
|
|
|
// Publish to the client for subscribed fields
|
|
c.PublishDeviceUIDUpdate("001fc23154ff65065182565553470187",
|
|
map[string]string{
|
|
"device:info": "{}",
|
|
"sensor:3:data": "{}",
|
|
"sensor:5:data": "{}", // Add a filtered field with value for testing filterFields
|
|
},
|
|
)
|
|
|
|
// Read the RPC realtime:data message from the websocket
|
|
msg, result, err := clnt.ReadRPCMsg()
|
|
require.Nil(t, err)
|
|
require.NotNil(t, msg)
|
|
|
|
assert.Nil(t, msg.Error)
|
|
assert.NotEqual(t, 0, msg.Time)
|
|
assert.Equal(t, msg.Type, rpc.MsgTypePublish)
|
|
assert.Equal(t, msg.ClassMethod, rpc.ClassMethodRealtimeData)
|
|
assert.Equal(t, msg.DeviceUID, "001fc23154ff65065182565553470187")
|
|
assert.JSONEq(t, result, `{"device:info":{},"sensor:3:data":{}}`)
|
|
}
|
|
|
|
// Test if project update publishes are filtered and send to the websocket client
|
|
func TestPublishprojectIDUpdate(t *testing.T) {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
|
|
// newMockServer
|
|
srv := newWsconnMockServer(NewWsHandlerContext(ctx, nil))
|
|
defer srv.Close()
|
|
|
|
// Connect with a websocket client
|
|
clnt, err := wsclnt.NewClient(srv.URL())
|
|
require.Nil(t, err)
|
|
defer clnt.Close()
|
|
|
|
// Get the connect client from the list
|
|
clients.mu.RLock()
|
|
li := clients.list.Front()
|
|
clients.mu.RUnlock()
|
|
require.NotNil(t, li)
|
|
require.NotNil(t, li.Value)
|
|
c := li.Value.(*client)
|
|
|
|
// Set device field subscriptions
|
|
clients.mu.Lock()
|
|
c.projectFields = map[uint64][]string{
|
|
10: {"last_update", "project:counter", "project:status"},
|
|
}
|
|
clients.mu.Unlock()
|
|
|
|
// Publish to the client for subscribed fields
|
|
c.PublishProjectIDUpdate(10,
|
|
map[string]string{
|
|
"last_update": "1234",
|
|
"project:counter": "{}",
|
|
"project:status": "{}",
|
|
"project:info": "{}", // Add a filtered field with value for testing filterFields
|
|
},
|
|
)
|
|
|
|
// Read the RPC realtime:data message from the websocket
|
|
msg, result, err := clnt.ReadRPCMsg()
|
|
require.Nil(t, err)
|
|
require.NotNil(t, msg)
|
|
|
|
assert.Nil(t, msg.Error)
|
|
assert.NotEqual(t, 0, msg.Time)
|
|
assert.Equal(t, msg.Type, rpc.MsgTypePublish)
|
|
assert.Equal(t, msg.ClassMethod, rpc.ClassMethodRealtimeData)
|
|
assert.Equal(t, uint(10), msg.ProjectID)
|
|
assert.JSONEq(t, result, `{"last_update":1234,"project:counter":{},"project:status":{}}`)
|
|
}
|