Browse Source

Renamed ticker to cond

Viktor Sokolov 2 months ago
parent
commit
11edf5bd93
3 changed files with 44 additions and 44 deletions
  1. 3 3
      asyncbuffer/buffer.go
  2. 12 12
      asyncbuffer/cond.go
  3. 29 29
      asyncbuffer/cond_test.go

+ 3 - 3
asyncbuffer/buffer.go

@@ -64,8 +64,8 @@ type AsyncBuffer struct {
 	finished atomic.Bool // Indicates that the buffer has finished reading
 	closed   atomic.Bool // Indicates that the buffer was closed
 
-	paused *Latch  // Paused buffer does not read data beyond threshold
-	ticker *Ticker // Ticker that signals when a new chunk is ready
+	paused *Latch // Paused buffer does not read data beyond threshold
+	ticker *Cond  // Ticker that signals when a new chunk is ready
 }
 
 // FromReadCloser creates a new AsyncBuffer that reads from the given io.Reader in background
@@ -73,7 +73,7 @@ func FromReader(r io.ReadCloser) *AsyncBuffer {
 	ab := &AsyncBuffer{
 		r:      r,
 		paused: NewLatch(),
-		ticker: NewTicker(),
+		ticker: NewCond(),
 	}
 
 	go ab.readChunks()

+ 12 - 12
asyncbuffer/ticker.go → asyncbuffer/cond.go

@@ -4,36 +4,36 @@ import (
 	"sync"
 )
 
-type tickCh = chan struct{}
+type condCh = chan struct{}
 
-// Ticker signals that an event has occurred to a multiple waiters.
-type Ticker struct {
+// Cond signals that an event has occurred to a multiple waiters.
+type Cond struct {
 	_         noCopy
 	mu        sync.RWMutex
-	ch        tickCh
+	ch        condCh
 	closeOnce sync.Once
 }
 
-// NewTicker creates a new Ticker instance with an initialized channel.
-func NewTicker() *Ticker {
-	return &Ticker{
-		ch: make(tickCh),
+// NewCond creates a new Ticker instance with an initialized channel.
+func NewCond() *Cond {
+	return &Cond{
+		ch: make(condCh),
 	}
 }
 
 // Tick signals that an event has occurred by closing the channel.
-func (t *Ticker) Tick() {
+func (t *Cond) Tick() {
 	t.mu.Lock()
 	defer t.mu.Unlock()
 
 	if t.ch != nil {
 		close(t.ch)
-		t.ch = make(tickCh)
+		t.ch = make(condCh)
 	}
 }
 
 // Wait blocks until the channel is closed, indicating that an event has occurred.
-func (t *Ticker) Wait() {
+func (t *Cond) Wait() {
 	t.mu.RLock()
 	ch := t.ch
 	t.mu.RUnlock()
@@ -46,7 +46,7 @@ func (t *Ticker) Wait() {
 }
 
 // Close closes the ticker channel and prevents further ticks.
-func (t *Ticker) Close() {
+func (t *Cond) Close() {
 	t.closeOnce.Do(func() {
 		t.mu.Lock()
 		defer t.mu.Unlock()

+ 29 - 29
asyncbuffer/ticker_test.go → asyncbuffer/cond_test.go

@@ -8,36 +8,36 @@ import (
 	"github.com/stretchr/testify/suite"
 )
 
-type TickerTestSuite struct {
+type TestCondSuite struct {
 	suite.Suite
-	ticker *Ticker
+	cond *Cond
 }
 
-func (s *TickerTestSuite) SetupTest() {
-	s.ticker = NewTicker()
+func (s *TestCondSuite) SetupTest() {
+	s.cond = NewCond()
 }
 
-func (s *TickerTestSuite) TeardownTest() {
-	if s.ticker != nil {
-		s.ticker.Close()
+func (s *TestCondSuite) TeardownTest() {
+	if s.cond != nil {
+		s.cond.Close()
 	}
 }
 
-// TestBasicWaitAndTick tests the basic functionality of the Ticker
-func (s *TickerTestSuite) TestBasicWaitAndTick() {
+// TestBasicWaitAndTick tests the basic functionality of the Cond
+func (s *TestCondSuite) TestBasicWaitAndTick() {
 	done := make(chan struct{})
 
-	ch := s.ticker.ch
+	ch := s.cond.ch
 
 	// Start a goroutine that will tick after a short delay
 	go func() {
 		time.Sleep(50 * time.Millisecond)
-		s.ticker.Tick()
+		s.cond.Tick()
 	}()
 
 	// Start a goroutine that will wait for the tick
 	go func() {
-		s.ticker.Wait()
+		s.cond.Wait()
 		close(done)
 	}()
 
@@ -51,11 +51,11 @@ func (s *TickerTestSuite) TestBasicWaitAndTick() {
 	}, 100*time.Millisecond, 10*time.Millisecond)
 
 	// Means that and old channel was closed and a new one has been created
-	s.Require().NotEqual(ch, s.ticker.ch)
+	s.Require().NotEqual(ch, s.cond.ch)
 }
 
 // TestWaitMultipleWaiters tests that multiple waiters can be unblocked by a single tick
-func (s *TickerTestSuite) TestWaitMultipleWaiters() {
+func (s *TestCondSuite) TestWaitMultipleWaiters() {
 	const numWaiters = 10
 
 	var wg sync.WaitGroup
@@ -69,7 +69,7 @@ func (s *TickerTestSuite) TestWaitMultipleWaiters() {
 		go func(index int) {
 			defer wg.Done()
 			startWg.Done() // Signal that this goroutine is ready
-			s.ticker.Wait()
+			s.cond.Wait()
 			results[index] = true
 		}(i)
 	}
@@ -80,7 +80,7 @@ func (s *TickerTestSuite) TestWaitMultipleWaiters() {
 	// Wait for all waiters to complete
 	done := make(chan struct{})
 	go func() {
-		s.ticker.Tick() // Signal that execution can proceed
+		s.cond.Tick() // Signal that execution can proceed
 		wg.Wait()
 		close(done)
 	}()
@@ -100,17 +100,17 @@ func (s *TickerTestSuite) TestWaitMultipleWaiters() {
 	}
 }
 
-// TestClose tests the behavior of the Ticker when closed
-func (s *TickerTestSuite) TestClose() {
-	s.ticker.Close()
-	s.ticker.Close() // Should not panic
-	s.ticker.Wait()  // Should eventually return
-	s.ticker.Tick()  // Should not panic
+// TestClose tests the behavior of the Cond when closed
+func (s *TestCondSuite) TestClose() {
+	s.cond.Close()
+	s.cond.Close() // Should not panic
+	s.cond.Wait()  // Should eventually return
+	s.cond.Tick()  // Should not panic
 
-	s.Require().Nil(s.ticker.ch)
+	s.Require().Nil(s.cond.ch)
 }
 
-func (s *TickerTestSuite) TestRapidTicksAndWaits() {
+func (s *TestCondSuite) TestRapidTicksAndWaits() {
 	const iterations = 1000
 
 	var wg sync.WaitGroup
@@ -120,10 +120,10 @@ func (s *TickerTestSuite) TestRapidTicksAndWaits() {
 	go func() {
 		defer wg.Done()
 		for range iterations {
-			s.ticker.Tick()
+			s.cond.Tick()
 			time.Sleep(time.Microsecond)
 		}
-		s.ticker.Close() // Close after all ticks
+		s.cond.Close() // Close after all ticks
 	}()
 
 	// Start multiple waiters
@@ -132,7 +132,7 @@ func (s *TickerTestSuite) TestRapidTicksAndWaits() {
 		go func() {
 			defer wg.Done()
 			for range iterations / 10 {
-				s.ticker.Wait()
+				s.cond.Wait()
 			}
 		}()
 	}
@@ -153,6 +153,6 @@ func (s *TickerTestSuite) TestRapidTicksAndWaits() {
 	}, 100*time.Millisecond, 10*time.Millisecond)
 }
 
-func TestTicker(t *testing.T) {
-	suite.Run(t, new(TickerTestSuite))
+func TestCond(t *testing.T) {
+	suite.Run(t, new(TestCondSuite))
 }