當前位置: 首頁>>代碼示例>>Golang>>正文


Golang require.Nil函數代碼示例

本文整理匯總了Golang中github.com/hhkbp2/testify/require.Nil函數的典型用法代碼示例。如果您正苦於以下問題:Golang Nil函數的具體用法?Golang Nil怎麽用?Golang Nil使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了Nil函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: TestRotatingFileHandler_AppendWithoutBackup

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)
}
開發者ID:zzn01,項目名稱:go-logging-1,代碼行數:27,代碼來源:handler_rotating_file_test.go

示例2: cleanupLogFils

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
}
開發者ID:zzn01,項目名稱:go-logging-1,代碼行數:14,代碼來源:handler_timed_rotating_file_test.go

示例3: TestTimedRotatingFileHandler_WithBackup

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))
}
開發者ID:zzn01,項目名稱:go-logging-1,代碼行數:31,代碼來源:handler_timed_rotating_file_test.go

示例4: _testConfigLogger

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))
}
開發者ID:trigrass2,項目名稱:go-logging,代碼行數:16,代碼來源:config_test.go

示例5: TestRotatingFileHandler_TruncateWithBackup

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")
}
開發者ID:trigrass2,項目名稱:go-logging,代碼行數:27,代碼來源:handler_rotating_file_test.go

示例6: TestFileHandler

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)
}
開發者ID:zzn01,項目名稱:go-logging-1,代碼行數:17,代碼來源:handler_file_test.go

示例7: TestShutdown

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())
}
開發者ID:trigrass2,項目名稱:go-logging,代碼行數:11,代碼來源:init_test.go

示例8: runTestHistogramGenerator

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())
	}
}
開發者ID:hhkbp2,項目名稱:yabf,代碼行數:12,代碼來源:histogram_generator_test.go

示例9: _testSetupDatagramServer

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
	}()
}
開發者ID:zzn01,項目名稱:go-logging-1,代碼行數:21,代碼來源:handler_datagram_test.go

示例10: _testSetupSocketServer

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)
	}()
}
開發者ID:trigrass2,項目名稱:go-logging,代碼行數:21,代碼來源:handler_socket_test.go

示例11: TestFileHandler_Asctime

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)
}
開發者ID:zzn01,項目名稱:go-logging-1,代碼行數:21,代碼來源:handler_file_test.go

示例12: TestFileGenerator

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())
	}
}
開發者ID:hhkbp2,項目名稱:yabf,代碼行數:21,代碼來源:file_generator_test.go

示例13: TestSyslogHandler

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")
}
開發者ID:trigrass2,項目名稱:go-logging,代碼行數:13,代碼來源:handler_syslog_test.go

示例14: runTestZipfianGenerator

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)
	}
}
開發者ID:hhkbp2,項目名稱:yabf,代碼行數:15,代碼來源:zipfian_generator_test.go

示例15: TestLoggerLogToHandler

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())
}
開發者ID:trigrass2,項目名稱:go-logging,代碼行數:24,代碼來源:logger_test.go


注:本文中的github.com/hhkbp2/testify/require.Nil函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。