func TestNegativeAdd(t *testing.T) { lg := limitgroup.NewLimitGroup(1) lg.Add(1) fin := make(chan struct{}) go func() { defer close(fin) lg.Wait() }() lg.Add(-1) <-fin }
func TestLimitNotHit(t *testing.T) { lg := limitgroup.NewLimitGroup(2) lg.Add(1) lg.Add(1) fin := make(chan struct{}) go func() { defer close(fin) lg.Wait() }() lg.Done() lg.Done() <-fin }
func TestLimitHit(t *testing.T) { lg := limitgroup.NewLimitGroup(1) lg.Add(1) fin := make(chan struct{}) go func() { defer close(fin) lg.Add(1) }() select { case <-fin: t.Fatal("should not get here") case <-time.After(10 * time.Millisecond): } }
// Start the background worker goroutines and get ready for accepting requests. func (c *Client) Start() error { if int64(c.BatchTimeout) == 0 && c.MaxBatchSize == 0 { return errZeroBoth } if c.MaxConcurrentBatches == 0 { c.workGroup = &sync.WaitGroup{} } else { c.workGroup = limitgroup.NewLimitGroup(c.MaxConcurrentBatches + 1) } c.Work = make(chan interface{}, c.PendingWorkCapacity) c.workGroup.Add(1) // this is the worker itself go c.worker() return nil }
func TestAddZeroIsIgnored(t *testing.T) { limitgroup.NewLimitGroup(1).Add(0) }
func TestAddMoreThanLimit(t *testing.T) { defer ensure.PanicDeepEqual(t, "delta greater than limit") limitgroup.NewLimitGroup(1).Add(2) }
func TestAddNegativeMoreThanExpected(t *testing.T) { defer ensure.PanicDeepEqual(t, "trying to return more slots than acquired") limitgroup.NewLimitGroup(1).Add(-1) }
func TestDoneMoreThanPossible(t *testing.T) { defer ensure.PanicDeepEqual(t, "trying to return more slots than acquired") limitgroup.NewLimitGroup(1).Done() }
func TestZeroLimit(t *testing.T) { defer ensure.PanicDeepEqual(t, "zero is not a valid limit") limitgroup.NewLimitGroup(0) }