示例#1
0
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
}
示例#2
0
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
}
示例#3
0
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):
	}
}
示例#4
0
// 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
}
示例#5
0
func TestAddZeroIsIgnored(t *testing.T) {
	limitgroup.NewLimitGroup(1).Add(0)
}
示例#6
0
func TestAddMoreThanLimit(t *testing.T) {
	defer ensure.PanicDeepEqual(t, "delta greater than limit")
	limitgroup.NewLimitGroup(1).Add(2)
}
示例#7
0
func TestAddNegativeMoreThanExpected(t *testing.T) {
	defer ensure.PanicDeepEqual(t, "trying to return more slots than acquired")
	limitgroup.NewLimitGroup(1).Add(-1)
}
示例#8
0
func TestDoneMoreThanPossible(t *testing.T) {
	defer ensure.PanicDeepEqual(t, "trying to return more slots than acquired")
	limitgroup.NewLimitGroup(1).Done()
}
示例#9
0
func TestZeroLimit(t *testing.T) {
	defer ensure.PanicDeepEqual(t, "zero is not a valid limit")
	limitgroup.NewLimitGroup(0)
}