|
| 1 | +package codec |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +type processRetryEncoder struct { |
| 9 | + processCalls int |
| 10 | +} |
| 11 | + |
| 12 | +func (e *processRetryEncoder) Process(in []byte, out []byte) (int, error) { |
| 13 | + e.processCalls++ |
| 14 | + if e.processCalls == 1 { |
| 15 | + copy(out, []byte("abc")) |
| 16 | + return 3, ErrBufTooSmall |
| 17 | + } |
| 18 | + copy(out, []byte("def")) |
| 19 | + return 3, nil |
| 20 | +} |
| 21 | + |
| 22 | +func (e *processRetryEncoder) Flush(out []byte) (int, error) { return 0, nil } |
| 23 | +func (e *processRetryEncoder) Finish(out []byte) (int, error) { return 0, nil } |
| 24 | +func (e *processRetryEncoder) Reset() {} |
| 25 | +func (e *processRetryEncoder) State() State { return StateStreaming } |
| 26 | + |
| 27 | +type processRetryDecoder struct { |
| 28 | + processCalls int |
| 29 | +} |
| 30 | + |
| 31 | +func (d *processRetryDecoder) Process(in []byte, out []byte) (int, error) { |
| 32 | + d.processCalls++ |
| 33 | + if d.processCalls == 1 { |
| 34 | + copy(out, []byte("ghi")) |
| 35 | + return 3, ErrBufTooSmall |
| 36 | + } |
| 37 | + copy(out, []byte("jkl")) |
| 38 | + return 3, nil |
| 39 | +} |
| 40 | + |
| 41 | +func (d *processRetryDecoder) Flush(out []byte) (int, error) { return 0, nil } |
| 42 | +func (d *processRetryDecoder) Finish(out []byte) (int, error) { return 0, nil } |
| 43 | +func (d *processRetryDecoder) Reset() {} |
| 44 | +func (d *processRetryDecoder) State() State { return StateStreaming } |
| 45 | + |
| 46 | +func TestEncodeBuffer_PreservesOutputAcrossProcessRetry(t *testing.T) { |
| 47 | + out, err := EncodeBuffer(&processRetryEncoder{}, []byte("ignored")) |
| 48 | + if err != nil { |
| 49 | + t.Fatalf("EncodeBuffer() error = %v", err) |
| 50 | + } |
| 51 | + if string(out) != "abcdef" { |
| 52 | + t.Fatalf("EncodeBuffer() = %q, want %q", out, "abcdef") |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func TestDecodeBuffer_PreservesOutputAcrossProcessRetry(t *testing.T) { |
| 57 | + out, err := DecodeBuffer(&processRetryDecoder{}, []byte("ignored")) |
| 58 | + if err != nil { |
| 59 | + t.Fatalf("DecodeBuffer() error = %v", err) |
| 60 | + } |
| 61 | + if string(out) != "ghijkl" { |
| 62 | + t.Fatalf("DecodeBuffer() = %q, want %q", out, "ghijkl") |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +type capRetryEncoder struct { |
| 67 | + processCalls int |
| 68 | + finishCalls int |
| 69 | + stopErr error |
| 70 | +} |
| 71 | + |
| 72 | +func (e *capRetryEncoder) Process(in []byte, out []byte) (int, error) { |
| 73 | + e.processCalls++ |
| 74 | + if e.processCalls > 2 { |
| 75 | + if e.stopErr == nil { |
| 76 | + e.stopErr = errors.New("unexpected extra process retry") |
| 77 | + } |
| 78 | + return 0, e.stopErr |
| 79 | + } |
| 80 | + return 0, ErrBufTooSmall |
| 81 | +} |
| 82 | +func (e *capRetryEncoder) Flush(out []byte) (int, error) { return 0, nil } |
| 83 | +func (e *capRetryEncoder) Reset() {} |
| 84 | +func (e *capRetryEncoder) State() State { return StateStreaming } |
| 85 | + |
| 86 | +func (e *capRetryEncoder) Finish(out []byte) (int, error) { |
| 87 | + e.finishCalls++ |
| 88 | + if e.finishCalls > 2 { |
| 89 | + if e.stopErr == nil { |
| 90 | + e.stopErr = errors.New("unexpected extra retry") |
| 91 | + } |
| 92 | + return 0, e.stopErr |
| 93 | + } |
| 94 | + return 0, ErrBufTooSmall |
| 95 | +} |
0 commit comments