You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
2.0 KiB
Go

package server
import (
pb "FRMS/internal/pkg/grpc"
"math/rand"
"testing"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
)
// dummyClient creates a dummy client for testing.
func dummyClient() *Client {
return &Client{
Id: rand.Int(),
Model: "dummy",
Type: "dummy",
}
}
func dummyDevices() []*pb.Device {
numDevs := 10
devs := make([]*pb.Device, numDevs)
for i := 0; i < numDevs; i++ {
dev := &pb.Device{
Addr: int32(rand.Intn(255)),
Status: pb.Status(rand.Intn(2)),
}
devs = append(devs, dev)
}
return devs
}
// dummyReactorManager creates a dummy reactor manager for testing.
func dummyReactorManager() (*ReactorManager, error) {
ch := make(chan error)
cl := dummyClient()
return NewReactorManager(cl, viper.New(), ch)
}
// TestNewReactorManager tries to create a new reactor manager.
func TestNewReactorManager(t *testing.T) {
assert := assert.New(t)
_, err := dummyReactorManager()
assert.Equal(err, nil, "failed to create reactor manager")
}
// TestReactorManager tries to start/stop reactor manager
func TestReactorManager(t *testing.T) {
assert := assert.New(t)
rm, err := dummyReactorManager()
assert.Equal(err, nil, "failed to create reactor manager")
cycles := 10
for i := 0; i < cycles; i++ {
assert.NoError(rm.Start(), "failed to start")
assert.NoError(rm.Stop(), "failed to start")
}
}
// TestUpdateClient tries to update a reactor managers embedded client.
func TestUpdateClient(t *testing.T) {
assert := assert.New(t)
rm, err := dummyReactorManager()
assert.Equal(err, nil, "failed to create reactor manager")
cl := dummyClient()
assert.NoError(rm.UpdateClient(cl), "failed to update client")
}
// TestReactorDeviceHandler ensures that a list of devices can be processed.
func TestReactorDeviceHandler(t *testing.T) {
assert := assert.New(t)
rm, err := dummyReactorManager()
assert.Equal(err, nil, "failed to create reactor manager")
devs := dummyDevices()
assert.NoError(rm.ReactorDeviceHandler(devs), "failed to handle devices")
}