コード例 #1
0
ファイル: retry_test.go プロジェクト: trigrass2/go-logging
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))
}
コード例 #2
0
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,
		testBufferSize,
		testBufferFlushTime,
		testInputChanSize,
		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
	require.Equal(t, totalSize, size*times)
	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)
}
コード例 #3
0
ファイル: retry_test.go プロジェクト: trigrass2/go-logging
func TestErrorRetryErrors(t *testing.T) {
	sleepCount := 0
	sleepFunc := func(d time.Duration) {
		sleepCount++
	}
	e1 := errors.New("test error 1")
	e2 := errors.New("test error 2")
	e3 := errors.New("test error 3")
	retry := NewErrorRetry().
		SleepFunc(sleepFunc).
		Delay(time.Second).
		OnError(e1).
		OnError(e2)
	triesBeforeSuccess := 3
	fnCount := 0
	fn := func() error {
		fnCount++
		if fnCount > triesBeforeSuccess {
			return e3
		}
		switch {
		case (fnCount / 2) == 0:
			return e1
		default:
			return e2
		}
	}
	err := retry.Do(fn)
	require.Equal(t, err, e3)
	require.Equal(t, sleepCount, triesBeforeSuccess)
	require.Equal(t, fnCount, triesBeforeSuccess+1)
}
コード例 #4
0
ファイル: utils_test.go プロジェクト: hhkbp2/yabf
func TestRandomBytes(t *testing.T) {
	length := int64(100)
	b1 := RandomBytes(length)
	require.Equal(t, length, int64(len(b1)))
	b2 := RandomBytes(length)
	require.Equal(t, length, int64(len(b2)))
	require.NotEqual(t, b1, b2)
}
コード例 #5
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())
	}
}
コード例 #6
0
ファイル: utils_test.go プロジェクト: hhkbp2/yabf
func TestToTime(t *testing.T) {
	millisecond := int64(12345)
	nanosecond := MillisecondToNanosecond(millisecond)
	require.Equal(t, millisecond*1000*1000, nanosecond)
	second := MillisecondToSecond(millisecond)
	require.Equal(t, millisecond/1000, second)
	v := SecondToNanosecond(second)
	require.Equal(t, second*1000*1000*1000, v)
	v = NanosecondToMicrosecond(nanosecond)
	require.Equal(t, nanosecond/1000, v)
	v = NanosecondToMillisecond(nanosecond)
	require.Equal(t, nanosecond/1000/1000, v)
}
コード例 #7
0
ファイル: counter_generator_test.go プロジェクト: hhkbp2/yabf
func TestCounterGenerator(t *testing.T) {
	value := int64(100)
	var g IntegerGenerator
	g = NewCounterGenerator(value)
	require.Equal(t, value-1, g.LastInt())
	for i := int64(0); i < 5; i++ {
		require.Equal(t, value+i, g.NextInt())
		require.Equal(t, value+i, g.LastInt())
	}
	for i := int64(5); i < 5; i++ {
		require.Equal(t, fmt.Sprintf("%d", value+i), g.NextString())
		require.Equal(t, fmt.Sprintf("%d", value+i), g.LastString())
	}
	require.Panics(t, func() { g.Mean() })
}
コード例 #8
0
ファイル: utils_test.go プロジェクト: hhkbp2/yabf
func TestProperties(t *testing.T) {
	k := "key"
	v := "value"
	p := NewProperties()
	p.Add(k, v)
	x := p.Get(k)
	require.Equal(t, v, x)
	x = p.GetDefault(k, "other")
	require.Equal(t, v, x)
	k1 := "a"
	v1 := "b"
	p2 := map[string]string{k1: v1}
	p.Merge(p2)
	z := p.Get(k1)
	require.Equal(t, v1, z)
}
コード例 #9
0
func TestTimedRotatingFileHandler_WithBackup(t *testing.T) {
	defer Shutdown()
	cleanupLogFils(t, testFileName)
	when := "S"
	format := "%Y-%m-%d_%H-%M-%S"
	interval := 2
	handler, err := NewTimedRotatingFileHandler(
		testFileName, when, uint32(interval), testRotateBackupCount, false)
	require.Nil(t, err)
	logger := GetLogger("trfile")
	logger.AddHandler(handler)
	message := "test"
	lastMessage := "last message"
	times := make([]time.Time, 0, testRotateBackupCount)
	for i := uint32(0); i < testRotateBackupCount+1; i++ {
		logger.Errorf(message)
		if i > 0 {
			times = append(times, time.Now())
		}
		time.Sleep(time.Duration(int64(time.Second) * int64(interval)))
	}
	logger.Errorf(lastMessage)
	logger.RemoveHandler(handler)
	handler.Close()
	for i := uint32(0); i < testRotateBackupCount; i++ {
		suffix := strftime.Format(format, times[i])
		checkFileContent(t, testFileName+"."+suffix, message+"\n")
	}
	checkFileContent(t, testFileName, lastMessage+"\n")
	require.Equal(t, 2, cleanupLogFils(t, testFileName))
}
コード例 #10
0
ファイル: uniform_generator_test.go プロジェクト: hhkbp2/yabf
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())
	}
}
コード例 #11
0
func TestNullHandler(t *testing.T) {
	handler := NewNullHandler()
	logger := GetLogger("null")
	logger.AddHandler(handler)
	require.Equal(t, 1, len(logger.GetHandlers()))
	message := "test"
	logger.Debugf(message)
	logger.Fatalf(message)
}
コード例 #12
0
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())
}
コード例 #13
0
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)
}
コード例 #14
0
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)
	}
}
コード例 #15
0
ファイル: init_test.go プロジェクト: trigrass2/go-logging
func TestShutdown(t *testing.T) {
	defer Shutdown()
	handler := NewMockHandler(t)
	logger := GetLogger("a")
	logger.AddHandler(handler)
	message := "abcd"
	logger.Errorf(message)
	record, err := handler.GetEmitOnTimeout(time.Second * 0)
	require.Nil(t, err)
	require.Equal(t, message, record.GetMessage())
}
コード例 #16
0
ファイル: retry_test.go プロジェクト: trigrass2/go-logging
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)
}
コード例 #17
0
ファイル: file_generator_test.go プロジェクト: hhkbp2/yabf
func TestFileGenerator(t *testing.T) {
	filename := "file_generator.data"
	var g Generator
	fg, err := NewFileGenerator(filename)
	require.Nil(t, err)
	g = fg
	total := 5
	for i := 1; i < total; i++ {
		last := g.NextString()
		require.Equal(t, last, fmt.Sprintf("%d", i))
		require.Equal(t, last, g.LastString())
	}
	err = fg.ReloadFile()
	require.Nil(t, err)
	defer fg.Close()
	for i := 1; i < total; i++ {
		last := g.NextString()
		require.Equal(t, last, fmt.Sprintf("%d", i))
		require.Equal(t, last, g.LastString())
	}
}
コード例 #18
0
ファイル: retry_test.go プロジェクト: trigrass2/go-logging
func TestNTimesRetry(t *testing.T) {
	sleepCount := 0
	sleepFunc := func(d time.Duration) {
		sleepCount++
	}
	maxTimes := 3
	sleepTime := time.Second * 1
	retry := NewNTimesRetry(sleepFunc, uint32(maxTimes), sleepTime)
	e := errors.New("test error")
	fnCount := 0
	fn := func() error {
		fnCount++
		return e
	}
	err := retry.Do(fn)
	require.Equal(t, err, e)
	require.Equal(t, fnCount, maxTimes)
	require.Equal(t, sleepCount, maxTimes)
	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)
}
コード例 #19
0
ファイル: retry_test.go プロジェクト: trigrass2/go-logging
func TestExponentialBackoffRetry(t *testing.T) {
	sleepCount := 0
	sleepFunc := func(d time.Duration) {
		sleepCount++
		time.Sleep(d)
	}
	times := 3
	baseSleepTime := time.Second * 1
	maxSleepTime := time.Duration(int64(time.Second) * int64(times))
	retry := NewExponentialBackoffRetry(sleepFunc, baseSleepTime, maxSleepTime)
	e := errors.New("test error")
	fnCount := 0
	fn := func() error {
		fnCount++
		if fnCount > times {
			return nil
		}
		return e
	}
	err := retry.Do(fn)
	require.Equal(t, err, nil)
	require.Equal(t, fnCount, times+1)
	require.Equal(t, sleepCount, times)
	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)
}
コード例 #20
0
ファイル: retry_test.go プロジェクト: trigrass2/go-logging
func TestErrorRetryMaxTries(t *testing.T) {
	sleepCount := 0
	sleepFunc := func(d time.Duration) {
		sleepCount++
	}
	maxTries := 3
	retry := NewErrorRetry().MaxTries(maxTries).SleepFunc(sleepFunc)
	fnCount := 0
	fn := func() error {
		fnCount++
		return ForceRetryError
	}
	err := retry.Do(fn)
	require.Equal(t, err, RetryFailedError)
	require.Equal(t, fnCount, maxTries)
	require.Equal(t, sleepCount, maxTries)
	sleepCount = 0
	fnCount = 0
	fn = func() error {
		fnCount++
		return nil
	}
	err = retry.Do(fn)
	require.Equal(t, err, nil)
	require.Equal(t, fnCount, 1)
	require.Equal(t, sleepCount, 0)
}
コード例 #21
0
ファイル: retry_test.go プロジェクト: trigrass2/go-logging
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)
}
コード例 #22
0
ファイル: retry_test.go プロジェクト: trigrass2/go-logging
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)
}
コード例 #23
0
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)
}
コード例 #24
0
ファイル: retry_test.go プロジェクト: trigrass2/go-logging
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)))
}
コード例 #25
0
ファイル: logger_test.go プロジェクト: trigrass2/go-logging
func TestLoggerLogToHandler(t *testing.T) {
	defer Shutdown()
	logger := GetLogger("b")
	logger.SetLevel(LevelDebug)
	require.Equal(t, 0, len(logger.GetHandlers()))
	handler := NewMockHandler(t)
	logger.AddHandler(handler)
	require.Equal(t, 1, len(logger.GetHandlers()))

	// test format log
	message := "abcd"
	format := "msg: %s"
	logger.Debugf(format, message)
	record, err := handler.GetEmitOnTimeout(time.Second * 0)
	require.Nil(t, err)
	require.Equal(t, fmt.Sprintf(format, message), record.GetMessage())

	// test default format for operand
	testError := fmt.Errorf("Example error")
	logger.Debug(testError)
	record, err = handler.GetEmitOnTimeout(time.Second * 0)
	require.Nil(t, err)
	require.Equal(t, testError.Error(), record.GetMessage())
}
コード例 #26
0
ファイル: zipfian_generator_test.go プロジェクト: hhkbp2/yabf
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)
	}
}
コード例 #27
0
ファイル: config_test.go プロジェクト: trigrass2/go-logging
func _testConfigLogger(t *testing.T) {
	logger1 := GetLogger("a.b")
	logger2 := GetLogger("a")
	message := "xxxxyyy"
	logger1.Info(message)
	logger1.Error(message)
	logger2.Debug(message)
	logger2.Error(message)
	Shutdown()
	// open the log file and check its content
	content, err := ioutil.ReadFile(testFileName)
	require.Nil(t, err)
	require.Equal(t, strings.Repeat(message+"\n", 2), string(content))
	// clean up the log file
	require.Nil(t, os.Remove(testFileName))
}
コード例 #28
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())
	}
}
コード例 #29
0
ファイル: retry_test.go プロジェクト: trigrass2/go-logging
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)))
}
コード例 #30
0
func TestFileHandler(t *testing.T) {
	handler, err := NewFileHandler(testFileName, testFileMode)
	require.Nil(t, err)
	logger := GetLogger("file1")
	logger.AddHandler(handler)
	message := "test"
	logger.Errorf(message)
	logger.RemoveHandler(handler)
	handler.Close()
	// open the log file and check its content equals to message
	// then clean it up.
	content, err := ioutil.ReadFile(testFileName)
	require.Nil(t, err)
	require.Equal(t, message+"\n", string(content))
	err = os.Remove(testFileName)
	require.Nil(t, err)
}