Skip to content

Commit

Permalink
Add some more tests and optimize others
Browse files Browse the repository at this point in the history
Signed-off-by: jannfis <[email protected]>
  • Loading branch information
jannfis committed Oct 30, 2024
1 parent 7883dbf commit 179b3ea
Showing 1 changed file with 44 additions and 46 deletions.
90 changes: 44 additions & 46 deletions internal/informer/informer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package informer

import (
"context"
"errors"
"testing"
"time"

Expand Down Expand Up @@ -32,6 +33,16 @@ func Test_Informer(t *testing.T) {
assert.Nil(t, i)
assert.ErrorContains(t, err, "without watch function")
})

t.Run("Error on option", func(t *testing.T) {
opt := func(i *GenericInformer) error {
return errors.New("some error")
}
i, err := NewGenericInformer(&v1alpha1.Application{}, opt)
assert.Nil(t, i)
assert.ErrorContains(t, err, "some error")
})

t.Run("Instantiate generic informer without processing", func(t *testing.T) {
i, err := NewGenericInformer(&v1alpha1.Application{},
WithListCallback(listFunc),
Expand All @@ -47,57 +58,43 @@ func Test_Informer(t *testing.T) {
assert.False(t, i.IsRunning())
})

t.Run("Cannot start informer twice", func(t *testing.T) {
ctl := fakeGenericInformer(t)
err := ctl.i.Start(context.TODO())
assert.NoError(t, err)
err = ctl.i.Start(context.TODO())
assert.Error(t, err)
})

t.Run("Can stop informer only when running", func(t *testing.T) {
ctl := fakeGenericInformer(t)
// Not running yet
err := ctl.i.Stop()
assert.Error(t, err)
err = ctl.i.Start(context.TODO())
assert.NoError(t, err)
// Running
err = ctl.i.Stop()
assert.NoError(t, err)
// Not running
err = ctl.i.Stop()
assert.Error(t, err)
})

t.Run("Run callbacks", func(t *testing.T) {
addCh := make(chan (bool))
updateCh := make(chan (bool))
delCh := make(chan (bool))
i, err := NewGenericInformer(&v1alpha1.Application{},
WithListCallback(listFunc),
WithWatchCallback(nopWatchFunc),
WithAddCallback(func(obj interface{}) {
addCh <- true
}),
WithUpdateCallback(func(newObj, oldObj interface{}) {
updateCh <- true
}),
WithDeleteCallback(func(obj interface{}) {
delCh <- true
}),
)
require.NotNil(t, i)
require.NoError(t, err)
err = i.Start(context.Background())
ctl := fakeGenericInformer(t)
err := ctl.i.Start(context.TODO())
assert.NoError(t, err)
assert.True(t, i.IsRunning())
watcher.Add(&v1alpha1.Application{})
tick := time.NewTicker(1 * time.Second)
app := &v1alpha1.Application{}
watcher.Add(app)
watcher.Delete(app)
watcher.Modify(app)
var added, updated, removed bool
run := true
for run {
select {
case added = <-addCh:
case updated = <-updateCh:
case removed = <-delCh:
case <-tick.C:
t.Error("timeout expired")
run = false
default:
if added && removed && updated {
run = false
}
time.Sleep(50 * time.Millisecond)
}
}
ctl.watcher.Add(app)
ctl.watcher.Modify(app)
ctl.watcher.Delete(app)
added, updated, deleted := requireCallbacks(t, ctl)
assert.True(t, added)
assert.True(t, updated)
assert.True(t, removed)
err = i.Stop()
assert.True(t, deleted)
err = ctl.i.Stop()
assert.NoError(t, err)
assert.False(t, i.IsRunning())
})
}

Expand All @@ -109,6 +106,7 @@ type informerCtl struct {
watcher *watch.FakeWatcher
}

// fakeGenericInformer returns a generic informer suitable for unit testing.
func fakeGenericInformer(t *testing.T, opts ...InformerOption) *informerCtl {
var err error
ctl := &informerCtl{
Expand Down Expand Up @@ -148,7 +146,7 @@ func fakeGenericInformer(t *testing.T, opts ...InformerOption) *informerCtl {
func requireCallbacks(t *testing.T, ctl *informerCtl) (added, updated, deleted bool) {
t.Helper()
run := true
tick := time.NewTicker(2 * time.Second)
tick := time.NewTicker(1 * time.Second)
added = false
for run {
select {
Expand Down

0 comments on commit 179b3ea

Please sign in to comment.