m3db / m3

M3 monorepo - Distributed TSDB, Aggregator and Query Engine, Prometheus Sidecar, Graphite Compatible, Metrics Platform
https://m3db.io/
Apache License 2.0
4.76k stars 453 forks source link

The testable example "ExampleObjectPool" is not testing output #3763

Open ameowlia opened 3 years ago

ameowlia commented 3 years ago

👋 Hello,

I am investigating the golang issue: https://github.com/golang/go/issues/48362, where testable examples are causing false positives when the output comment block is not the last comment block example.

I ran a parser against the top golang repos, including this repo, and I found this bug with your ExampleObjectPool testable example.

The output comment block should be moved to the end like so:

func ExampleObjectPool() {
    opts := pool.NewObjectPoolOptions()
    p := pool.NewObjectPool(opts)
    p.Init(func() interface{} {
        // The Pool's Allocator should generally only return pointer
        // types, since a pointer can be put into the return interface
        // value without an allocation.
        return new(exampleObject)
    })

    // Get an exampleObject from the pool.
    o := p.Get().(*exampleObject)

    fmt.Printf("Retrieved struct should have default values: %+v", o)

    // Use the exampleObject.
    _ = o

    // Reset the exampleObject and return it to the pool.
    o.reset()
    p.Put(o)

    // Output: Retrieved struct should have default values: &{a:0 b:0 c:0}
}

I am happy to PR this in, but it will take a few days for me to get approval from my employer before I can submit it.

gibbscullen commented 3 years ago

@ameowlia sounds good! Let us know when you have something for us to review.