-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathwarc_test.go
More file actions
107 lines (93 loc) · 2.76 KB
/
warc_test.go
File metadata and controls
107 lines (93 loc) · 2.76 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package warc
import (
"fmt"
"slices"
"strconv"
"strings"
"sync/atomic"
"testing"
)
func TestGetNextWarcFileName(t *testing.T) {
var serial atomic.Uint64
result := getNextWARCFilename("", "test", CompressionGzip, &serial)
split := []string{"test", "00000", ".warc.gz.open"}
if strings.Contains(result, split[0]) && strings.Contains(result, split[1]) && strings.Contains(result, split[2]) {
t.Errorf("Expected filename to contain %s, %s, and %s, but got %s", split[0], split[1], split[2], result)
}
}
func TestSequentialGetNextWarcFileName(t *testing.T) {
// test if the serials increment correctly when called sequentially
var serial atomic.Uint64
var prevSerial uint64
for range 100 {
result := getNextWARCFilename("", "test", CompressionGzip, &serial)
split := strings.Split(result, "-")
if len(split) < 3 {
t.Errorf("Unexpected filename format: %s", result)
continue
}
serialStr := split[2]
serialNum, err := strconv.ParseUint(serialStr, 10, 64)
if err != nil {
t.Errorf("Error parsing serial number from filename %s: %v", result, err)
continue
}
if serialNum != prevSerial+1 {
t.Errorf("Expected serial %d, got %d in filename %s", prevSerial+1, serialNum, result)
}
prevSerial = serialNum
}
}
func TestConcurrentGetNextWarcFileName(t *testing.T) {
// test if the serials increment correctly when called concurrently
var serial atomic.Uint64
var prevSerial uint64
const goroutines = 10
const iterations = 20
results := make(chan string, goroutines*iterations)
errors := make(chan error, goroutines*iterations)
for g := 0; g < goroutines; g++ {
go func() {
for i := 0; i < iterations; i++ {
result := getNextWARCFilename("", "test", CompressionGzip, &serial)
results <- result
}
}()
}
serials := make([]uint64, 0, goroutines*iterations)
for i := 0; i < goroutines*iterations; i++ {
select {
case result := <-results:
split := strings.Split(result, "-")
if len(split) < 3 {
errors <- fmt.Errorf("unexpected filename format: %s", result)
continue
}
serialStr := split[2]
serialNum, err := strconv.ParseUint(serialStr, 10, 64)
if err != nil {
errors <- fmt.Errorf("error parsing serial number from filename %s: %v", result, err)
continue
}
serials = append(serials, serialNum)
case err := <-errors:
t.Error(err)
}
}
slices.Sort(serials)
prevSerial = 0
for _, s := range serials {
if s != prevSerial+1 {
t.Errorf("Expected serial %d, got %d", prevSerial+1, s)
}
prevSerial = s
}
if serials[0] != 1 {
t.Errorf("Expected first serial to be 1, got %d", serials[0])
}
if serials[len(serials)-1] != uint64(goroutines*iterations) {
t.Errorf("Expected last serial to be %d, got %d", goroutines*iterations, serials[len(serials)])
}
close(results)
close(errors)
}