1
0
mirror of https://github.com/golang/go synced 2024-09-30 22:58:34 -06:00
go/internal/memoize/memoize_test.go
Rebecca Stambler ecc01b7716 internal/memoize: document the complicated parts of the memoize package
This change is more of an exercise for myself to better understand the
implementation of the memoize package. It adds detailed documentation
for the get function in particular.

I also modified the tests to use a table-driven test format. I'm not
certain if this was the right approach (in case we want to add a
different type of test case in the future), but for now, it seems to
work fine.

Change-Id: I191a3b65af230e0af54b221c9f671582adec6c79
Reviewed-on: https://go-review.googlesource.com/c/tools/+/181685
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
2019-06-12 16:51:35 +00:00

235 lines
5.4 KiB
Go

// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package memoize_test
import (
"bytes"
"context"
"fmt"
"io"
"runtime"
"strings"
"testing"
"time"
"golang.org/x/tools/internal/memoize"
)
func TestStore(t *testing.T) {
ctx := context.Background()
s := &memoize.Store{}
logBuffer := &bytes.Buffer{}
s.Bind("logger", func(context.Context) interface{} { return logBuffer }).Get(ctx)
// These tests check the behavior of the Bind and Get functions.
// They confirm that the functions only ever run once for a given value.
for _, test := range []struct {
name, key, want string
}{
{
name: "nothing",
},
{
name: "get 1",
key: "_1",
want: `
start @1
simple a = A
simple b = B
simple c = C
end @1 = A B C
`[1:],
},
{
name: "redo 1",
key: "_1",
want: ``,
},
{
name: "get 2",
key: "_2",
want: `
start @2
simple d = D
simple e = E
simple f = F
end @2 = D E F
`[1:],
},
{
name: "redo 2",
key: "_2",
want: ``,
},
{
name: "get 3",
key: "_3",
want: `
start @3
end @3 = @1[ A B C] @2[ D E F]
`[1:],
},
{
name: "get 4",
key: "_4",
want: `
start @3
simple g = G
error ERR = fail
simple h = H
end @3 = G !fail H
`[1:],
},
} {
s.Bind(test.key, generate(s, test.key)).Get(ctx)
got := logBuffer.String()
if got != test.want {
t.Errorf("at %q expected:\n%v\ngot:\n%s", test.name, test.want, got)
}
logBuffer.Reset()
}
// This test checks that values are garbage collected and removed from the
// store when they are no longer used.
pinned := []string{"b", "_1", "_3"} // keys to pin in memory
unpinned := []string{"a", "c", "d", "_2", "_4"} // keys to garbage collect
// Handles maintain a strong reference to their values.
// By generating handles for the pinned keys and keeping the pins alive in memory,
// we ensure these keys stay cached.
var pins []*memoize.Handle
for _, key := range pinned {
h := s.Bind(key, generate(s, key))
h.Get(ctx)
pins = append(pins, h)
}
// Force the garbage collector to run.
runAllFinalizers(t)
// Confirm our expectation that pinned values should remain cached,
// and unpinned values should be garbage collected.
for _, k := range pinned {
if v := s.Cached(k); v == nil {
t.Errorf("pinned value %q was nil", k)
}
}
for _, k := range unpinned {
if v := s.Cached(k); v != nil {
t.Errorf("unpinned value %q should be nil, was %q", k, v)
}
}
// This forces the pins to stay alive until this point in the function.
runtime.KeepAlive(pins)
}
func runAllFinalizers(t *testing.T) {
// The following is very tricky, so be very when careful changing it.
// It relies on behavior of finalizers that is not guaranteed.
// First, run the GC to queue the finalizers.
runtime.GC()
// wait is used to signal that the finalizers are all done.
wait := make(chan struct{})
// Register a finalizer against an immediately collectible object.
//
// The finalizer will signal on the wait channel once it executes,
// and it was the most recently registered finalizer,
// so the wait channel will be closed when all of the finalizers have run.
runtime.SetFinalizer(&struct{ s string }{"obj"}, func(_ interface{}) { close(wait) })
// Now, run the GC again to pick up the tracker object above.
runtime.GC()
// Wait for the finalizers to run or a timeout.
select {
case <-wait:
case <-time.Tick(time.Second):
t.Fatalf("finalizers had not run after 1 second")
}
}
type stringOrError struct {
memoize.NoCopy
value string
err error
}
func (v *stringOrError) String() string {
if v.err != nil {
return v.err.Error()
}
return v.value
}
func asValue(v interface{}) *stringOrError {
if v == nil {
return nil
}
return v.(*stringOrError)
}
func generate(s *memoize.Store, key interface{}) memoize.Function {
return func(ctx context.Context) interface{} {
name := key.(string)
switch name {
case "":
return nil
case "err":
return logGenerator(ctx, s, "ERR", "", fmt.Errorf("fail"))
case "_1":
return joinValues(ctx, s, "@1", "a", "b", "c")
case "_2":
return joinValues(ctx, s, "@2", "d", "e", "f")
case "_3":
return joinValues(ctx, s, "@3", "_1", "_2")
case "_4":
return joinValues(ctx, s, "@3", "g", "err", "h")
default:
return logGenerator(ctx, s, name, strings.ToUpper(name), nil)
}
}
}
// logGenerator generates a *stringOrError value, while logging to the store's logger.
func logGenerator(ctx context.Context, s *memoize.Store, name string, v string, err error) *stringOrError {
// Get the logger from the store.
w := s.Cached("logger").(io.Writer)
if err != nil {
fmt.Fprintf(w, "error %v = %v\n", name, err)
} else {
fmt.Fprintf(w, "simple %v = %v\n", name, v)
}
return &stringOrError{value: v, err: err}
}
// joinValues binds a list of keys to their values, while logging to the store's logger.
func joinValues(ctx context.Context, s *memoize.Store, name string, keys ...string) *stringOrError {
// Get the logger from the store.
w := s.Cached("logger").(io.Writer)
fmt.Fprintf(w, "start %v\n", name)
value := ""
for _, key := range keys {
v := asValue(s.Bind(key, generate(s, key)).Get(ctx))
if v == nil {
value = value + " <nil>"
} else if v.err != nil {
value = value + " !" + v.err.Error()
} else {
value = value + " " + v.value
}
}
fmt.Fprintf(w, "end %v = %v\n", name, value)
return &stringOrError{value: fmt.Sprintf("%s[%v]", name, value)}
}