mirror of
https://github.com/kataras/iris.git
synced 2025-12-18 10:27:06 +00:00
accesslog: LockWriter (to lock unprotected writers) and Clock (for testing)
relative to: https://github.com/kataras/iris/issues/1601
This commit is contained in:
61
middleware/accesslog/accesslog_test.go
Normal file
61
middleware/accesslog/accesslog_test.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package accesslog
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris/v12/context"
|
||||
"github.com/kataras/iris/v12/core/memstore"
|
||||
)
|
||||
|
||||
func TestAccessLogPrint_Simple(t *testing.T) {
|
||||
t.Parallel()
|
||||
const goroutinesN = 42
|
||||
|
||||
w := new(bytes.Buffer)
|
||||
ac := New()
|
||||
ac.Writer = w
|
||||
ac.LockWriter = true
|
||||
ac.Clock = TClock(time.Time{})
|
||||
|
||||
var (
|
||||
expected string
|
||||
wg = new(sync.WaitGroup)
|
||||
)
|
||||
|
||||
for i := 0; i < goroutinesN; i++ {
|
||||
wg.Add(1)
|
||||
expected += "0001-01-01 00:00:00|1s|GET|/path_value?url_query=url_query_value|path_param=path_param_value url_query=url_query_value custom=custom_value|200|Incoming|Outcoming|\n"
|
||||
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
|
||||
ac.Print(
|
||||
nil,
|
||||
1*time.Second,
|
||||
ac.TimeFormat,
|
||||
200,
|
||||
"GET",
|
||||
"/path_value?url_query=url_query_value",
|
||||
"Incoming",
|
||||
"Outcoming",
|
||||
&context.RequestParams{
|
||||
Store: []memstore.Entry{
|
||||
{Key: "path_param", ValueRaw: "path_param_value"},
|
||||
},
|
||||
}, []memstore.StringEntry{
|
||||
{Key: "url_query", Value: "url_query_value"},
|
||||
}, []memstore.Entry{
|
||||
{Key: "custom", ValueRaw: "custom_value"},
|
||||
})
|
||||
}()
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
|
||||
if got := w.String(); expected != got {
|
||||
t.Fatalf("expected printed result to be:\n'%s'\n\nbut got:\n'%s'", expected, got)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user