github.com/tilt-dev/tilt@v0.33.15-0.20240515162809-0a22ed45d8a0/internal/controllers/core/filewatch/fsevent/fake.go (about)

     1  package fsevent
     2  
     3  import (
     4  	"sync"
     5  	"testing"
     6  	"time"
     7  )
     8  
     9  type FakeTimerMaker struct {
    10  	RestTimerLock *sync.Mutex
    11  	MaxTimerLock  *sync.Mutex
    12  	t             *testing.T
    13  }
    14  
    15  func (f FakeTimerMaker) Maker() TimerMaker {
    16  	return func(d time.Duration) <-chan time.Time {
    17  		var lock *sync.Mutex
    18  		// we have separate locks for the separate uses of timer so that tests can control the timers independently
    19  		switch d {
    20  		case BufferMinRestDuration:
    21  			lock = f.RestTimerLock
    22  		case BufferMaxDuration:
    23  			lock = f.MaxTimerLock
    24  		default:
    25  			// if you hit this, someone (you!?) might have added a new timer with a new duration, and you probably
    26  			// want to add a case above
    27  			f.t.Error("makeTimer called on unsupported duration")
    28  		}
    29  		ret := make(chan time.Time, 1)
    30  		go func() {
    31  			lock.Lock()
    32  			ret <- time.Unix(0, 0)
    33  			lock.Unlock()
    34  			close(ret)
    35  		}()
    36  		return ret
    37  	}
    38  }
    39  
    40  func MakeFakeTimerMaker(t *testing.T) FakeTimerMaker {
    41  	restTimerLock := new(sync.Mutex)
    42  	maxTimerLock := new(sync.Mutex)
    43  
    44  	return FakeTimerMaker{restTimerLock, maxTimerLock, t}
    45  }