quii / learn-go-with-tests

Learn Go with test-driven development
MIT License
22.2k stars 2.81k forks source link

Running `blogrenderer` benchmark test causes panic #638

Open logan-connolly opened 1 year ago

logan-connolly commented 1 year ago

Hey @quii,

First off, thanks so much for this amazing learning resource. It has helped me so much!

As the title suggest, running BenchmarkRender causes a panic. It seems that the blogrender unit tests run fine because postRenderer.Render is only called only once, but as soon as you call it again, you can reproduce the panic.

Traceback ```shell learn-go-with-tests/blogrenderer  main 🐹 v1.20 ❯ go test -bench=. goos: linux goarch: amd64 pkg: github.com/quii/learn-go-with-tests/blogrenderer cpu: Intel(R) Core(TM) i7-10710U CPU @ 1.10GHz BenchmarkRender-12 panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x1 addr=0x30 pc=0x5c53d1] goroutine 35 [running]: github.com/gomarkdown/markdown/parser.(*Parser).finalize(...) /home/archie/go/pkg/mod/github.com/gomarkdown/markdown@v0.0.0-20211212230626-5af6ad2f47df/parser/parser.go:206 github.com/gomarkdown/markdown/parser.(*Parser).addChild(0xc0000a6000, {0x6dc6b0?, 0xc000028900}) /home/archie/go/pkg/mod/github.com/gomarkdown/markdown@v0.0.0-20211212230626-5af6ad2f47df/parser/parser.go:211 +0x71 github.com/gomarkdown/markdown/parser.(*Parser).addBlock(0xc0000a6000, {0x6dc6b0, 0xc000028900}) /home/archie/go/pkg/mod/github.com/gomarkdown/markdown@v0.0.0-20211212230626-5af6ad2f47df/parser/block.go:392 +0x105 github.com/gomarkdown/markdown/parser.(*Parser).renderParagraph(0xc000018320?, {0xc000018320, 0x10?, 0x10}) /home/archie/go/pkg/mod/github.com/gomarkdown/markdown@v0.0.0-20211212230626-5af6ad2f47df/parser/block.go:1548 +0x10a github.com/gomarkdown/markdown/parser.(*Parser).paragraph(0xc0000a6000, {0xc000018320, 0xe, 0x10}) /home/archie/go/pkg/mod/github.com/gomarkdown/markdown@v0.0.0-20211212230626-5af6ad2f47df/parser/block.go:1711 +0x53f github.com/gomarkdown/markdown/parser.(*Parser).block(0xc0000a6000, {0xc000018320?, 0xc000211ad8?, 0x5e57a4?}) /home/archie/go/pkg/mod/github.com/gomarkdown/markdown@v0.0.0-20211212230626-5af6ad2f47df/parser/block.go:373 +0xf1c github.com/gomarkdown/markdown/parser.(*Parser).Parse(0xc0000a6000, {0xc000018320?, 0x65d000?, 0xc0000288a0?}) /home/archie/go/pkg/mod/github.com/gomarkdown/markdown@v0.0.0-20211212230626-5af6ad2f47df/parser/parser.go:272 +0x3e github.com/gomarkdown/markdown.Parse({0xc000018320?, 0x0?, 0x99?}, 0x65d000?) /home/archie/go/pkg/mod/github.com/gomarkdown/markdown@v0.0.0-20211212230626-5af6ad2f47df/markdown.go:53 +0x65 github.com/gomarkdown/markdown.ToHTML({0xc000018320?, 0x68a75c?, 0xe?}, 0x6893de?, {0x0, 0x0}) /home/archie/go/pkg/mod/github.com/gomarkdown/markdown@v0.0.0-20211212230626-5af6ad2f47df/markdown.go:77 +0x45 github.com/quii/learn-go-with-tests/blogrenderer.newPostVM(...) /home/archie/projects/work/learn-go-with-tests-source/blogrenderer/renderer.go:52 github.com/quii/learn-go-with-tests/blogrenderer.(*PostRenderer).Render(0xc000060300, {0x6da980, 0x88df00}, {{0x689512, 0xb}, {0x68d96e, 0x15}, {0x68a75c, 0xe}, {0xc00006c000, ...}}) /home/archie/projects/work/learn-go-with-tests-source/blogrenderer/renderer.go:37 +0x125 github.com/quii/learn-go-with-tests/blogrenderer_test.BenchmarkRender(0xc00010a280) /home/archie/projects/work/learn-go-with-tests-source/blogrenderer/renderer_test.go:68 +0x1b3 testing.(*B).runN(0xc00010a280, 0x64) /usr/lib/go/src/testing/benchmark.go:193 +0x102 testing.(*B).launch(0xc00010a280) /usr/lib/go/src/testing/benchmark.go:334 +0x1c5 created by testing.(*B).doBench /usr/lib/go/src/testing/benchmark.go:284 +0x6c exit status 2 FAIL github.com/quii/learn-go-with-tests/blogrenderer 0.007s ```

Hope this helps and thanks again for all your hard work!

cativovo commented 2 months ago

Hi, I also encountered the issue. I think the cause of the panic is the reuse of the Parser. Here's a related issue from gomarkdown - https://github.com/gomarkdown/markdown/issues/280. Hope this helps. Thank you for all the hard work.