MCPcopy
hub / github.com/syncthing/syncthing / TestAutoClosedFile

Function TestAutoClosedFile

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

Source from the content-addressed store, hash-verified

138}
139
140func TestAutoClosedFile(t *testing.T) {
141 os.RemoveAll("_autoclose")
142 defer os.RemoveAll("_autoclose")
143 os.Mkdir("_autoclose", 0o755)
144 file := filepath.FromSlash("_autoclose/tmp")
145 data := []byte("hello, world\n")
146
147 // An autoclosed file that closes very quickly
148 ac, err := newAutoclosedFile(file, time.Millisecond, time.Millisecond)
149 if err != nil {
150 t.Fatal(err)
151 }
152
153 // Write some data.
154 if _, err := ac.Write(data); err != nil {
155 t.Fatal(err)
156 }
157
158 // Wait for it to close
159 start := time.Now()
160 for {
161 time.Sleep(time.Millisecond)
162 ac.mut.Lock()
163 fd := ac.fd
164 ac.mut.Unlock()
165 if fd == nil {
166 break
167 }
168 if time.Since(start) > time.Second {
169 t.Fatal("File should have been closed after first write")
170 }
171 }
172
173 // Write more data, which should be an append.
174 if _, err := ac.Write(data); err != nil {
175 t.Fatal(err)
176 }
177
178 // Close.
179 if err := ac.Close(); err != nil {
180 t.Fatal(err)
181 }
182
183 // The file should have both writes in it.
184 bs, err := os.ReadFile(file)
185 if err != nil {
186 t.Fatal(err)
187 }
188 if len(bs) != 2*len(data) {
189 t.Fatalf("Writes failed, expected %d bytes, not %d", 2*len(data), len(bs))
190 }
191
192 // Open the file again.
193 ac, err = newAutoclosedFile(file, time.Second, time.Second)
194 if err != nil {
195 t.Fatal(err)
196 }
197

Callers

nothing calls this directly

Calls 9

newAutoclosedFileFunction · 0.85
FatalMethod · 0.80
UnlockMethod · 0.80
RemoveAllMethod · 0.65
MkdirMethod · 0.65
NowMethod · 0.65
SinceMethod · 0.65
CloseMethod · 0.65
WriteMethod · 0.45

Tested by

no test coverage detected