Example #1
0
func TestUntilElapsedRetry(t *testing.T) {
	sleepCount := 0
	sleepFunc := func(d time.Duration) {
		sleepCount++
		time.Sleep(d)
	}
	sleepTime := time.Second * 1
	maxElapsedTime := time.Second * 3
	retry := NewUntilElapsedRetry(sleepFunc, sleepTime, maxElapsedTime)
	e := errors.New("test error")
	fnCount := 0
	fn := func() error {
		fnCount++
		return e
	}
	startTime := time.Now()
	err := retry.Do(fn)
	require.Equal(t, err, e)
	count := int(maxElapsedTime / sleepTime)
	require.Equal(t, fnCount, count)
	require.Equal(t, sleepCount, count)
	require.True(t, time.Since(startTime) > maxElapsedTime)
	fnCount = 0
	fn = func() error {
		fnCount++
		return nil
	}
	sleepCount = 0
	startTime = time.Now()
	err = retry.Do(fn)
	require.Equal(t, err, nil)
	require.Equal(t, fnCount, 1)
	require.Equal(t, sleepCount, 0)
	require.True(t, time.Since(startTime) < maxElapsedTime)
}
Example #2
0
func runTestHistogramGenerator(t *testing.T, g IntegerGenerator, times int, area int64) {
	for i := 0; i < times; i++ {
		last := g.NextInt()
		require.True(t, last <= area)
		require.Equal(t, g.LastInt(), last)
		str := g.NextString()
		v, err := strconv.ParseInt(str, 0, 64)
		require.Nil(t, err)
		require.True(t, v <= area)
		require.Equal(t, str, g.LastString())
	}
}
Example #3
0
func runTestZipfianGenerator(t *testing.T, f func(min, max int64) IntegerGenerator) {
	min := int64(1000)
	max := int64(2000)
	g := f(min, max)
	total := 10
	for i := 0; i < total; i++ {
		last := g.NextInt()
		require.True(t, last >= min && last <= max)
		require.Equal(t, last, g.LastInt())
		str := g.NextString()
		v, err := strconv.ParseInt(str, 0, 64)
		require.Nil(t, err)
		require.True(t, v >= min && v <= max)
	}
}
Example #4
0
func TestBoundedExponentialBackoffRetry(t *testing.T) {
	sleepCount := 0
	sleepFunc := func(d time.Duration) {
		sleepCount++
		time.Sleep(d)
	}
	times := 3
	maxTries := 2
	baseSleepTime := time.Second * 1
	maxSleepTime := time.Duration(int64(time.Second) * int64(times))
	retry := NewBoundedExponentialBackoffRetry(
		sleepFunc, uint32(maxTries), baseSleepTime, maxSleepTime)
	e := errors.New("test error")
	fnCount := 0
	fn := func() error {
		fnCount++
		return e
	}
	startTime := time.Now()
	err := retry.Do(fn)
	require.Equal(t, err, e)
	require.Equal(t, fnCount, maxTries)
	require.Equal(t, sleepCount, maxTries)
	require.True(t, time.Since(startTime) > (baseSleepTime*(1+2)))
	fnCount = 0
	fn = func() error {
		fnCount++
		return nil
	}
	sleepCount = 0
	err = retry.Do(fn)
	require.Equal(t, err, nil)
	require.Equal(t, fnCount, 1)
	require.Equal(t, sleepCount, 0)
}
Example #5
0
func TestErrorRetryDelay(t *testing.T) {
	sleepCount := 0
	sleepFunc := func(d time.Duration) {
		sleepCount++
		time.Sleep(d)
	}
	maxTries := 2
	delay := time.Second * 2
	maxDelay := time.Second * 3
	retry := NewErrorRetry().
		MaxTries(maxTries).
		SleepFunc(sleepFunc).
		Delay(delay).
		MaxDelay(maxDelay)
	fnCount := 0
	fn := func() error {
		fnCount++
		return ForceRetryError
	}
	startTime := time.Now()
	err := retry.Do(fn)
	require.Equal(t, err, RetryFailedError)
	require.Equal(t, fnCount, 2)
	require.Equal(t, sleepCount, 2)
	require.True(t, time.Since(startTime) > (delay+maxDelay))
}
func TestRotatingFileHandler_AppendWithoutBackup(t *testing.T) {
	defer Shutdown()
	// clean up the existing log file
	if FileExists(testFileName) {
		require.Nil(t, os.Remove(testFileName))
	}
	backupCount := uint32(0)
	handler, err := NewRotatingFileHandler(
		testFileName, os.O_APPEND, testRotateMaxBytes, backupCount)
	require.Nil(t, err)
	logger := GetLogger("rfile2")
	logger.AddHandler(handler)
	message := strings.Repeat("abcdefghij", 9) + "rstuvwxyz"
	size := uint64(len(message) + 1)
	totalSize := testRotateMaxBytes * (uint64(testRotateBackupCount) + 2)
	times := totalSize / size
	for i := uint64(0); i < times; i++ {
		logger.Errorf(message)
	}
	logger.RemoveHandler(handler)
	handler.Close()
	fileInfo, err := os.Stat(testFileName)
	require.Nil(t, err)
	require.True(t, uint64(fileInfo.Size()) > testRotateMaxBytes)
	require.Equal(t, totalSize, uint64(fileInfo.Size()))
	removeFile(t, testFileName)
}
Example #7
0
func TestUniformIntegerGenerator(t *testing.T) {
	lowerBound := int64(1000)
	upperBound := int64(2000)
	var g IntegerGenerator
	uig := NewUniformIntegerGenerator(lowerBound, upperBound)
	g = uig
	total := 10
	for i := 0; i < total; i++ {
		last := g.NextInt()
		require.True(t, last >= lowerBound && last <= upperBound)
		require.Equal(t, last, g.LastInt())
		str := g.NextString()
		v, err := strconv.ParseInt(str, 0, 64)
		require.Nil(t, err)
		require.True(t, v >= lowerBound && v <= upperBound)
		require.Equal(t, float64((lowerBound+upperBound)/2.0), g.Mean())
	}
}
func TestExponentGenerator(t *testing.T) {
	total := 100
	recordCount := int64(10000)
	percentile := float64(95)
	fraction := float64(0.8571428571)
	var g IntegerGenerator
	eg := NewExponentialGenerator(percentile, float64(recordCount)*fraction)
	g = eg
	for i := 0; i < total; i++ {
		last := g.NextInt()
		require.True(t, last >= recordCount)
		require.Equal(t, g.LastInt(), last)
		str := g.NextString()
		v, err := strconv.ParseInt(str, 0, 64)
		require.Nil(t, err)
		require.True(t, v >= recordCount)
		require.Equal(t, g.LastString(), str)
	}
}
func TestHotspotIntegerGenerator(t *testing.T) {
	lowerBound := int64(1000)
	upperBound := int64(2000)
	hotsetFraction := float64(0.2)
	hotOpnFraction := float64(0.99)
	var g IntegerGenerator
	hig := NewHotspotIntegerGenerator(lowerBound, upperBound, hotsetFraction, hotOpnFraction)
	g = hig
	last := g.NextInt()
	interval := upperBound - lowerBound
	hotsetHigh := lowerBound + int64(float64(interval)*hotsetFraction)
	require.True(t, last <= hotsetHigh)
	require.Equal(t, last, g.LastInt())
	str := g.NextString()
	last, err := strconv.ParseInt(str, 0, 64)
	require.Nil(t, err)
	require.True(t, last <= hotsetHigh)
	require.Equal(t, str, g.LastString())
}
Example #10
0
func TestErrorRetryDeadline(t *testing.T) {
	delay := time.Second * 1
	deadline := time.Second * 4
	retry := NewErrorRetry().Delay(delay).Deadline(deadline)
	fn := func() error {
		return ForceRetryError
	}
	startTime := time.Now()
	err := retry.Do(fn)
	require.Equal(t, err, RetryFailedError)
	require.True(t, time.Since(startTime) <= deadline)
}
Example #11
0
func TestErrorRetryJitter(t *testing.T) {
	maxTries := 2
	delay := time.Second * 1
	retry := NewErrorRetry().MaxTries(maxTries).Delay(delay).MaxJitter(0.5)
	fn := func() error {
		return ForceRetryError
	}
	startTime := time.Now()
	err := retry.Do(fn)
	require.Equal(t, err, RetryFailedError)
	require.True(
		t, int64(time.Since(startTime)) > (int64(delay)*int64(maxTries)))
}
Example #12
0
func TestErrorRetryBackoff(t *testing.T) {
	maxTries := 2
	delay := time.Second * 1
	backoff := 3
	retry := NewErrorRetry().
		MaxTries(maxTries).
		Delay(delay).
		Backoff(uint32(backoff))
	fn := func() error {
		return ForceRetryError
	}
	startTime := time.Now()
	err := retry.Do(fn)
	require.Equal(t, err, RetryFailedError)
	require.True(
		t, int64(time.Since(startTime)) > (int64(delay)*int64(1+backoff)))
}
Example #13
0
func TestDiscreteGenerator(t *testing.T) {
	var g Generator
	dg := NewDiscreteGenerator()
	g = dg
	startWeight := float64(1.0)
	total := 4
	for i := 0; i < total; i++ {
		dg.AddValue(startWeight, fmt.Sprintf("%g", startWeight+float64(i)))
	}
	for i := 0; i < total; i++ {
		n := g.NextString()
		v, err := strconv.ParseFloat(n, 64)
		require.Nil(t, err)
		require.True(t, v < startWeight+float64(total))
		require.Equal(t, n, g.LastString())
	}
}
func TestDatagramHandler(t *testing.T) {
	host := "127.0.0.1"
	port := uint16(8082)
	serverReceived := list.New()
	ch := make(chan int)
	_testSetupDatagramServer(t, host, port, serverReceived, ch)
	require.Equal(t, 0, serverReceived.Len())
	handler := NewDatagramHandler(host, port)
	logger := GetLogger("datagram")
	logger.AddHandler(handler)
	message := "test"
	logger.Errorf(message)
	handler.Close()
	<-ch
	require.Equal(t, 1, serverReceived.Len())
	m, ok := serverReceived.Front().Value.(string)
	require.True(t, ok)
	require.Equal(t, message, m)
}
func TestThriftHandler(t *testing.T) {
	host := "127.0.0.1"
	port := uint16(8082)
	serverReceived := list.New()
	ch := make(chan int, 1)
	server := _testSetupThriftServer(t, host, port, serverReceived, ch)
	require.Equal(t, 0, serverReceived.Len())
	// wait a little while for server to startup
	time.Sleep(time.Millisecond * 100)
	handler := NewThriftHandler(host, port)
	logger := GetLogger("thrift")
	logger.AddHandler(handler)
	message := "test"
	logger.Errorf(message)
	handler.Close()
	<-ch
	server.Stop()
	require.Equal(t, 1, serverReceived.Len())
	m, ok := serverReceived.Front().Value.(string)
	require.True(t, ok)
	require.Equal(t, message, m)
}
func checkFileSize(t *testing.T, filename string) {
	fileInfo, err := os.Stat(filename)
	require.Nil(t, err)
	require.True(t, fileInfo.Size() > 0)
	require.True(t, uint64(fileInfo.Size()) <= testRotateMaxBytes)
}
Example #17
0
func (self *MockHandler) HandleError(record *LogRecord, err error) {
	require.True(self.t, false, "should not be any error")
}