Skip to content

Commit

Permalink
use sync chan
Browse files Browse the repository at this point in the history
Signed-off-by: sivchari <[email protected]>
  • Loading branch information
sivchari committed Aug 29, 2024
1 parent 0e08009 commit ddd99a1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 26 deletions.
45 changes: 19 additions & 26 deletions internal/test/envtest/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
goruntime "runtime"
"strconv"
"strings"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -71,32 +70,26 @@ import (
"sigs.k8s.io/cluster-api/webhooks"
)

var once sync.Once

func init() {
once.Do(func() {
logger := klog.Background()
// Use klog as the internal logger for this envtest environment.
log.SetLogger(logger)
// Additionally force all controllers to use the Ginkgo logger.
ctrl.SetLogger(logger)
// Add logger for ginkgo.
klog.SetOutput(ginkgo.GinkgoWriter)
// Use the writer that klog sets to write.
klog.LogToStderr(false)

// Calculate the scheme.
utilruntime.Must(apiextensionsv1.AddToScheme(scheme.Scheme))
utilruntime.Must(admissionv1.AddToScheme(scheme.Scheme))
utilruntime.Must(clusterv1.AddToScheme(scheme.Scheme))
utilruntime.Must(bootstrapv1.AddToScheme(scheme.Scheme))
utilruntime.Must(expv1.AddToScheme(scheme.Scheme))
utilruntime.Must(addonsv1.AddToScheme(scheme.Scheme))
utilruntime.Must(controlplanev1.AddToScheme(scheme.Scheme))
utilruntime.Must(admissionv1.AddToScheme(scheme.Scheme))
utilruntime.Must(runtimev1.AddToScheme(scheme.Scheme))
utilruntime.Must(ipamv1.AddToScheme(scheme.Scheme))
})
logger := klog.Background()
// Use klog as the internal logger for this envtest environment.
log.SetLogger(logger)
// Additionally force all controllers to use the Ginkgo logger.
ctrl.SetLogger(logger)
// Add logger for ginkgo.
klog.SetOutput(ginkgo.GinkgoWriter)

// Calculate the scheme.
utilruntime.Must(apiextensionsv1.AddToScheme(scheme.Scheme))
utilruntime.Must(admissionv1.AddToScheme(scheme.Scheme))
utilruntime.Must(clusterv1.AddToScheme(scheme.Scheme))
utilruntime.Must(bootstrapv1.AddToScheme(scheme.Scheme))
utilruntime.Must(expv1.AddToScheme(scheme.Scheme))
utilruntime.Must(addonsv1.AddToScheme(scheme.Scheme))
utilruntime.Must(controlplanev1.AddToScheme(scheme.Scheme))
utilruntime.Must(admissionv1.AddToScheme(scheme.Scheme))
utilruntime.Must(runtimev1.AddToScheme(scheme.Scheme))
utilruntime.Must(ipamv1.AddToScheme(scheme.Scheme))
}

// RunInput is the input for Run.
Expand Down
4 changes: 4 additions & 0 deletions test/infrastructure/inmemory/pkg/runtime/cache/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ func (c *cache) startSyncer(ctx context.Context) error {
c.syncQueue.ShutDown()
}()

syncLoopStarted := make(chan struct{})
go func() {
log.Info("Starting sync loop")
syncLoopStarted <- struct{}{}
for {
select {
case <-time.After(c.syncPeriod / 4):
Expand Down Expand Up @@ -79,6 +81,8 @@ func (c *cache) startSyncer(ctx context.Context) error {
wg.Wait()
}()

<-syncLoopStarted

if err := wait.PollUntilContextTimeout(ctx, 50*time.Millisecond, 5*time.Second, false, func(context.Context) (done bool, err error) {
if atomic.LoadInt64(&workers) < int64(c.syncConcurrency) {
return false, nil
Expand Down

0 comments on commit ddd99a1

Please sign in to comment.