diff options
Diffstat (limited to 'compat/json/borrowed_encode_test.go')
-rw-r--r-- | compat/json/borrowed_encode_test.go | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/compat/json/borrowed_encode_test.go b/compat/json/borrowed_encode_test.go index 3d5d675..999c694 100644 --- a/compat/json/borrowed_encode_test.go +++ b/compat/json/borrowed_encode_test.go @@ -14,6 +14,7 @@ import ( "math" "reflect" "regexp" + "runtime/debug" "strconv" "testing" "unicode" @@ -781,6 +782,42 @@ func TestIssue10281(t *testing.T) { } } +//nolint:paralleltest // MODIFIED: added; can't be parallel because it fusses with the global GC. +func TestMarshalErrorAndReuseEncodeState(t *testing.T) { + // Disable the GC temporarily to prevent encodeState's in Pool being cleaned away during the test. + percent := debug.SetGCPercent(-1) + defer debug.SetGCPercent(percent) + + // Trigger an error in Marshal with cyclic data. + type Dummy struct { + Name string + Next *Dummy + } + dummy := Dummy{Name: "Dummy"} + dummy.Next = &dummy + if b, err := Marshal(dummy); err == nil { + t.Errorf("Marshal(dummy) = %#q; want error", b) + } + + type Data struct { + A string + I int + } + data := Data{A: "a", I: 1} + b, err := Marshal(data) + if err != nil { + t.Errorf("Marshal(%v) = %v", data, err) + } + + var data2 Data + if err := Unmarshal(b, &data2); err != nil { + t.Errorf("Unmarshal(%v) = %v", data2, err) + } + if data2 != data { + t.Errorf("expect: %v, but get: %v", data, data2) + } +} + func TestHTMLEscape(t *testing.T) { t.Parallel() // MODIFIED: added var b, want bytes.Buffer |