prysm-pulse/shared/testutil/log.go
Nishant Das f46ee335bd
Allow Sync Service to Query State Of Network (#787)
* adding proto messages

* adding fields

* adding new service

* new changes

* checking in file from syncFix

* fixing test

* add test

* adding to node

* goimports

* adding query service to sync services

* sync working

* finally works

* fix test

* lint

* fix build

* fix test
2018-11-19 09:59:11 +08:00

66 lines
1.6 KiB
Go

// Package testutil defines the testing utils such as asserting logs.
package testutil
import (
"strings"
"testing"
"time"
"github.com/sirupsen/logrus/hooks/test"
)
// AssertLogsContain checks that the desired string is a subset of the current log output.
// Set exitOnFail to true to immediately exit the test on failure
func AssertLogsContain(t *testing.T, hook *test.Hook, want string) {
assertLogs(t, hook, want, true)
}
// AssertLogsDoNotContain is the inverse check of AssertLogsContain
func AssertLogsDoNotContain(t *testing.T, hook *test.Hook, want string) {
assertLogs(t, hook, want, false)
}
func assertLogs(t *testing.T, hook *test.Hook, want string, flag bool) {
t.Logf("scanning for: %s", want)
entries := hook.AllEntries()
match := false
for _, e := range entries {
if strings.Contains(e.Message, want) {
match = true
}
t.Logf("log: %s", e.Message)
}
if flag && !match {
t.Fatalf("log not found: %s", want)
} else if !flag && match {
t.Fatalf("unwanted log found: %s", want)
}
}
// WaitForLog waits for the desired string to appear the logs within a
// time period. If it does not appear within the limit, the function
// will throw an error.
func WaitForLog(t *testing.T, hook *test.Hook, want string) {
t.Logf("waiting for: %s", want)
match := false
timer := time.After(1 * time.Second)
for {
select {
case <-timer:
t.Fatalf("log not found in time period: %s", want)
default:
if match {
return
}
entries := hook.AllEntries()
for _, e := range entries {
if strings.Contains(e.Message, want) {
match = true
}
}
}
}
}