github.com/ydb-platform/ydb-go-sdk/v3@v3.89.2/internal/xtest/clock.go (about)

     1  package xtest
     2  
     3  import (
     4  	"sync/atomic"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/jonboulle/clockwork"
     9  )
    10  
    11  // FastClock returns fake clock with very fast time speed advanced until end of test
    12  // the clock stops advance at end of test
    13  func FastClock(t testing.TB) clockwork.FakeClock {
    14  	clock := clockwork.NewFakeClock()
    15  	var needStop atomic.Bool
    16  	clockStopped := make(chan struct{})
    17  
    18  	go func() {
    19  		defer close(clockStopped)
    20  
    21  		for {
    22  			if needStop.Load() {
    23  				return
    24  			}
    25  
    26  			clock.Advance(time.Second)
    27  			time.Sleep(time.Microsecond)
    28  		}
    29  	}()
    30  
    31  	t.Cleanup(func() {
    32  		needStop.Store(true)
    33  		<-clockStopped
    34  	})
    35  
    36  	return clock
    37  }