-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlazy_test.go
More file actions
107 lines (83 loc) · 1.78 KB
/
lazy_test.go
File metadata and controls
107 lines (83 loc) · 1.78 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 gg_test
import (
"sync"
"testing"
"github.com/mitranim/gg"
"github.com/mitranim/gg/gtest"
)
// TODO: test with concurrency.
func TestOnce(t *testing.T) {
defer gtest.Catch(t)
var count int
fun := gg.Once(func() int {
count++
return count
})
gtest.Eq(fun(), 1)
gtest.Eq(count, 1)
gtest.Eq(fun(), 1)
gtest.Eq(count, 1)
gtest.Eq(fun(), 1)
gtest.Eq(count, 1)
}
func TestOnce_panic_retry(t *testing.T) {
defer gtest.Catch(t)
var count int
fun := gg.Once(func() int {
count++
if count <= 3 {
panic(`intermittent_failure`)
}
return 123
})
gtest.PanicStr(`intermittent_failure`, func() { fun() })
gtest.PanicStr(`intermittent_failure`, func() { fun() })
gtest.PanicStr(`intermittent_failure`, func() { fun() })
gtest.Eq(fun(), 123)
gtest.Eq(fun(), 123)
gtest.Eq(fun(), 123)
}
func BenchmarkOnce_make(b *testing.B) {
for ind := 0; ind < b.N; ind++ {
gg.Nop1(gg.Once(gg.Cwd))
}
}
func BenchmarkOnce_call(b *testing.B) {
once := gg.Once(gg.Cwd)
for ind := 0; ind < b.N; ind++ {
gg.Nop1(once())
}
}
func Benchmark_sync_OnceValue_make(b *testing.B) {
for ind := 0; ind < b.N; ind++ {
gg.Nop1(sync.OnceValue(gg.Cwd))
}
}
func Benchmark_sync_OnceValue_call(b *testing.B) {
once := sync.OnceValue(gg.Cwd)
for ind := 0; ind < b.N; ind++ {
gg.Nop1(once())
}
}
// TODO: test with concurrency.
func TestLazy(t *testing.T) {
defer gtest.Catch(t)
var count int
once := gg.NewLazy(func() int {
count++
if count > 1 {
panic(gg.Errf(`excessive count %v`, count))
}
return count
})
gtest.Eq(*gg.CastUnsafe[*int](once), 0)
gtest.Eq(once.Get(), 1)
gtest.Eq(once.Get(), once.Get())
gtest.Eq(*gg.CastUnsafe[*int](once), 1)
}
func BenchmarkLazy(b *testing.B) {
once := gg.NewLazy(gg.Cwd)
for ind := 0; ind < b.N; ind++ {
gg.Nop1(once.Get())
}
}