Skip to content

Commit 242e5ed

Browse files
Bryan C. Millsgopherbot
Bryan C. Mills
authored andcommitted
cover: eliminate an unnecessary fsync in TestParseProfiles
CL 179377 added TestParseProfiles, and used a call to (*os.File).Sync instead of closing the file to flush its pending writes. Unfortunately, on some filesystems Sync is fabulously expensive — it may flush all pending writes everywhere on the filesystem, instead of just flushing the writes to the one file, and flushes all the way to disk even though this test really only needs tho writes to be observable in the same process. Instead, we can simplify the test significantly by using os.WriteFile to write and flush the file's contents. Fixes golang/go#57481. Change-Id: I7cda28fb6e9c8183dedadf79dbafe7e870ec0c42 Reviewed-on: https://go-review.googlesource.com/c/tools/+/495798 TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Than McIntosh <[email protected]> Auto-Submit: Bryan Mills <[email protected]> Run-TryBot: Bryan Mills <[email protected]>
1 parent 651d951 commit 242e5ed

File tree

1 file changed

+5
-19
lines changed

1 file changed

+5
-19
lines changed

cover/profile_test.go

+5-19
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ package cover
66

77
import (
88
"fmt"
9-
"io/ioutil"
109
"os"
10+
"path/filepath"
1111
"reflect"
1212
"testing"
1313
)
@@ -208,26 +208,12 @@ some/fancy/path:42.69,44.16 2 -1`,
208208

209209
for _, tc := range tests {
210210
t.Run(tc.name, func(t *testing.T) {
211-
f, err := ioutil.TempFile("", "")
212-
if err != nil {
213-
t.Fatalf("Failed to create a temp file: %v.", err)
214-
}
215-
defer func() {
216-
f.Close()
217-
os.Remove(f.Name())
218-
}()
219-
n, err := f.WriteString(tc.input)
220-
if err != nil {
221-
t.Fatalf("Failed to write to temp file: %v", err)
222-
}
223-
if n < len(tc.input) {
224-
t.Fatalf("Didn't write enough bytes to temp file (wrote %d, expected %d).", n, len(tc.input))
225-
}
226-
if err := f.Sync(); err != nil {
227-
t.Fatalf("Failed to sync temp file: %v", err)
211+
fname := filepath.Join(t.TempDir(), "test.cov")
212+
if err := os.WriteFile(fname, []byte(tc.input), 0644); err != nil {
213+
t.Fatal(err)
228214
}
229215

230-
result, err := ParseProfiles(f.Name())
216+
result, err := ParseProfiles(fname)
231217
if err != nil {
232218
if !tc.expectErr {
233219
t.Errorf("Unexpected error: %v", err)

0 commit comments

Comments
 (0)