Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(30)

Side by Side Diff: client/cipd/internal/instancecache_test.go

Issue 1870263002: cipd: instance cache (Closed) Base URL: https://chromium.googlesource.com/external/github.com/luci/luci-go@master
Patch Set: restore check, improve comment Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « client/cipd/internal/instancecache.go ('k') | client/cipd/internal/messages/messages.proto » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package internal
6
7 import (
8 "bytes"
9 "fmt"
10 "io/ioutil"
11 "os"
12 "testing"
13 "time"
14
15 "github.com/luci/luci-go/client/cipd/common"
16 "github.com/luci/luci-go/client/cipd/local"
17
18 "path/filepath"
19 "strings"
20
21 . "github.com/smartystreets/goconvey/convey"
22 )
23
24 func TestInstanceCache(t *testing.T) {
25 Convey("InstanceCache", t, func(c C) {
26 tempDir, err := ioutil.TempDir("", "instanceche_test")
27 So(err, ShouldBeNil)
28 defer os.RemoveAll(tempDir)
29
30 now := time.Date(2016, 1, 2, 3, 4, 5, 6, time.UTC)
31
32 fs := local.NewFileSystem(tempDir, nil)
33 cache := NewInstanceCache(fs, nil)
34
35 put := func(cache *InstanceCache, pin common.Pin, data string) {
36 err = cache.Put(pin, now, func(f *os.File) error {
37 _, err := f.WriteString(data)
38 So(err, ShouldBeNil)
39 return nil
40 })
41 So(err, ShouldBeNil)
42 }
43
44 testHas := func(cache *InstanceCache, pin common.Pin, data strin g) {
45 buf := &bytes.Buffer{}
46 err = cache.Get(pin, buf, now)
47 So(err, ShouldBeNil)
48 So(buf.String(), ShouldEqual, data)
49 }
50
51 Convey("Works", func() {
52 cache2 := NewInstanceCache(fs, nil)
53
54 pin := common.Pin{"pkg", "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb bbbbbbbbbb"}
55 err := cache.Get(pin, ioutil.Discard, now)
56 So(os.IsNotExist(err), ShouldBeTrue)
57
58 // Add new.
59 put(cache, pin, "blah")
60 testHas(cache, pin, "blah")
61 testHas(cache2, pin, "blah")
62
63 // Replace existing.
64 put(cache, pin, "huh")
65 testHas(cache, pin, "huh")
66 testHas(cache2, pin, "huh")
67 })
68
69 pini := func(i int) common.Pin {
70 pin := common.Pin{"pkg", fmt.Sprintf("%d", i)}
71 pin.InstanceID = strings.Repeat("a", 40-len(pin.Instance ID)) + pin.InstanceID
72 return pin
73 }
74
75 Convey("GC", func() {
76 // Add twice more the limit.
77 for i := 0; i < instanceCacheMaxSize*2; i++ {
78 put(cache, pini(i), "blah")
79 now = now.Add(time.Second)
80 }
81
82 // Check the number of actual files.
83 tempDirFile, err := os.Open(tempDir)
84 So(err, ShouldBeNil)
85
86 files, err := tempDirFile.Readdirnames(0)
87 So(err, ShouldBeNil)
88 So(files, ShouldHaveLength, instanceCacheMaxSize+1) // 1 for state.db
89
90 // Try to get.
91 for i := 0; i < instanceCacheMaxSize*2; i++ {
92 err := cache.Get(pini(i), ioutil.Discard, now)
93 So(os.IsNotExist(err), ShouldEqual, i < instance CacheMaxSize)
94 }
95
96 })
97
98 Convey("Sync", func() {
99 stateDbPath := filepath.Join(tempDir, instanceCacheState Filename)
100 const count = 10
101
102 testSync := func(causeResync func()) {
103 // Add instances.
104 for i := 0; i < count; i++ {
105 put(cache, pini(i), "blah")
106 }
107
108 causeResync()
109
110 // state.db must be restored.
111 for i := 0; i < count; i++ {
112 lastAccess, ok := cache.getAccessTime(no w, pini(i))
113 So(ok, ShouldBeTrue)
114 So(lastAccess.IsZero(), ShouldBeTrue)
115 }
116
117 _, ok := cache.getAccessTime(now, common.Pin{"no nexistent", "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"})
118 So(ok, ShouldBeFalse)
119 }
120
121 Convey("state.db disappeared", func() {
122 testSync(func() {
123 err := os.Remove(stateDbPath)
124 So(err, ShouldBeNil)
125 })
126 })
127
128 Convey("state.db corrupted", func() {
129 testSync(func() {
130 f, err := os.Create(stateDbPath)
131 So(err, ShouldBeNil)
132 f.WriteString("blah")
133 defer f.Close()
134 })
135 })
136 })
137 })
138 }
OLDNEW
« no previous file with comments | « client/cipd/internal/instancecache.go ('k') | client/cipd/internal/messages/messages.proto » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698