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)
}
func cleanupLogFils(t *testing.T, basepath string) int {
	dirName, baseName := filepath.Split(basepath)
	fileInfos, err := ioutil.ReadDir(dirName)
	require.Nil(t, err)
	count := 0
	for _, info := range fileInfos {
		name := info.Name()
		if strings.HasPrefix(name, baseName) {
			require.Nil(t, os.Remove(filepath.Join(dirName, name)))
			count += 1
		}
	}
	return count
}
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))
}
Beispiel #4
0
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))
}
func TestRotatingFileHandler_TruncateWithBackup(t *testing.T) {
	defer Shutdown()
	handler, err := NewRotatingFileHandler(
		testFileName,
		testFileMode,
		testBufferSize,
		testBufferFlushTime,
		testInputChanSize,
		testRotateMaxBytes,
		testRotateBackupCount)
	require.Nil(t, err)
	logger := GetLogger("rfile1")
	logger.AddHandler(handler)
	// every message is 99 byte, and \n
	message := strings.Repeat("abcdefghij", 9) + "rstuvwxyz"
	size := uint64(len(message) + 1)
	total := testRotateMaxBytes * (uint64(testRotateBackupCount) + 2) / size
	for i := uint64(0); i < total; i++ {
		logger.Errorf(message)
	}
	logger.RemoveHandler(handler)
	handler.Close()
	checkFileSize(t, testFileName)
	checkFileSize(t, testFileName+".1")
	removeFile(t, testFileName)
	removeFile(t, testFileName+".1")
}
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)
}
Beispiel #7
0
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())
}
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())
	}
}
func _testSetupDatagramServer(
	t *testing.T, host string, port uint16, received *list.List, ch chan int) {

	address, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", host, port))
	require.Nil(t, err)
	conn, err := net.ListenUDP("udp", address)
	require.Nil(t, err)
	go func() {
		bin := make([]byte, 1024)
		n, _, err := conn.ReadFromUDP(bin)
		require.Nil(t, err)
		defer conn.Close()
		buf := bytes.NewBuffer(bin[:n])
		decoder := gob.NewDecoder(buf)
		var record SocketLogRecord
		err = decoder.Decode(&record)
		require.Nil(t, err)
		received.PushBack(*record.Message)
		ch <- 1
	}()
}
func _testSetupSocketServer(
	t *testing.T, host string, port uint16, received *list.List, ch chan int) {

	address := fmt.Sprintf("%s:%d", host, port)
	listener, err := net.Listen("tcp", address)
	require.Nil(t, err)
	go func() {
		conn, err := listener.Accept()
		require.Nil(t, err)
		defer listener.Close()
		go func(conn net.Conn) {
			defer conn.Close()
			decoder := gob.NewDecoder(conn)
			var record SocketLogRecord
			err := decoder.Decode(&record)
			require.Nil(t, err)
			received.PushBack(record.Message)
			ch <- 1
		}(conn)
	}()
}
Beispiel #11
0
func TestFileHandler_Asctime(t *testing.T) {
	handler, err := NewFileHandler(testFileName, testFileMode)
	formatter := NewStandardFormatter(
		"%(asctime)s %(message)s",
		"%Y-%m-%d %H:%M:%S %3n")
	handler.SetFormatter(formatter)
	require.Nil(t, err)
	logger := GetLogger("file2")
	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, 24+len(message)+1, len(content))
	err = os.Remove(testFileName)
	require.Nil(t, err)
}
Beispiel #12
0
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())
	}
}
func TestSyslogHandler(t *testing.T) {
	defer Shutdown()
	handler, err := NewSyslogHandler(
		syslog.LOG_USER|syslog.LOG_DEBUG,
		"atag")
	require.Nil(t, err)
	logger := GetLogger("syslog")
	logger.SetLevel(LevelDebug)
	logger.AddHandler(handler)
	prefix := "test syslog handler "
	logger.Debugf(prefix + "Debug() a message")
	logger.Errorf(prefix + "Error() a message")
}
Beispiel #14
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)
	}
}
Beispiel #15
0
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())
}
Beispiel #16
0
func TestHistogramGenerator(t *testing.T) {
	buckets := []int64{1, 2, 3, 4}
	blockSize := int64(1)
	var area int64
	for i := 0; i < len(buckets); i++ {
		area += buckets[i] * blockSize
	}
	hg := NewHistogramGenerator(buckets, blockSize)
	times := 10
	runTestHistogramGenerator(t, hg, times, area)
	filename := "histogram_generator.data"
	hg, err := NewHistogramGeneratorFromFile(filename)
	require.Nil(t, err)
	runTestHistogramGenerator(t, hg, times, area)
}
Beispiel #17
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())
	}
}
Beispiel #18
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())
}
func _testSetupThriftServer(
	t *testing.T, host string, port uint16, received *list.List, ch chan int) thrift.TServer {

	handler := NewTestThriftServerHandler(received, ch)
	processor := gen.NewThriftLoggingServiceProcessor(handler)

	address := fmt.Sprintf("%s:%d", host, port)
	serverTransport, err := thrift.NewTServerSocket(address)
	require.Nil(t, err)
	transportFactory := thrift.NewTFramedTransportFactory(thrift.NewTTransportFactory())
	protocolFactory := thrift.NewTBinaryProtocolFactoryDefault()
	server := thrift.NewTSimpleServer4(
		processor,
		serverTransport,
		transportFactory,
		protocolFactory)
	go func() {
		server.Serve()
	}()
	return server
}
func removeFile(t *testing.T, filename string) {
	err := os.Remove(filename)
	require.Nil(t, err)
}
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)
}
func checkFileContent(t *testing.T, file, content string) {
	c, err := ioutil.ReadFile(file)
	require.Nil(t, err)
	require.Equal(t, content, string(c))
}
Beispiel #25
0
func TestApplyJsonConfigFile(t *testing.T) {
	file := "./config_example.json"
	require.Nil(t, ApplyJsonConfigFile(file))
	_testConfigLogger(t)
}
Beispiel #26
0
func TestApplyYAMLConfigFile(t *testing.T) {
	file := "./config_example.yml"
	require.Nil(t, ApplyYAMLConfigFile(file))
	_testConfigLogger(t)
}