mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-01-17 23:06:30 -05:00
Test case for access log
This commit is contained in:
parent
6d5d8b12cd
commit
a80093a727
@ -33,6 +33,11 @@ type accessLog struct {
|
|||||||
type fileAccessLogger struct {
|
type fileAccessLogger struct {
|
||||||
queue chan *accessLog
|
queue chan *accessLog
|
||||||
logger *log.Logger
|
logger *log.Logger
|
||||||
|
file *os.File
|
||||||
|
}
|
||||||
|
|
||||||
|
func (logger *fileAccessLogger) close() {
|
||||||
|
logger.file.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (logger *fileAccessLogger) Log(from, to string, status AccessStatus, reason string) {
|
func (logger *fileAccessLogger) Log(from, to string, status AccessStatus, reason string) {
|
||||||
@ -59,6 +64,7 @@ func newFileAccessLogger(path string) accessLogger {
|
|||||||
return &fileAccessLogger{
|
return &fileAccessLogger{
|
||||||
queue: make(chan *accessLog, 16),
|
queue: make(chan *accessLog, 16),
|
||||||
logger: log.New(file, "", log.Ldate|log.Ltime),
|
logger: log.New(file, "", log.Ldate|log.Ltime),
|
||||||
|
file: file,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
34
common/log/access_test.go
Normal file
34
common/log/access_test.go
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
package log
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/v2ray/v2ray-core/testing/unit"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestAccessLog(t *testing.T) {
|
||||||
|
assert := unit.Assert(t)
|
||||||
|
|
||||||
|
filename := "/tmp/test_access_log.log"
|
||||||
|
InitAccessLogger(filename)
|
||||||
|
_, err := os.Stat(filename)
|
||||||
|
assert.Error(err).IsNil()
|
||||||
|
|
||||||
|
Access("test_from", "test_to", AccessAccepted, "test_reason")
|
||||||
|
<-time.After(2 * time.Second)
|
||||||
|
|
||||||
|
accessLoggerInstance.(*fileAccessLogger).close()
|
||||||
|
accessLoggerInstance = &noOpAccessLogger{}
|
||||||
|
|
||||||
|
content, err := ioutil.ReadFile(filename)
|
||||||
|
assert.Error(err).IsNil()
|
||||||
|
|
||||||
|
assert.Bool(strings.Contains(string(content), "test_from")).IsTrue()
|
||||||
|
assert.Bool(strings.Contains(string(content), "test_to")).IsTrue()
|
||||||
|
assert.Bool(strings.Contains(string(content), "test_reason")).IsTrue()
|
||||||
|
assert.Bool(strings.Contains(string(content), "accepted")).IsTrue()
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user