forked from internetarchive/gowarc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_test.go
More file actions
82 lines (72 loc) · 1.75 KB
/
file_test.go
File metadata and controls
82 lines (72 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package warc
import (
"os"
"strings"
"sync"
"sync/atomic"
"testing"
)
func TestGenerateWARCFilename(t *testing.T) {
serial := &atomic.Uint64{}
serial.Store(5)
fname1 := generateWARCFilename("youtube", CompressionGzip, serial)
if !strings.HasSuffix(fname1, ".warc.gz.open") {
t.Errorf("expected filename suffix: .warc.gz.open, got: %v", fname1)
}
if !strings.HasPrefix(fname1, "youtube-") {
t.Errorf("expected filename prefix: youtube-, got: %v", fname1)
}
if !strings.Contains(fname1, "-00006-") {
t.Errorf("expected filename containing serial+1: -00006-, got: %v", fname1)
}
}
func TestIsFileSizeExceeded(t *testing.T) {
tests := []struct {
name string
sizeMB int64 // size in megabytes
maxSize float64 // max allowed size
expected bool
}{
{"Below limit", 1, 2.0, false},
{"Above limit", 3, 2.0, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpFile, err := os.CreateTemp("", "testfile")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmpFile.Name())
defer tmpFile.Close()
// Truncate file to desired size
if err := tmpFile.Truncate(tt.sizeMB * 1024 * 1024); err != nil {
t.Fatal(err)
}
result := isFileSizeExceeded(tmpFile, tt.maxSize)
if result != tt.expected {
t.Errorf("expected %v, got %v", tt.expected, result)
}
})
}
}
// to be run with -race flag
func TestGenerateWARCFilename_NoRace(_ *testing.T) {
var serial atomic.Uint64
var wg sync.WaitGroup
iterations := 1000
prefix := "test"
compression := CompressionGzip
start := make(chan struct{})
for range 10 {
wg.Add(1)
go func() {
defer wg.Done()
<-start
for range iterations {
_ = generateWARCFilename(prefix, compression, &serial)
}
}()
}
close(start)
wg.Wait()
}