MCPcopy
hub / github.com/syncthing/syncthing / TestRotatedFile

Function TestRotatedFile

cmd/syncthing/monitor_test.go:17–85  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

15)
16
17func TestRotatedFile(t *testing.T) {
18 // Verify that log rotation happens.
19
20 dir := t.TempDir()
21
22 open := func(name string) (io.WriteCloser, error) {
23 f, err := os.Create(name)
24 t.Cleanup(func() {
25 if f != nil {
26 _ = f.Close()
27 }
28 })
29
30 return f, err
31 }
32
33 logName := filepath.Join(dir, "log.txt")
34 testData := []byte("12345678\n")
35 maxSize := int64(len(testData) + len(testData)/2)
36
37 // We allow the log file plus two rotated copies.
38 rf, err := newRotatedFile(logName, open, maxSize, 2)
39 if err != nil {
40 t.Fatal(err)
41 }
42
43 // Write some bytes.
44 if _, err := rf.Write(testData); err != nil {
45 t.Fatal(err)
46 }
47 // They should be in the log.
48 checkSize(t, logName, len(testData))
49 checkNotExist(t, logName+".0")
50
51 // Write some more bytes. We should rotate and write into a new file as the
52 // new bytes don't fit.
53 if _, err := rf.Write(testData); err != nil {
54 t.Fatal(err)
55 }
56 checkSize(t, logName, len(testData))
57 checkSize(t, numberedFile(logName, 0), len(testData))
58 checkNotExist(t, logName+".1")
59
60 // Write another byte. That should fit without causing an extra rotate.
61 _, _ = rf.Write([]byte{42})
62 checkSize(t, logName, len(testData)+1)
63 checkSize(t, numberedFile(logName, 0), len(testData))
64 checkNotExist(t, numberedFile(logName, 1))
65
66 // Write some more bytes. We should rotate and write into a new file as the
67 // new bytes don't fit.
68 if _, err := rf.Write(testData); err != nil {
69 t.Fatal(err)
70 }
71 checkSize(t, logName, len(testData))
72 checkSize(t, numberedFile(logName, 0), len(testData)+1) // the one we wrote extra to, now rotated
73 checkSize(t, numberedFile(logName, 1), len(testData))
74 checkNotExist(t, numberedFile(logName, 2))

Callers

nothing calls this directly

Calls 8

newRotatedFileFunction · 0.85
checkSizeFunction · 0.85
checkNotExistFunction · 0.85
numberedFileFunction · 0.85
FatalMethod · 0.80
CreateMethod · 0.65
CloseMethod · 0.65
WriteMethod · 0.45

Tested by

no test coverage detected