tweaked coordinator and reactor manager for better test coverage. documented both, tested reactormanager

main
KeeganForelight 1 year ago
parent a0bda5d6b3
commit 11914d8d67

@ -258,7 +258,7 @@ func (r *ReactorCoordinator) Register() error {
// ReactorStatusHandler is a gRPC handler used to handle incoming // ReactorStatusHandler is a gRPC handler used to handle incoming
// reactor requests containing information about said reactor. // reactor requests containing information about said reactor.
// It will get the associate reactor manager and pass the // It will get the associate reactor manager and pass the
// request down for further processing. // request device information before returning an acknowledgement.
func (r *ReactorCoordinator) ReactorStatusHandler(ctx context.Context, req *pb.ReactorStatusPing) (*pb.ReactorStatusResponse, error) { func (r *ReactorCoordinator) ReactorStatusHandler(ctx context.Context, req *pb.ReactorStatusPing) (*pb.ReactorStatusResponse, error) {
rm, err := r.GetReactorManager(int(req.GetId())) rm, err := r.GetReactorManager(int(req.GetId()))
@ -267,5 +267,7 @@ func (r *ReactorCoordinator) ReactorStatusHandler(ctx context.Context, req *pb.R
return &pb.ReactorStatusResponse{}, err return &pb.ReactorStatusResponse{}, err
} }
return rm.ReactorStatusHandler(ctx, req) go rm.ReactorDeviceHandler(req.GetDevices())
return &pb.ReactorStatusResponse{Id: int32(rm.Id)}, nil
} }

@ -6,8 +6,6 @@ import (
"FRMS/internal/pkg/manager" "FRMS/internal/pkg/manager"
"time" "time"
"context"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
@ -90,17 +88,13 @@ func (r *ReactorManager) UpdateClient(cl *Client) error {
return nil return nil
} }
// ReactorStatusHandler implements a gRPC handler that is called by reactors. // ReactorDeviceHandler processes incoming device information and
// Takes in a context and request which has reactor and device information. // updates the manager accordingly.
// For now, loops over devices and logs information about their status. func (r *ReactorManager) ReactorDeviceHandler(devs []*pb.Device) error {
func (r *ReactorManager) ReactorStatusHandler(
ctx context.Context,
req *pb.ReactorStatusPing,
) (*pb.ReactorStatusResponse, error) {
logging.Debug(logging.DClient, "RMA %v recieved ping", r.Id) logging.Debug(logging.DClient, "RMA %v recieved ping", r.Id)
for _, dev := range req.GetDevices() { for _, dev := range devs {
logging.Debug( logging.Debug(
logging.DClient, logging.DClient,
"RMA %v device %v is %v", "RMA %v device %v is %v",
@ -110,5 +104,5 @@ func (r *ReactorManager) ReactorStatusHandler(
) )
} }
return &pb.ReactorStatusResponse{Id: int32(r.Id)}, nil return nil
} }

@ -1,6 +1,7 @@
package server package server
import ( import (
pb "FRMS/internal/pkg/grpc"
"math/rand" "math/rand"
"testing" "testing"
@ -17,6 +18,22 @@ func dummyClient() *Client {
} }
} }
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. // dummyReactorManager creates a dummy reactor manager for testing.
func dummyReactorManager() (*ReactorManager, error) { func dummyReactorManager() (*ReactorManager, error) {
@ -56,3 +73,13 @@ func TestUpdateClient(t *testing.T) {
assert.NoError(rm.UpdateClient(cl), "failed to update client") 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")
}

Loading…
Cancel
Save