This repository was archived by the owner on Sep 10, 2025. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathmisc_test.go
More file actions
120 lines (99 loc) · 2.18 KB
/
Copy pathmisc_test.go
File metadata and controls
120 lines (99 loc) · 2.18 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
108
109
110
111
112
113
114
115
116
117
118
119
120
package ctx
import (
"errors"
"sync"
"testing"
)
func TestPersistWithHealthCheck(t *testing.T) {
t.Run("No error", func(t *testing.T) {
m := &sync.Map{}
name := "test"
persistWithHealthCheck(2, 0, m, name, func() (interface{}, error) {
return "valid", nil
})
if v, _ := m.Load(name); v.(string) != "valid" {
t.Error("Should be 'valid'")
}
})
t.Run("Has error on first, none on second", func(t *testing.T) {
m := &sync.Map{}
name := "test"
attempt := -1
persistWithHealthCheck(2, 0, m, name, func() (interface{}, error) {
attempt++
if attempt == 1 {
return "valid", nil
}
return nil, errors.New("I am error")
})
if v, _ := m.Load(name); v.(string) != "valid" {
t.Error("Should be 'valid'")
}
})
t.Run("Reach maxAttempt", func(t *testing.T) {
m := &sync.Map{}
name := "test"
defer func() {
if recover() == nil {
t.Error("Recover should be nil.")
}
}()
attempt := -1
persistWithHealthCheck(2, 0, m, name, func() (interface{}, error) {
attempt++
if attempt == 2 {
return "valid", nil
}
return nil, errors.New("I am error")
})
})
}
func TestPersist(t *testing.T) {
t.Run("Set", func(t *testing.T) {
name := "test"
m := &sync.Map{}
persist(m, name, func() interface{} {
return "set"
})
if value, _ := m.Load(name); value.(string) != "set" {
t.Error("Should be 'set'")
}
})
t.Run("Get", func(t *testing.T) {
name := "test"
m := &sync.Map{}
m.Store(name, "get")
persist(m, name, func() interface{} {
return "set"
})
if value, _ := m.Load(name); value.(string) != "get" {
t.Error("Should be 'get'")
}
})
}
func TestPanicIfFound(t *testing.T) {
t.Run("Found", func(t *testing.T) {
defer func() {
if recover() == nil {
t.Error("Recover should be nil.")
}
}()
panicOnFound(true)
})
t.Run("Not Found", func(t *testing.T) {
panicOnFound(false)
})
}
func TestCheckForLockOrReturnValue(t *testing.T) {
t.Run("Is Locked", func(t *testing.T) {
defer func() {
if recover() == nil {
t.Error("Recover should not be nil")
}
}()
checkForLockOrReturnValue(lock{})
})
t.Run("Is Unlocked", func(t *testing.T) {
checkForLockOrReturnValue("hello")
})
}