1
2
3
4
5
6
7
8
9 package lockedfile_test
10
11 import (
12 "bytes"
13 "encoding/binary"
14 "math/rand"
15 "path/filepath"
16 "testing"
17 "time"
18
19 "cmd/go/internal/lockedfile"
20 )
21
22 func isPowerOf2(x int) bool {
23 return x > 0 && x&(x-1) == 0
24 }
25
26 func roundDownToPowerOf2(x int) int {
27 if x <= 0 {
28 panic("nonpositive x")
29 }
30 bit := 1
31 for x != bit {
32 x = x &^ bit
33 bit <<= 1
34 }
35 return x
36 }
37
38 func TestTransform(t *testing.T) {
39 path := filepath.Join(t.TempDir(), "blob.bin")
40
41 const maxChunkWords = 8 << 10
42 buf := make([]byte, 2*maxChunkWords*8)
43 for i := uint64(0); i < 2*maxChunkWords; i++ {
44 binary.LittleEndian.PutUint64(buf[i*8:], i)
45 }
46 if err := lockedfile.Write(path, bytes.NewReader(buf[:8]), 0666); err != nil {
47 t.Fatal(err)
48 }
49
50 var attempts int64 = 128
51 if !testing.Short() {
52 attempts *= 16
53 }
54 const parallel = 32
55
56 var sem = make(chan bool, parallel)
57
58 for n := attempts; n > 0; n-- {
59 sem <- true
60 go func() {
61 defer func() { <-sem }()
62
63 time.Sleep(time.Duration(rand.Intn(100)) * time.Microsecond)
64 chunkWords := roundDownToPowerOf2(rand.Intn(maxChunkWords) + 1)
65 offset := rand.Intn(chunkWords)
66
67 err := lockedfile.Transform(path, func(data []byte) (chunk []byte, err error) {
68 chunk = buf[offset*8 : (offset+chunkWords)*8]
69
70 if len(data)&^7 != len(data) {
71 t.Errorf("read %d bytes, but each write is an integer multiple of 8 bytes", len(data))
72 return chunk, nil
73 }
74
75 words := len(data) / 8
76 if !isPowerOf2(words) {
77 t.Errorf("read %d 8-byte words, but each write is a power-of-2 number of words", words)
78 return chunk, nil
79 }
80
81 u := binary.LittleEndian.Uint64(data)
82 for i := 1; i < words; i++ {
83 next := binary.LittleEndian.Uint64(data[i*8:])
84 if next != u+1 {
85 t.Errorf("wrote sequential integers, but read integer out of sequence at offset %d", i)
86 return chunk, nil
87 }
88 u = next
89 }
90
91 return chunk, nil
92 })
93
94 if err != nil {
95 t.Errorf("unexpected error from Transform: %v", err)
96 }
97 }()
98 }
99
100 for n := parallel; n > 0; n-- {
101 sem <- true
102 }
103 }
104
View as plain text