diff --git a/internal/pkg/server/coordinator.go b/internal/pkg/server/coordinator.go index 65c98a8..6bf495c 100644 --- a/internal/pkg/server/coordinator.go +++ b/internal/pkg/server/coordinator.go @@ -102,7 +102,7 @@ func (c *CentralCoordinator) ClientHandler(cl *Client) *ClientResponse { cr.URL, cr.Org, cr.Token, cr.Bucket, err = c.Database.GetReactorClient(cl.Id) } else { // throw error - err = errors.New(fmt.Sprintf("Client type %s not recognized!")) + err = fmt.Errorf("Client type %s not recognized!", cl.Type) } // returns based on cl type if err != nil { @@ -166,7 +166,10 @@ func (m *ReactorManagers) UpdateReactorManager(cl *Client, errCh chan error) err if !exists { logging.Debug(logging.DClient, "RCO creating manager for reactor client %v", cl.Id) // creating - rm = NewReactorManager(cl, m.Config, errCh) + if rm, err = NewReactorManager(cl, m.Config, errCh); err != nil { + return err + } + // starting if err = rm.Start(); err != nil { return err diff --git a/internal/pkg/server/reactormanager_test.go b/internal/pkg/server/reactormanager_test.go new file mode 100644 index 0000000..64cd1b0 --- /dev/null +++ b/internal/pkg/server/reactormanager_test.go @@ -0,0 +1,58 @@ +package server + +import ( + "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", + } +} + +// 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") +}