@ -10,6 +10,7 @@ import (
"fmt"
"log"
"testing"
"time"
)
func TestFuncWriter ( t * testing . T ) {
@ -23,22 +24,21 @@ func TestStdLogger(t *testing.T) {
lg . Printf ( "plumbed through" )
}
func TestRateLimiter ( t * testing . T ) {
// Testing function. args[0] should indicate what should
logTester := func ( want [ ] string ) Logf {
i := 0
return func ( format string , args ... interface { } ) {
got := fmt . Sprintf ( format , args ... )
if i >= len ( want ) {
t . Fatalf ( "Logging continued past end of expected input: %s" , got )
}
if got != want [ i ] {
t . Fatalf ( "wanted: %s \n got: %s" , want [ i ] , got )
}
i ++
func logTester ( want [ ] string , t * testing . T , i * int ) Logf {
return func ( format string , args ... interface { } ) {
got := fmt . Sprintf ( format , args ... )
if * i >= len ( want ) {
t . Fatalf ( "Logging continued past end of expected input: %s" , got )
}
if got != want [ * i ] {
t . Fatalf ( "wanted: %s \n got: %s" , want [ * i ] , got )
}
t . Log ( got )
* i ++
}
}
func TestRateLimiter ( t * testing . T ) {
want := [ ] string {
"boring string with constant formatting (constant)" ,
@ -51,7 +51,9 @@ func TestRateLimiter(t *testing.T) {
"4 shouldn't get filtered." ,
}
lg := RateLimitedFn ( logTester ( want ) , 1 , 2 , 50 )
testsRun := 0
lgtest := logTester ( want , t , & testsRun )
lg := RateLimitedFn ( lgtest , 1 * time . Second , 2 , 50 )
var prefixed Logf
for i := 0 ; i < 10 ; i ++ {
lg ( "boring string with constant formatting %s" , "(constant)" )
@ -62,7 +64,46 @@ func TestRateLimiter(t *testing.T) {
prefixed ( " shouldn't get filtered." )
}
}
if testsRun < len ( want ) {
t . Fatalf ( "Tests after %s weren't logged." , want [ testsRun ] )
}
}
func testTimer ( d time . Duration ) func ( ) time . Time {
timeNow := time . Now ( )
return func ( ) time . Time {
timeNow = timeNow . Add ( d )
return timeNow
}
}
func TestLogOnChange ( t * testing . T ) {
want := [ ] string {
"1 2 3 4 5 6" ,
"1 2 3 4 5 6" ,
"1 2 3 4 5 7" ,
"1 2 3 4 5" ,
"1 2 3 4 5 6 7" ,
}
timeNow := testTimer ( 1 * time . Second )
testsRun := 0
lgtest := logTester ( want , t , & testsRun )
lg := LogOnChange ( lgtest , 5 * time . Second , timeNow )
for i := 0 ; i < 10 ; i ++ {
lg ( "%s" , "1 2 3 4 5 6" )
}
lg ( "1 2 3 4 5 7" )
lg ( "1 2 3 4 5" )
lg ( "1 2 3 4 5" )
lg ( "1 2 3 4 5 6 7" )
if testsRun < len ( want ) {
t . Fatalf ( "'Wanted' lines including and after [%s] weren't logged." , want [ testsRun ] )
}
}
func TestArgWriter ( t * testing . T ) {