add addafter method that permits enqueueing the objects themselves (#408)

This commit is contained in:
Victor Agababov 2019-05-08 16:14:37 -07:00 committed by Knative Prow Robot
parent c0f06c32f1
commit c3f131538a
2 changed files with 46 additions and 0 deletions

View File

@ -117,6 +117,17 @@ func NewImpl(r Reconciler, logger *zap.SugaredLogger, workQueueName string, repo
}
}
// EnqueueAfter takes a resource, converts it into a namespace/name string,
// and passes it to EnqueueKey.
func (c *Impl) EnqueueAfter(obj interface{}, after time.Duration) {
key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(obj)
if err != nil {
c.logger.Errorw("Enqueue", zap.Error(err))
return
}
c.EnqueueKeyAfter(key, after)
}
// Enqueue takes a resource, converts it into a namespace/name string,
// and passes it to EnqueueKey.
func (c *Impl) Enqueue(obj interface{}) {

View File

@ -419,6 +419,41 @@ func TestEnqueues(t *testing.T) {
}
func TestEnqeueAfter(t *testing.T) {
impl := NewImpl(&NopReconciler{}, TestLogger(t), "Testing", &FakeStatsReporter{})
impl.EnqueueAfter(&Resource{
ObjectMeta: metav1.ObjectMeta{
Name: "for",
Namespace: "waiting",
},
}, time.Second)
impl.EnqueueAfter(&Resource{
ObjectMeta: metav1.ObjectMeta{
Name: "waterfall",
Namespace: "the",
},
}, 300*time.Millisecond)
impl.EnqueueAfter(&Resource{
ObjectMeta: metav1.ObjectMeta{
Name: "to",
Namespace: "fall",
},
}, 2*time.Second)
time.Sleep(50 * time.Millisecond)
if got, want := impl.WorkQueue.Len(), 0; got != want {
t.Errorf("|Queue| = %d, want: %d", got, want)
}
// Sleep the remaining time.
time.Sleep(time.Second - 50*time.Millisecond)
if got, want := impl.WorkQueue.Len(), 2; got != want {
t.Errorf("|Queue| = %d, want: %d", got, want)
}
impl.WorkQueue.ShutDown()
if got, want := drainWorkQueue(impl.WorkQueue), []string{"the/waterfall", "waiting/for"}; !cmp.Equal(got, want) {
t.Errorf("Queue = %v, want: %v, diff: %s", got, want, cmp.Diff(got, want))
}
}
func TestEnqeueKeyAfter(t *testing.T) {
impl := NewImpl(&NopReconciler{}, TestLogger(t), "Testing", &FakeStatsReporter{})
impl.EnqueueKeyAfter("waiting/for", time.Second)
impl.EnqueueKeyAfter("the/waterfall", time.Second>>1)