(t *testing.T)
| 46 | } |
| 47 | |
| 48 | func 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"}, |
nothing calls this directly
no test coverage detected
searching dependent graphs…