118 lines
4.3 KiB
Go
118 lines
4.3 KiB
Go
package main
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"src.dualinventive.com/go/dinet/rpc"
|
|
mtiep "src.dualinventive.com/go/websocketserver/internal/mtiwss/testutil"
|
|
"src.dualinventive.com/go/websocketserver/internal/testenv"
|
|
)
|
|
|
|
// Test if the redis device data is send to the websocket client from redis after subscribe
|
|
func TestDeviceSubscribeInitial(t *testing.T) {
|
|
tenv := testenv.New(t)
|
|
defer tenv.Close()
|
|
|
|
// Prepare redis with device:info data and send the initial subscription
|
|
_, err := tenv.RedisDo("HSET",
|
|
"device:001fc23154ff65065182565553470187",
|
|
"device:info", `{"label":"zkl-3000-rc","version":"1.2.3"}`)
|
|
require.Nil(t, err)
|
|
tenv.MtiWss().SetResponse(mtiep.RealtimeReply)
|
|
tenv.Ws().WriteString(mtiep.RealtimeRequest)
|
|
|
|
// Read RPC message from websocket
|
|
msg, result, err := tenv.Ws().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":{"label":"zkl-3000-rc","version":"1.2.3"}}`)
|
|
}
|
|
|
|
// Test if the redis project data is send to the websocket client from redis after subscribe
|
|
func TestProjectSubscribeInitial(t *testing.T) {
|
|
tenv := testenv.New(t)
|
|
defer tenv.Close()
|
|
|
|
// Prepare redis with device:info data and send the initial subscription
|
|
_, err := tenv.RedisDo("HMSET",
|
|
"project:10",
|
|
"last_update", `1234`,
|
|
"project:counter", `{}`,
|
|
"project:status", `{}`)
|
|
require.Nil(t, err)
|
|
tenv.MtiWss().SetResponse(mtiep.RealtimeReply)
|
|
tenv.Ws().WriteString(mtiep.RealtimeRequest)
|
|
|
|
// Read RPC message from websocket
|
|
msg, result, err := tenv.Ws().ReadRPCMsg()
|
|
require.Nil(t, err)
|
|
require.NotNil(t, msg)
|
|
|
|
assert.Nil(t, msg.Error)
|
|
assert.Equal(t, msg.Type, rpc.MsgTypePublish)
|
|
assert.Equal(t, msg.ClassMethod, rpc.ClassMethodRealtimeData)
|
|
assert.Equal(t, msg.ProjectID, uint(10))
|
|
assert.JSONEq(t, `{"last_update":1234,"project:counter":{},"project:status":{}}`, result)
|
|
}
|
|
|
|
// Test if the redis device update is passed through the wss to a websocket client from a redis publish "event"
|
|
// and if a not-subscribed field and value is filtered for the connected client
|
|
func TestDeviceUpdatePublishEvent(t *testing.T) {
|
|
tenv := testenv.New(t)
|
|
defer tenv.Close()
|
|
|
|
// Prepare redis with device:001fc23154ff65065182565553470187 data and send the initial subscription
|
|
_, err := tenv.RedisDo("HMSET",
|
|
"device:001fc23154ff65065182565553470187",
|
|
"last_update", `12345678`,
|
|
"device:info", `{"version":"1.2.3"}`,
|
|
"sensor:3:data", `{}`,
|
|
"sensor:5:data", `{}`) // sensor:5:data is filtered
|
|
require.Nil(t, err)
|
|
tenv.MtiWss().SetResponse(mtiep.RealtimeReply)
|
|
tenv.Ws().WriteString(mtiep.RealtimeRequest)
|
|
|
|
// Read initial realtime:data pub RPC message from websocket
|
|
msg, result, err := tenv.Ws().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")
|
|
// In the initial response the sensor:5:data is not present because the connection is not subscribed
|
|
assert.JSONEq(t, `{"last_update":12345678,"device:info":{"version":"1.2.3"},"sensor:3:data":{}}`, result)
|
|
|
|
// Publish with redis on channel device, msg 001fc23154ff65065182565553470187
|
|
_, err = tenv.RedisDo("HSET", "device:001fc23154ff65065182565553470187", "sensor:3:data", `{"value":"1337"}`)
|
|
require.Nil(t, err)
|
|
_, err = tenv.RedisDo("PUBLISH", "device", "001fc23154ff65065182565553470187")
|
|
require.Nil(t, err)
|
|
|
|
// Read realtime:data pub event RPC message from websocket
|
|
msg, result, err = tenv.Ws().ReadRPCMsg()
|
|
require.Nil(t, err)
|
|
require.NotNil(t, msg)
|
|
|
|
assert.Nil(t, msg.Error)
|
|
assert.Equal(t, msg.Type, rpc.MsgTypePublish)
|
|
assert.Equal(t, msg.ClassMethod, rpc.ClassMethodRealtimeData)
|
|
assert.Equal(t, msg.DeviceUID, "001fc23154ff65065182565553470187")
|
|
// In the event triggered realtime:data pub RPC message sensor:5:data is not present
|
|
// because the connection is not subscribed
|
|
assert.JSONEq(t,
|
|
`{"last_update":12345678,
|
|
"device:info":{"version":"1.2.3"},
|
|
"sensor:3:data":{"value":"1337"}}`, result)
|
|
}
|