diff options
author | Luke Shumaker <lukeshu@lukeshu.com> | 2023-02-10 14:13:21 -0700 |
---|---|---|
committer | Luke Shumaker <lukeshu@lukeshu.com> | 2023-02-10 14:13:21 -0700 |
commit | 7a5a0e821693364156c1de8f7bd87286b0eed98b (patch) | |
tree | ee5fcacbba4d4eb7c44e4cb1f35fb950f11aee1d /compat/json/borrowed_stream_test.go | |
parent | 480ccfd05a13ac36516c536a71203280a31b4d28 (diff) | |
parent | 483bbdc970b26d774ace39edfde8420aba53b742 (diff) |
Merge branch 'lukeshu/go1.20'
Diffstat (limited to 'compat/json/borrowed_stream_test.go')
-rw-r--r-- | compat/json/borrowed_stream_test.go | 54 |
1 files changed, 39 insertions, 15 deletions
diff --git a/compat/json/borrowed_stream_test.go b/compat/json/borrowed_stream_test.go index 6c3a403..d90898b 100644 --- a/compat/json/borrowed_stream_test.go +++ b/compat/json/borrowed_stream_test.go @@ -13,6 +13,7 @@ import ( "net/http" "net/http/httptest" "reflect" + "runtime/debug" "strings" "testing" ) @@ -61,6 +62,44 @@ func TestEncoder(t *testing.T) { } } +//nolint:paralleltest // MODIFIED: added; can't be parallel because it fusses with the global GC. +func TestEncoderErrorAndReuseEncodeState(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 + + var buf bytes.Buffer + enc := NewEncoder(&buf) + if err := enc.Encode(dummy); err == nil { + t.Errorf("Encode(dummy) == nil; want error") + } + + type Data struct { + A string + I int + } + data := Data{A: "a", I: 1} + if err := enc.Encode(data); err != nil { + t.Errorf("Marshal(%v) = %v", data, err) + } + + var data2 Data + if err := Unmarshal(buf.Bytes(), &data2); err != nil { + t.Errorf("Unmarshal(%v) = %v", data2, err) + } + if data2 != data { + t.Errorf("expect: %v, but get: %v", data, data2) + } +} + var streamEncodedIndent = `0.1 "hello" null @@ -324,21 +363,6 @@ func TestBlocking(t *testing.T) { } } -func BenchmarkEncoderEncode(b *testing.B) { - b.ReportAllocs() - type T struct { - X, Y string - } - v := &T{"foo", "bar"} - b.RunParallel(func(pb *testing.PB) { - for pb.Next() { - if err := NewEncoder(io.Discard).Encode(v); err != nil { - b.Fatal(err) - } - } - }) -} - //nolint:dupword // False positive, this is commented-out code, not a real comment. // MODIFIED: added nolint declaration /* // MODIFIED: we don't have tokens type tokenStreamCase struct { |