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