-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathiterator_test.go
More file actions
63 lines (53 loc) · 1.28 KB
/
iterator_test.go
File metadata and controls
63 lines (53 loc) · 1.28 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
package patron
import (
"sync/atomic"
"testing"
"time"
)
func TestForEach(t *testing.T) {
t.Run("basic execution", func(t *testing.T) {
items := []int{1, 2, 3, 4, 5}
var sum int64
ForEach(items, func(item int) {
atomic.AddInt64(&sum, int64(item))
})
if sum != 15 {
t.Errorf("Expected sum 15, got %d", sum)
}
})
t.Run("concurrent constraints", func(t *testing.T) {
// This test ensures that we are actually running concurrently
// If it was sequential, it would take 500ms
// With concurrency, it should be much faster (approx 100ms)
items := []int{1, 2, 3, 4, 5}
start := time.Now()
ForEach(items, func(item int) {
time.Sleep(100 * time.Millisecond)
})
duration := time.Since(start)
// Should be less than sum of sleeps (e.g. < 400ms)
if duration >= 400*time.Millisecond {
t.Errorf("Expected duration < 400ms, got %v", duration)
}
})
t.Run("empty slice", func(t *testing.T) {
var items []int
ForEach(items, func(item int) {})
})
}
func BenchmarkForEach(b *testing.B) {
// Setup a large slice
count := 10000
items := make([]int, count)
for i := range items {
items[i] = i
}
b.ResetTimer()
b.Run("PatronForEach", func(b *testing.B) {
for i := 0; i < b.N; i++ {
ForEach(items, func(item int) {
_ = item * 2
})
}
})
}