Skip to content

Commit

Permalink
✨ Add testcases in controller when source.Channel
Browse files Browse the repository at this point in the history
is passed

Added tests to verify the controller
implementation when `source.Channel`
is passed.
  • Loading branch information
varshaprasad96 committed Feb 6, 2021
1 parent 09bce2f commit 9c8b6ba
Showing 1 changed file with 36 additions and 2 deletions.
38 changes: 36 additions & 2 deletions pkg/internal/controller/controller_test.go
Expand Up @@ -28,6 +28,7 @@ import (
dto "github.com/prometheus/client_model/go"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/util/workqueue"
"sigs.k8s.io/controller-runtime/pkg/cache"
Expand Down Expand Up @@ -172,21 +173,54 @@ var _ = Describe("controller", func() {
}, 10.0)

It("should not error when channel is passed as a source", func(done Done) {
eventCount := 0
// channel to be closed when event is processed
processed := make(chan struct{})
// source channel to be injected
ch := make(chan event.GenericEvent, 1)

ctx, cancel := context.WithCancel(context.TODO())
defer cancel()

ins := &source.Channel{Source: make(chan event.GenericEvent)}
Expect(inject.StopChannelInto(make(<-chan struct{}), ins)).To(BeTrue())
// event to be sent to the channel
p := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "bar"},
}
evt := event.GenericEvent{
Object: p,
}

ins := &source.Channel{Source: ch}
ins.DestBufferSize = 1
Expect(inject.StopChannelInto(ctx.Done(), ins)).To(BeTrue())

// send the event to the channel
ch <- evt

ctrl.startWatches = []watchDescription{{
src: ins,
handler: handler.Funcs{
GenericFunc: func(evt event.GenericEvent, q workqueue.RateLimitingInterface) {
defer GinkgoRecover()
eventCount++

if eventCount > 0 {
close(processed)
}
},
},
}}

go func() {
defer GinkgoRecover()
Expect(ctrl.Start(ctx)).To(Succeed())
}()

select {
case <-processed:
Expect(eventCount).To(BeEquivalentTo(1))
}

close(done)
})

Expand Down

0 comments on commit 9c8b6ba

Please sign in to comment.