|
| 1 | +// Copyright (c) 2025 Uber Technologies, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package failure |
| 16 | + |
| 17 | +import ( |
| 18 | + "testing" |
| 19 | + |
| 20 | + "github.com/stretchr/testify/assert" |
| 21 | + "github.com/stretchr/testify/require" |
| 22 | +) |
| 23 | + |
| 24 | +func TestRoundTrip(t *testing.T) { |
| 25 | + tests := []struct { |
| 26 | + name string |
| 27 | + in Failure |
| 28 | + want Failure |
| 29 | + }{ |
| 30 | + { |
| 31 | + name: "subjects and detail", |
| 32 | + in: New("speculator failed", Subject{Type: "queue", ID: "test-queue"}). |
| 33 | + withDetail(map[string]any{"stage": "ask"}), |
| 34 | + want: Failure{ |
| 35 | + Subjects: []Subject{{Type: "queue", ID: "test-queue"}}, |
| 36 | + Detail: map[string]any{"stage": "ask"}, |
| 37 | + }, |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "several subjects keep their order", |
| 41 | + in: New("two at fault", Subject{Type: "batch", ID: "q/batch/2"}, Subject{Type: "batch", ID: "q/batch/1"}), |
| 42 | + want: Failure{Subjects: []Subject{{Type: "batch", ID: "q/batch/2"}, {Type: "batch", ID: "q/batch/1"}}}, |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "nested detail survives", |
| 46 | + in: Failure{Detail: map[string]any{"path": map[string]any{"id": "abc"}}}, |
| 47 | + want: Failure{Detail: map[string]any{"path": map[string]any{"id": "abc"}}}, |
| 48 | + }, |
| 49 | + } |
| 50 | + |
| 51 | + for _, tt := range tests { |
| 52 | + t.Run(tt.name, func(t *testing.T) { |
| 53 | + encoded, err := Encode(tt.in) |
| 54 | + require.NoError(t, err) |
| 55 | + require.NotEmpty(t, encoded) |
| 56 | + |
| 57 | + got, err := Decode(encoded) |
| 58 | + require.NoError(t, err) |
| 59 | + assert.Equal(t, tt.want, got) |
| 60 | + }) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +// The message is carried outside the blob so that whatever stores it keeps a |
| 65 | +// legible column, and so decoding never has to tell an encoded failure apart |
| 66 | +// from a message that happens to look like one. |
| 67 | +func TestEncodeOmitsMessage(t *testing.T) { |
| 68 | + encoded, err := Encode(New("boom", Subject{Type: "batch", ID: "q/batch/1"})) |
| 69 | + require.NoError(t, err) |
| 70 | + assert.NotContains(t, string(encoded), "boom") |
| 71 | + |
| 72 | + got, err := Decode(encoded) |
| 73 | + require.NoError(t, err) |
| 74 | + assert.Empty(t, got.Message) |
| 75 | + assert.Equal(t, []Subject{{Type: "batch", ID: "q/batch/1"}}, got.Subjects) |
| 76 | +} |
| 77 | + |
| 78 | +// Nothing structured means nothing to store, which is what lets a caller treat |
| 79 | +// an absent blob as "unattributed" without a sentinel. |
| 80 | +func TestEncodeNothingStructured(t *testing.T) { |
| 81 | + encoded, err := Encode(New("just a message")) |
| 82 | + require.NoError(t, err) |
| 83 | + assert.Nil(t, encoded) |
| 84 | +} |
| 85 | + |
| 86 | +func TestDecodeEmpty(t *testing.T) { |
| 87 | + got, err := Decode(nil) |
| 88 | + require.NoError(t, err) |
| 89 | + assert.Equal(t, Failure{}, got) |
| 90 | +} |
| 91 | + |
| 92 | +func TestDecodeMalformed(t *testing.T) { |
| 93 | + _, err := Decode([]byte("not json")) |
| 94 | + assert.Error(t, err) |
| 95 | +} |
| 96 | + |
| 97 | +// Detail goes through encoding/json, so every number returns as a float64 |
| 98 | +// whatever its Go type going in. Pinned because a caller that stores an int64 |
| 99 | +// and reads it back expecting one would otherwise find out at runtime. |
| 100 | +func TestDetailNumbersDecodeAsFloat64(t *testing.T) { |
| 101 | + encoded, err := Encode(Failure{Detail: map[string]any{"attempt": int64(3)}}) |
| 102 | + require.NoError(t, err) |
| 103 | + |
| 104 | + got, err := Decode(encoded) |
| 105 | + require.NoError(t, err) |
| 106 | + assert.Equal(t, float64(3), got.Detail["attempt"]) |
| 107 | +} |
| 108 | + |
| 109 | +func TestIDsOfType(t *testing.T) { |
| 110 | + f := New("mixed", |
| 111 | + Subject{Type: "batch", ID: "q/batch/1"}, |
| 112 | + Subject{Type: "queue", ID: "q"}, |
| 113 | + Subject{Type: "batch", ID: "q/batch/2"}, |
| 114 | + ) |
| 115 | + |
| 116 | + assert.Equal(t, []string{"q/batch/1", "q/batch/2"}, f.IDsOfType("batch")) |
| 117 | + assert.Equal(t, []string{"q"}, f.IDsOfType("queue")) |
| 118 | + assert.Empty(t, f.IDsOfType("request")) |
| 119 | + assert.Empty(t, Failure{}.IDsOfType("batch")) |
| 120 | +} |
| 121 | + |
| 122 | +// withDetail keeps the table above readable; New covers message and subjects, |
| 123 | +// which is what most callers set. |
| 124 | +func (f Failure) withDetail(detail map[string]any) Failure { |
| 125 | + f.Detail = detail |
| 126 | + return f |
| 127 | +} |
0 commit comments