Skip to content

Commit

Permalink
Make tests faster by fast polling
Browse files Browse the repository at this point in the history
  • Loading branch information
sheerun committed May 11, 2020
1 parent d65e8e2 commit f8860b2
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 10 deletions.
2 changes: 1 addition & 1 deletion source/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func NewIstioGatewaySource(
informerFactory.Start(wait.NeverStop)

// wait for the local cache to be populated.
err = wait.Poll(time.Second, 60*time.Second, func() (bool, error) {
err = poll(time.Second, 60*time.Second, func() (bool, error) {
return serviceInformer.Informer().HasSynced(), nil
})
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion source/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func NewIngressSource(kubeClient kubernetes.Interface, namespace, annotationFilt
informerFactory.Start(wait.NeverStop)

// wait for the local cache to be populated.
err = wait.Poll(time.Second, 60*time.Second, func() (bool, error) {
err = poll(time.Second, 60*time.Second, func() (bool, error) {
return ingressInformer.Informer().HasSynced(), nil
})
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions source/ingress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
v1 "k8s.io/api/core/v1"
"k8s.io/api/extensions/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes/fake"

"sigs.k8s.io/external-dns/endpoint"
Expand Down Expand Up @@ -1018,7 +1017,7 @@ func testIngressEndpoints(t *testing.T) {
var err error

// wait up to a few seconds for new resources to appear in informer cache.
err = wait.Poll(time.Second, 3*time.Second, func() (bool, error) {
err = poll(time.Second, 3*time.Second, func() (bool, error) {
res, err = ingressSource.Endpoints()
if err != nil {
// stop waiting if we get an error
Expand Down
2 changes: 1 addition & 1 deletion source/ingressroute.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func NewContourIngressRouteSource(
informerFactory.Start(wait.NeverStop)

// wait for the local cache to be populated.
err = wait.Poll(time.Second, 60*time.Second, func() (bool, error) {
err = poll(time.Second, 60*time.Second, func() (bool, error) {
return ingressRouteInformer.Informer().HasSynced(), nil
})
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion source/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func NewNodeSource(kubeClient kubernetes.Interface, annotationFilter, fqdnTempla
informerFactory.Start(wait.NeverStop)

// wait for the local cache to be populated.
err = wait.Poll(time.Second, 60*time.Second, func() (bool, error) {
err = poll(time.Second, 60*time.Second, func() (bool, error) {
return nodeInformer.Informer().HasSynced(), nil
})
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion source/ocproute.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func NewOcpRouteSource(
informerFactory.Start(wait.NeverStop)

// wait for the local cache to be populated.
err = wait.Poll(time.Second, 60*time.Second, func() (bool, error) {
err = poll(time.Second, 60*time.Second, func() (bool, error) {
return routeInformer.Informer().HasSynced(), nil
})
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion source/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func NewServiceSource(kubeClient kubernetes.Interface, namespace, annotationFilt
informerFactory.Start(wait.NeverStop)

// wait for the local cache to be populated.
err = wait.Poll(time.Second, 60*time.Second, func() (bool, error) {
err = poll(time.Second, 60*time.Second, func() (bool, error) {
return serviceInformer.Informer().HasSynced(), nil
})
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions source/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/stretchr/testify/suite"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes/fake"

"sigs.k8s.io/external-dns/endpoint"
Expand Down Expand Up @@ -1118,7 +1117,7 @@ func testServiceSourceEndpoints(t *testing.T) {
var res []*endpoint.Endpoint

// wait up to a few seconds for new resources to appear in informer cache.
err = wait.Poll(time.Second, 3*time.Second, func() (bool, error) {
err = poll(time.Second, 3*time.Second, func() (bool, error) {
res, err = client.Endpoints()
if err != nil {
// stop waiting if we get an error
Expand Down
13 changes: 13 additions & 0 deletions source/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/wait"
"sigs.k8s.io/external-dns/endpoint"
)

Expand Down Expand Up @@ -224,3 +225,15 @@ func matchLabelSelector(selector labels.Selector, srcAnnotations map[string]stri
annotations := labels.Set(srcAnnotations)
return selector.Matches(annotations)
}

// Used for fast testing
var FAST_POLL = false

func poll(interval time.Duration, timeout time.Duration, condition wait.ConditionFunc) error {
if FAST_POLL {
interval = 10 * time.Millisecond
timeout = 10 * time.Second
}

return wait.Poll(interval, timeout, condition)
}
7 changes: 7 additions & 0 deletions source/source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package source
import (
"context"
"fmt"
"os"
"testing"
"time"

Expand All @@ -28,6 +29,12 @@ import (
"sigs.k8s.io/external-dns/internal/testutils"
)

func TestMain(m *testing.M) {
FAST_POLL = true
// call flag.Parse() here if TestMain uses flags
os.Exit(m.Run())
}

func TestGetTTLFromAnnotations(t *testing.T) {
for _, tc := range []struct {
title string
Expand Down

0 comments on commit f8860b2

Please sign in to comment.