mirror of
https://github.com/temporalio/temporal.git
synced 2026-08-31 02:51:51 -07:00
Remove captured log assertion helper
This commit is contained in:
@@ -1,12 +1,9 @@
|
||||
package testlogger
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"slices"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"go.temporal.io/server/common/log/tag"
|
||||
)
|
||||
|
||||
@@ -17,29 +14,6 @@ type CapturedLog struct {
|
||||
Tags []tag.Tag
|
||||
}
|
||||
|
||||
// CapturedLogPattern describes a captured log using formatted tag values.
|
||||
// Tags match a subset of the captured log's tags.
|
||||
type CapturedLogPattern struct {
|
||||
Level Level
|
||||
Message string
|
||||
Tags map[string]string
|
||||
}
|
||||
|
||||
func (p CapturedLogPattern) matches(record CapturedLog) bool {
|
||||
if record.Level != p.Level || record.Message != p.Message {
|
||||
return false
|
||||
}
|
||||
|
||||
for key, expected := range p.Tags {
|
||||
if !slices.ContainsFunc(record.Tags, func(actual tag.Tag) bool {
|
||||
return actual.Key() == key && formatValue(actual) == expected
|
||||
}) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
type captureFilterTag struct {
|
||||
key string
|
||||
value string
|
||||
@@ -78,37 +52,6 @@ func (c *Capture) Snapshot() []CapturedLog {
|
||||
return records
|
||||
}
|
||||
|
||||
// RequireContains fails the test with tag diffs when the capture does not include a matching log.
|
||||
func (c *Capture) RequireContains(t TestingT, pattern CapturedLogPattern) {
|
||||
t.Helper()
|
||||
records := c.Snapshot()
|
||||
if slices.ContainsFunc(records, pattern.matches) {
|
||||
return
|
||||
}
|
||||
|
||||
var failure strings.Builder
|
||||
fmt.Fprintf(&failure, "captured log pattern not found: level=%s message=%q", pattern.Level, pattern.Message)
|
||||
candidateCount := 0
|
||||
for _, record := range records {
|
||||
if record.Level != pattern.Level || record.Message != pattern.Message {
|
||||
continue
|
||||
}
|
||||
candidateCount++
|
||||
actualTags := make(map[string]string, len(pattern.Tags))
|
||||
for _, actual := range record.Tags {
|
||||
key := actual.Key()
|
||||
if _, expected := pattern.Tags[key]; expected {
|
||||
actualTags[key] = formatValue(actual)
|
||||
}
|
||||
}
|
||||
fmt.Fprintf(&failure, "\n\ncandidate %d tag mismatch (-want +got):\n%s", candidateCount, cmp.Diff(pattern.Tags, actualTags))
|
||||
}
|
||||
if candidateCount == 0 {
|
||||
fmt.Fprintf(&failure, "\n\nno captured log had the expected level and message; captured logs: %+v", records)
|
||||
}
|
||||
t.Fatalf("%s", failure.String())
|
||||
}
|
||||
|
||||
func (c *Capture) record(record CapturedLog) {
|
||||
if len(c.filterTags) > 0 {
|
||||
matched := false
|
||||
|
||||
@@ -1,11 +1,8 @@
|
||||
package testlogger_test
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"go.temporal.io/server/common/log"
|
||||
@@ -13,20 +10,6 @@ import (
|
||||
"go.temporal.io/server/common/testing/testlogger"
|
||||
)
|
||||
|
||||
type fatalRecorder struct {
|
||||
testing.TB
|
||||
helperCalled bool
|
||||
message string
|
||||
}
|
||||
|
||||
func (r *fatalRecorder) Helper() {
|
||||
r.helperCalled = true
|
||||
}
|
||||
|
||||
func (r *fatalRecorder) Fatalf(format string, args ...any) {
|
||||
r.message = fmt.Sprintf(format, args...)
|
||||
}
|
||||
|
||||
func TestCaptureLifecycle(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
@@ -104,39 +87,6 @@ func TestCaptureSnapshotIsDefensiveCopy(t *testing.T) {
|
||||
}}, capture.Snapshot())
|
||||
}
|
||||
|
||||
func TestCaptureContains(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
testLogger := testlogger.NewTestLogger(t, testlogger.FailOnExpectedErrorOnly)
|
||||
capture := testLogger.StartCapture()
|
||||
testLogger.Error("failed", tag.String("operation", "StartOperation"), tag.Time("attempt-start", time.Now()), tag.Error(errors.New("failure")))
|
||||
|
||||
pattern := testlogger.CapturedLogPattern{
|
||||
Level: testlogger.Error,
|
||||
Message: "failed",
|
||||
Tags: map[string]string{
|
||||
"operation": "StartOperation",
|
||||
"error": "failure",
|
||||
},
|
||||
}
|
||||
capture.RequireContains(t, pattern)
|
||||
|
||||
pattern.Tags["operation"] = "CancelOperation"
|
||||
recorder := &fatalRecorder{}
|
||||
capture.RequireContains(recorder, pattern)
|
||||
require.True(t, recorder.helperCalled)
|
||||
require.Contains(t, recorder.message, "candidate 1 tag mismatch")
|
||||
require.Contains(t, recorder.message, "CancelOperation")
|
||||
require.Contains(t, recorder.message, "StartOperation")
|
||||
|
||||
// A pattern with an extra tag matches nothing.
|
||||
pattern.Tags["operation"] = "StartOperation"
|
||||
pattern.Tags["unexpected"] = "value"
|
||||
recorder = &fatalRecorder{}
|
||||
capture.RequireContains(recorder, pattern)
|
||||
require.Contains(t, recorder.message, "unexpected")
|
||||
}
|
||||
|
||||
func TestCaptureRecordsConcurrentDerivedLoggers(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user