Debian Codegraph @

debian/go/src/golang.org/x/telemetry/internal/counter/rotate_test.go

raw symbols SCIP
1 // Copyright 2023 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package counter
6
7 import (
8 "fmt"
9 "os"
10 "path/filepath"
11 "runtime"
12 "testing"
13 "time"
14
15 "golang.org/x/telemetry/internal/mmap"
16 "golang.org/x/telemetry/internal/telemetry"
17 "golang.org/x/telemetry/internal/testenv"
18 )
19
20 // this test traces the life of a counter from creation
21 // through two file.rotate()s, followed by a failure to rotate
22 func TestRotateCounters(t *testing.T) {
23 testenv.SkipIfUnsupportedPlatform(t)
24 t.Logf("GOOS %s GOARCH %s", runtime.GOOS, runtime.GOARCH)
25 setup(t)
26 defer restore()
27
28 now := getnow()
29 CounterTime = func() time.Time { return now }
30
31 var f file
32 defer close(&f)
33 c := f.New("gophers")
34 if c.ptr.count != nil {
35 t.Error("new counter has non-nil ptr.count")
36 }
37 c.Inc() // make sure neither hits Counter.add()
38 c.Inc() // second use takes a different code path
39 // at this point c.file is not mapped so c's value is stored in extra.
40 if c.ptr.count != nil {
41 t.Error("counter without mapped file has non-nil ptr.count")
42 }
43 if c.file.current.Load() != nil {
44 t.Error("counter has mapped file unexpectedly")
45 }
46 state := c.state.load()
47 if state.extra() != 2 {
48 // the value of c is in its extra field
49 t.Errorf("got %d, expected 2", state.extra())
50 }
51 // Read should give the same answer
52 if v, err := Read(c); err != nil || v != 2 {
53 t.Errorf("Read got %d, %v, expected 2, nil", v, err)
54 }
55 f.rotate()
56 c.Inc() // this goes through counter.add() safely
57 if c.file.current.Load() == nil {
58 t.Error("rotated file has no mapping")
59 }
60 // rotate called c.releaseLock(), moving c's value from extra to the file
61 state = c.state.load()
62 if state.extra() != 0 {
63 t.Errorf("got %d, expected 0", state.extra())
64 }
65 if c.ptr.count == nil {
66 t.Errorf("c has unexpected nil ptr")
67 } else if c.ptr.count.Load() != 3 {
68 // the value of c is in the mapped file
69 t.Errorf("got %d, expected 3", c.ptr.count.Load())
70 }
71 // and Read should give the same result
72 if v, err := Read(c); err != nil || v != 3 {
73 t.Errorf("Read gave %d, %v, expected 3, nil", v, err)
74 }
75
76 // move into the future and rotate the file, remapping it
77 now = now.Add(7 * 24 * time.Hour)
78 f.rotate()
79 if got, want := f.timeBegin.Format("2006-01-02"), now.Format("2006-01-02"); got != want {
80 t.Errorf("f.timeBegin = %q, want %q", got, want)
81 }
82
83 // c has value 0 in the new file
84 // but c won't have a pointer until the next Inc()
85 state = c.state.load()
86 if c.ptr.count == nil {
87 t.Errorf("c unexpedtedly has nil ptr")
88 } else if state.havePtr() {
89 t.Error("unexpected pointer")
90 }
91 if state.extra() != 0 {
92 t.Errorf("got %d, expected 0", state.extra())
93 }
94 c.Inc()
95 state = c.state.load()
96 if state.extra() != 0 {
97 // as expected
98 t.Errorf("got %d, expected 0", state.extra())
99 }
100 if !state.havePtr() {
101 t.Errorf("expectd havePtr")
102 }
103 if c.ptr.count == nil || c.ptr.count.Load() != 1 {
104 t.Errorf("c has wrong value")
105 }
106 // add a counter
107 y := f.New("counter")
108
109 // simulate failure to remap
110 oldmap := memmap
111 now = now.Add(7 * 24 * time.Hour)
112 memmap = func(*os.File, *mmap.Data) (mmap.Data, error) { return mmap.Data{}, fmt.Errorf("too bad") }
113 f.rotate()
114 memmap = oldmap
115
116 // no mapping
117 if f.current.Load() != nil {
118 t.Errorf("unexpected mapping")
119 }
120 c.Inc()
121 // c should not have a pointer, but its internal
122 // count should have been incremented
123 if c.ptr.count != nil {
124 t.Error("expected nil ptr")
125 }
126 if c.state.load().extra() != 1 {
127 t.Errorf("got %d, but expected extra to be 1", c.state.load().extra())
128 }
129 // make sure a new counter doesn't fault
130 x := f.New("newcounter")
131 x.Inc()
132 if x.state.load().extra() != 1 {
133 t.Errorf("got %d, but expected extra to be 1", c.state.load().extra())
134 }
135 // make sure an existing unused counter doesn't fault
136 // (it's incremented, but not visible externally)
137 y.Inc()
138 if y.state.load().extra() != 1 {
139 t.Errorf("got %d, but expected extra to be 1", c.state.load().extra())
140 }
141 }
142
143 // return the current date according to counterTime()
144 func getnow() time.Time {
145 year, month, day := CounterTime().Date()
146 now := time.Date(year, month, day, 0, 0, 0, 0, time.UTC)
147 return now
148 }
149
150 func TestRotate(t *testing.T) {
151 testenv.SkipIfUnsupportedPlatform(t)
152
153 t.Logf("GOOS %s GOARCH %s", runtime.GOOS, runtime.GOARCH)
154 now := getnow()
155 setup(t)
156 defer restore()
157 // pretend something was uploaded
158 os.WriteFile(filepath.Join(telemetry.Default.UploadDir(), "anything"), []byte{}, 0666)
159 var f file
160 defer close(&f)
161 c := f.New("gophers")
162 c.Inc()
163 var modified int
164 for i := 0; i < 2; i++ {
165 // nothing should change on the second rotate
166 f.rotate()
167 fi, err := os.ReadDir(telemetry.Default.LocalDir())
168 if err != nil || len(fi) != 2 {
169 t.Fatalf("err=%v, len(fi) = %d, want 2", err, len(fi))
170 }
171 x := fi[0].Name()
172 y := x[len(x)-len("2006-01-02")-len(".v1.count") : len(x)-len(".v1.count")]
173 us, err := time.ParseInLocation("2006-01-02", y, time.UTC)
174 if err != nil {
175 t.Fatal(err)
176 }
177 // we expect today's date?
178 if us != now {
179 t.Errorf("us = %v, want %v, i=%d y=%s", us, now, i, y)
180 }
181 fd, err := os.Open(filepath.Join(telemetry.Default.LocalDir(), fi[0].Name()))
182 if err != nil {
183 t.Fatal(err)
184 }
185 stat, err := fd.Stat()
186 if err != nil {
187 t.Fatal(err)
188 }
189 mt := stat.ModTime().Nanosecond()
190 if modified == 0 {
191 modified = mt
192 }
193 if modified != mt {
194 t.Errorf("modified = %v, want %v", mt, modified)
195 }
196 fd.Close()
197 }
198 CounterTime = func() time.Time { return now.Add(7 * 24 * time.Hour) }
199 f.rotate()
200 fi, err := os.ReadDir(telemetry.Default.LocalDir())
201 if err != nil || len(fi) != 3 {
202 t.Fatalf("err=%v, len(fi) = %d, want 3", err, len(fi))
203 }
204 }
205
206 // These were useful while debugging failed mapping
207 func (s *counterState) String() string {
208 if s == nil {
209 return "nil"
210 }
211 return s.load().String()
212 }
213
214 func (b counterStateBits) String() string {
215 rdrs := b.readers()
216 locked := b.locked()
217 if locked {
218 rdrs = 0 // rdrs == 1<<30 - 1
219 }
220 havePtr := b&stateHavePtr != 0
221 extra := uint64(b&stateExtra) >> stateExtraShift
222 return fmt.Sprintf("rdrs:0x%x locked:%v\thavePtr:%v\textra:%d", rdrs, locked, havePtr, extra)
223 }