Skip to content

Commit

Permalink
test: add tests for manager
Browse files Browse the repository at this point in the history
  • Loading branch information
raczu committed Nov 9, 2024
1 parent 1621f3b commit 1a30b75
Show file tree
Hide file tree
Showing 3 changed files with 477 additions and 8 deletions.
30 changes: 22 additions & 8 deletions internal/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package manager
import (
"context"
"crypto/tls"
"errors"
k2kconfig "github.com/raczu/kube2kafka/internal/config"
"github.com/raczu/kube2kafka/pkg/exporter"
"github.com/raczu/kube2kafka/pkg/kube/watcher"
Expand All @@ -13,6 +14,10 @@ import (
"sync"
)

var (
ErrManagerNotSetup = errors.New("manager was not set up")
)

type Manager struct {
watcher *watcher.Watcher
processor *processor.Processor
Expand Down Expand Up @@ -98,17 +103,20 @@ func (m *Manager) Setup() error {
}

func (m *Manager) Start(ctx context.Context) error {
if m.watcher == nil || m.processor == nil || m.exporter == nil {
return ErrManagerNotSetup
}

m.logger.Info("starting manager...")
ch := make(chan error, 1)
errs := make(chan error, 1)
subctx, cancel := context.WithCancel(ctx)
defer cancel()

m.wg.Add(3)
go func() {
defer m.wg.Done()
if err := m.watcher.Watch(subctx); err != nil {
ch <- err
return
errs <- err
}
}()

Expand All @@ -120,19 +128,25 @@ func (m *Manager) Start(ctx context.Context) error {
go func() {
defer m.wg.Done()
if err := m.exporter.Export(subctx); err != nil {
ch <- err
errs <- err
}
}()

var err error
select {
case <-subctx.Done():
m.logger.Info("context canceled, shutting down manager...")
case err = <-ch:
m.logger.Error("one of the components failed", zap.Error(err))
case <-ctx.Done():
m.logger.Info("context canceled, stopping manager...")
case err = <-errs:
m.logger.Error("one of the components failed, stopping manager...")
cancel()
}

m.wg.Wait()
// Ensure that error is not lost even if the context was canceled.
close(errs)
if e, ok := <-errs; ok {
err = e
}

return err
}
13 changes: 13 additions & 0 deletions internal/manager/manager_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package manager_test

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func TestManager(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Manager Suite")
}
Loading

0 comments on commit 1a30b75

Please sign in to comment.