Skip to content

Commit

Permalink
Splitting cache_test into two for Windows (bazelbuild#139)
Browse files Browse the repository at this point in the history
* Splitting cache_test into two for Windows

* Adding GetCacheHits/Misses as part of the Cache interface to use in re-client.
  • Loading branch information
Ola Rozenfeld authored Jun 24, 2020
1 parent d78b5ea commit 17eef19
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 47 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ require (
github.com/godbus/dbus v4.1.0+incompatible // indirect
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b
github.com/golang/protobuf v1.3.2
github.com/golang/protobuf v1.3.3
github.com/gonum/blas v0.0.0-20181208220705-f22b278b28ac // indirect
github.com/gonum/floats v0.0.0-20181209220543-c233463c7e82 // indirect
github.com/gonum/internal v0.0.0-20181124074243-f884aa714029 // indirect
Expand Down
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@ github.com/bradfitz/go-smtpd v0.0.0-20170404230938-deb6d6237625 h1:ckJgFhFWywOx+
github.com/bradfitz/go-smtpd v0.0.0-20170404230938-deb6d6237625/go.mod h1:HYsPBTaaSFSlLx/70C2HPIMNZpVV8+vt/A+FMnYP11g=
github.com/bradfitz/gomemcache v0.0.0-20190913173617-a41fca850d0b h1:L/QXpzIa3pOvUGt1D1lA5KjYhPBAN/3iWdP7xeFS9F0=
github.com/bradfitz/gomemcache v0.0.0-20190913173617-a41fca850d0b/go.mod h1:H0wQNHz2YrLsuXOZozoeDmnHXkNCRmMW0gwFWDfEZDA=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e h1:fY5BOSpyZCqRo5OhCuC+XN+r/bBCmeuuJtjz+bCNIf8=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/client9/misspell v0.3.4 h1:ta993UF76GwbvJcIo3Y68y/M3WxlpEHPWIGDkJYwzJI=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
github.com/coreos/go-systemd v0.0.0-20181012123002-c6f51f82210d h1:t5Wuyh53qYyg9eqn4BbnlIT+vmhyww0TatL+zT3uWgI=
github.com/coreos/go-systemd v0.0.0-20181012123002-c6f51f82210d/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f h1:lBNOc5arjvs8E5mO2tbpBpLoyyu8B6e44T7hJy6potg=
Expand Down Expand Up @@ -73,6 +75,9 @@ github.com/disintegration/gift v1.2.1 h1:Y005a1X4Z7Uc+0gLpSAsKhWi4qLtsdEcMIbbdvd
github.com/disintegration/gift v1.2.1/go.mod h1:Jh2i7f7Q2BM7Ezno3PhfezbR1xpUg9dUg3/RlKGr4HI=
github.com/edsrzf/mmap-go v1.0.0 h1:CEBF7HpRnUCSJgGUb5h1Gm7e3VkmVDrR8lvWVLtrOFw=
github.com/edsrzf/mmap-go v1.0.0/go.mod h1:YO35OhQPt3KJa3ryjFM5Bs14WD66h8eGKpfaBNrHW5M=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568 h1:BHsljHzVlRcyQhjrss6TZTdY2VfCqZPbv5k3iBFa2ZQ=
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc=
github.com/fogleman/gg v1.3.0 h1:/7zJX8F6AaYQc57WQCyN9cAIz+4bCJGO9B+dyW29am8=
Expand Down Expand Up @@ -104,6 +109,7 @@ github.com/golang/protobuf v1.3.1 h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db h1:woRePGFeVFfLKN/pOkfl+p/TAqKOfFu+7KPlMVpok/w=
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/gonum/blas v0.0.0-20181208220705-f22b278b28ac h1:Q0Jsdxl5jbxouNs1TQYt0gxesYMU4VXRbsTlgDloZ50=
Expand Down Expand Up @@ -190,6 +196,7 @@ github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/remyoudompheng/bigfft v0.0.0-20190728182440-6a916e37a237 h1:HQagqIiBmr8YXawX/le3+O26N+vPPC1PtjaF3mwnook=
github.com/remyoudompheng/bigfft v0.0.0-20190728182440-6a916e37a237/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
Expand Down Expand Up @@ -359,8 +366,11 @@ google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiq
google.golang.org/grpc v1.21.1/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM=
google.golang.org/grpc v1.22.0 h1:J0UbZOIrCAl+fpTOf8YLs4dJo8L/owV4LYVtAXQoPkw=
google.golang.org/grpc v1.22.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg=
google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg=
google.golang.org/grpc v1.24.0 h1:vb/1TCsVn3DcJlQ0Gs1yB1pKI6Do2/QNwxdKqmc/b0s=
google.golang.org/grpc v1.24.0/go.mod h1:XDChyiUovWa60DnaeDeZmSW86xtLtjtZbwvSiRnRtcA=
google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY=
google.golang.org/grpc v1.29.1/go.mod h1:itym6AZVZYACWQqET3MqgPpjcuV5QH3BxFS3IjizoKk=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
13 changes: 12 additions & 1 deletion go/pkg/filemetadata/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

# gazelle:ignore
# gazelle removes select and put them in srcs.

go_library(
name = "go_default_library",
srcs = [
Expand All @@ -19,7 +22,10 @@ go_test(
srcs = [
"cache_test.go",
"filemetadata_test.go",
],
] + select({
"windows": [],
"//conditions:default": ["cache_posix_test.go"],
}),
embed = [":go_default_library"],
deps = [
"//go/pkg/cache:go_default_library",
Expand All @@ -28,3 +34,8 @@ go_test(
"@com_github_google_go_cmp//cmp/cmpopts:go_default_library",
],
)

config_setting(
name = "windows",
constraint_values = ["@platforms//os:windows"],
)
18 changes: 14 additions & 4 deletions go/pkg/filemetadata/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ const (
// Cache is a store for file digests that supports invalidation.
type fmCache struct {
Backend *cache.Cache
CacheHits uint64
CacheMisses uint64
cacheHits uint64
cacheMisses uint64
Validate bool
}

Expand Down Expand Up @@ -86,6 +86,16 @@ func (c *fmCache) Reset() {
c.Backend.Reset()
}

// GetCacheHits returns the number of cache hits.
func (c *fmCache) GetCacheHits() uint64 {
return c.cacheHits
}

// GetCacheMisses returns the number of cache misses.
func (c *fmCache) GetCacheMisses() uint64 {
return c.cacheMisses
}

func (c *fmCache) check() error {
if c.Backend == nil {
return fmt.Errorf("no backend found for store")
Expand Down Expand Up @@ -119,8 +129,8 @@ func (c *fmCache) loadMetadata(filename string) (*Metadata, bool, error) {

func (c *fmCache) updateMetrics(cacheHit bool) {
if cacheHit {
atomic.AddUint64(&c.CacheHits, 1)
atomic.AddUint64(&c.cacheHits, 1)
} else {
atomic.AddUint64(&c.CacheMisses, 1)
atomic.AddUint64(&c.cacheMisses, 1)
}
}
34 changes: 34 additions & 0 deletions go/pkg/filemetadata/cache_posix_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// +build !windows

package filemetadata

import (
"io/ioutil"
"os"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)

func TestExecutableCacheLoad(t *testing.T) {
c := NewSingleFlightCache()
filename, err := createFile(t, true, "")
if err != nil {
t.Fatalf("Failed to create tmp file for testing digests: %v", err)
}
if err = ioutil.WriteFile(filename, contents, os.ModeTemporary); err != nil {
t.Fatalf("Failed to write to tmp file for testing digests: %v", err)
}
got := c.Get(filename)
if got.Err != nil {
t.Errorf("Get(%v) failed. Got error: %v", filename, got.Err)
}
want := &Metadata{
Digest: wantDg,
IsExecutable: true,
}
if diff := cmp.Diff(want, got, cmpopts.IgnoreFields(Metadata{}, "MTime")); diff != "" {
t.Errorf("Get(%v) returned diff. (-want +got)\n%s", filename, diff)
}
}
60 changes: 19 additions & 41 deletions go/pkg/filemetadata/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var (
)

func TestSimpleCacheLoad(t *testing.T) {
c := &fmCache{Backend: cache.GetInstance()}
c := NewSingleFlightCache()
filename, err := createFile(t, false, "")
if err != nil {
t.Fatalf("Failed to create tmp file for testing digests: %v", err)
Expand All @@ -37,38 +37,16 @@ func TestSimpleCacheLoad(t *testing.T) {
if diff := cmp.Diff(want, got, cmpopts.IgnoreFields(Metadata{}, "MTime")); diff != "" {
t.Errorf("Get(%v) returned diff. (-want +got)\n%s", filename, diff)
}
if c.CacheHits != 0 {
t.Errorf("Cache has wrong num of CacheHits, want 0, got %v", c.CacheHits)
if c.GetCacheHits() != 0 {
t.Errorf("Cache has wrong num of CacheHits, want 0, got %v", c.GetCacheHits())
}
if c.CacheMisses != 1 {
t.Errorf("Cache has wrong num of CacheMisses, want 1, got %v", c.CacheMisses)
}
}

func TestExecutableCacheLoad(t *testing.T) {
c := &fmCache{Backend: cache.GetInstance()}
filename, err := createFile(t, true, "")
if err != nil {
t.Fatalf("Failed to create tmp file for testing digests: %v", err)
}
if err = ioutil.WriteFile(filename, contents, os.ModeTemporary); err != nil {
t.Fatalf("Failed to write to tmp file for testing digests: %v", err)
}
got := c.Get(filename)
if got.Err != nil {
t.Errorf("Get(%v) failed. Got error: %v", filename, got.Err)
}
want := &Metadata{
Digest: wantDg,
IsExecutable: true,
}
if diff := cmp.Diff(want, got, cmpopts.IgnoreFields(Metadata{}, "MTime")); diff != "" {
t.Errorf("Get(%v) returned diff. (-want +got)\n%s", filename, diff)
if c.GetCacheMisses() != 1 {
t.Errorf("Cache has wrong num of CacheMisses, want 1, got %v", c.GetCacheMisses())
}
}

func TestCacheOnceLoadMultiple(t *testing.T) {
c := &fmCache{Backend: cache.GetInstance()}
c := NewSingleFlightCache()
filename, err := createFile(t, false, "")
if err != nil {
t.Fatalf("Failed to create tmp file for testing digests: %v", err)
Expand All @@ -89,16 +67,16 @@ func TestCacheOnceLoadMultiple(t *testing.T) {
t.Errorf("Get(%v) returned diff. (-want +got)\n%s", filename, diff)
}
}
if c.CacheHits != 1 {
t.Errorf("Cache has wrong num of CacheHits, want 1, got %v", c.CacheHits)
if c.GetCacheHits() != 1 {
t.Errorf("Cache has wrong num of CacheHits, want 1, got %v", c.GetCacheHits())
}
if c.CacheMisses != 1 {
t.Errorf("Cache has wrong num of CacheMisses, want 1, got %v", c.CacheMisses)
if c.GetCacheMisses() != 1 {
t.Errorf("Cache has wrong num of CacheMisses, want 1, got %v", c.GetCacheMisses())
}
}

func TestLoadAfterChangeWithoutValidation(t *testing.T) {
c := &fmCache{Backend: cache.GetInstance()}
c := NewSingleFlightCache()
filename, err := createFile(t, false, "")
if err != nil {
t.Fatalf("Failed to create tmp file for testing digests: %v", err)
Expand Down Expand Up @@ -132,11 +110,11 @@ func TestLoadAfterChangeWithoutValidation(t *testing.T) {
if diff := cmp.Diff(want, got, cmpopts.IgnoreFields(Metadata{}, "MTime")); diff != "" {
t.Errorf("Get(%v) returned diff. (-want +got)\n%s", filename, diff)
}
if c.CacheHits != 1 {
t.Errorf("Cache has wrong num of CacheHits, want 1, got %v", c.CacheHits)
if c.GetCacheHits() != 1 {
t.Errorf("Cache has wrong num of CacheHits, want 1, got %v", c.GetCacheHits())
}
if c.CacheMisses != 1 {
t.Errorf("Cache has wrong num of CacheMisses, want 1, got %v", c.CacheMisses)
if c.GetCacheMisses() != 1 {
t.Errorf("Cache has wrong num of CacheMisses, want 1, got %v", c.GetCacheMisses())
}
}

Expand Down Expand Up @@ -177,10 +155,10 @@ func TestLoadAfterChange(t *testing.T) {
if diff := cmp.Diff(want, got, cmpopts.IgnoreFields(Metadata{}, "MTime")); diff != "" {
t.Errorf("Get(%v) returned diff. (-want +got)\n%s", filename, diff)
}
if c.CacheHits != 0 {
t.Errorf("Cache has wrong num of CacheHits, want 0, got %v", c.CacheHits)
if c.GetCacheHits() != 0 {
t.Errorf("Cache has wrong num of CacheHits, want 0, got %v", c.GetCacheHits())
}
if c.CacheMisses != 2 {
t.Errorf("Cache has wrong num of CacheMisses, want 2, got %v", c.CacheMisses)
if c.GetCacheMisses() != 2 {
t.Errorf("Cache has wrong num of CacheMisses, want 2, got %v", c.GetCacheMisses())
}
}
13 changes: 13 additions & 0 deletions go/pkg/filemetadata/filemetadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ type Cache interface {
Get(path string) *Metadata
Delete(filename string) error
Reset()
GetCacheHits() uint64
GetCacheMisses() uint64
}

type noopCache struct{}
Expand All @@ -88,6 +90,17 @@ func (c *noopCache) Delete(string) error {
// Reset clears the cache. It is a noop for the Noop cache.
func (c *noopCache) Reset() {}

// GetCacheHits returns the number of cache hits. It returns 0 for Noop cache.
func (c *noopCache) GetCacheHits() uint64 {
return 0
}

// GetCacheMisses returns the number of cache misses.
// It returns 0 for Noop cache.
func (c *noopCache) GetCacheMisses() uint64 {
return 0
}

// NewNoopCache returns a cache that doesn't cache (evaluates on every Get).
func NewNoopCache() Cache {
return &noopCache{}
Expand Down
8 changes: 8 additions & 0 deletions go/pkg/tree/tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,14 @@ func (c *callCountingMetadataCache) Reset() {
c.cache.Reset()
}

func (c *callCountingMetadataCache) GetCacheHits() uint64 {
return 0
}

func (c *callCountingMetadataCache) GetCacheMisses() uint64 {
return 0
}

func TestComputeMerkleTreeEmptySubdirs(t *testing.T) {
fileBlob := []byte("bla")
fileDg := digest.NewFromBlob(fileBlob)
Expand Down

0 comments on commit 17eef19

Please sign in to comment.