prysm-pulse/sharding/internal/log_helper.go
Yutaro Mori 136a630809 sharding: fix race condition in simulator tests (#237)
Former-commit-id: d53651fc87d713f29b458544eec8f060c0f2a284 [formerly 64adf38c5615fd6b833361e8836f2f97d089b78c]
Former-commit-id: 896ca03a66d8fa727897eedb970c5088db2e53b9
2018-07-08 13:40:49 -05:00

50 lines
1.0 KiB
Go

package internal
import (
"testing"
"github.com/ethereum/go-ethereum/log"
)
var _ = log.Handler(&LogHandler{})
// LogHandler provides methods for testing ethereum logs.
type LogHandler struct {
records []*log.Record
t *testing.T
}
func NewLogHandler(t *testing.T) *LogHandler {
return &LogHandler{t: t}
}
// Log adds records to the record slice.
func (t *LogHandler) Log(r *log.Record) error {
t.records = append(t.records, r)
return nil
}
// Pop the record at index 0.
func (t *LogHandler) Pop() *log.Record {
var r *log.Record
r, t.records = t.records[0], t.records[1:]
return r
}
// Length of the records slice.
func (t *LogHandler) Len() int {
return len(t.records)
}
// VerifyLogMsg verfies that the log at index 0 matches the string exactly.
// This method removes the verified message from the slice.
func (h *LogHandler) VerifyLogMsg(str string) {
if h.Len() == 0 {
h.t.Error("Expected a log, but there were none!")
return
}
if l := h.Pop(); l.Msg != str {
h.t.Errorf("Unexpected log: %v. Wanted: %s", l.Msg, str)
}
}