MCPcopy Index your code
hub / github.com/riverqueue/river / TestAppendLogDataWithCap

Function TestAppendLogDataWithCap

riverlog/river_log_test.go:48–199  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

46}
47
48func TestAppendLogDataWithCap(t *testing.T) {
49 t.Parallel()
50
51 marshalLog := func(tb testing.TB, attempt int, log string) []byte {
52 tb.Helper()
53 b, err := json.Marshal(logAttempt{Attempt: attempt, Log: log})
54 require.NoError(tb, err)
55 return b
56 }
57
58 marshalMetadataWithLogs := func(tb testing.TB, logs []logAttempt) []byte {
59 tb.Helper()
60 b, err := json.Marshal(map[string]any{
61 metadataKey: logs,
62 })
63 require.NoError(tb, err)
64 return b
65 }
66
67 unmarshalLogs := func(tb testing.TB, rawArray []byte) []logAttempt {
68 tb.Helper()
69 var logs []logAttempt
70 require.NoError(tb, json.Unmarshal(rawArray, &logs))
71 return logs
72 }
73
74 t.Run("MissingKeyStartsFromEmptyArray", func(t *testing.T) {
75 t.Parallel()
76
77 newEntry := marshalLog(t, 1, "new")
78 result, dropped, err := appendLogDataWithCap([]byte(`{"other":"value"}`), newEntry, maxTotalBytes)
79 require.NoError(t, err)
80 require.Zero(t, dropped)
81 require.Equal(t, []logAttempt{{Attempt: 1, Log: "new"}}, unmarshalLogs(t, result))
82 })
83
84 t.Run("NonArrayLogValueReturnsError", func(t *testing.T) {
85 t.Parallel()
86
87 newEntry := marshalLog(t, 1, "new")
88 _, _, err := appendLogDataWithCap([]byte(`{"river:log":{"not":"array"}}`), newEntry, maxTotalBytes)
89 require.EqualError(t, err, `"river:log" value is not an array`)
90 })
91
92 t.Run("PrunesOldestEntriesOnlyAsNeeded", func(t *testing.T) {
93 t.Parallel()
94
95 existing := []logAttempt{
96 {Attempt: 1, Log: "a"},
97 {Attempt: 2, Log: "b"},
98 {Attempt: 3, Log: "c"},
99 }
100 newEntry := marshalLog(t, 4, "d")
101
102 target, err := json.Marshal([]logAttempt{
103 {Attempt: 2, Log: "b"},
104 {Attempt: 3, Log: "c"},
105 {Attempt: 4, Log: "d"},

Callers

nothing calls this directly

Calls 3

appendLogDataWithCapFunction · 0.85
HelperMethod · 0.65
RunMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…