-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbalancer_test.go
More file actions
93 lines (85 loc) · 1.85 KB
/
balancer_test.go
File metadata and controls
93 lines (85 loc) · 1.85 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
package structures_test
import (
"fmt"
"log/slog"
"testing"
"time"
"github.com/stevo-go-utils/structures"
)
func TestAdd(t *testing.T) {
balancer := structures.NewBalancer[int]()
balancer.Add(1, 2, 3)
fmt.Println(balancer.Vals())
}
func TestPeek(t *testing.T) {
balancer := structures.NewBalancer[int]()
balancer.Add(1, 2)
val, ok := balancer.Peek()
if !ok {
t.Errorf("expected true, got false")
}
if val != 1 {
t.Errorf("expected 1, got %d", val)
}
}
func TestUse(t *testing.T) {
balancer := structures.NewBalancer[int](
structures.UseTimeoutBalancerOpt(1 * time.Second),
)
balancer.Add(1)
after := time.Now()
res, ok := balancer.Use()
if !ok {
t.Errorf("expected true, got false")
}
res.Wait()
res.Use()
time.Sleep(1 * time.Second)
stats, ok := balancer.Stats(res.Data())
if !ok {
t.Errorf("expected true, got false")
}
if stats.LastUsed().Before(after) {
t.Errorf("expected after, got before")
}
}
func TestRemove(t *testing.T) {
balancer := structures.NewBalancer[int]()
balancer.Add(1, 2, 4, 6)
balancer.Remove(1, 4, 6, 5)
fmt.Println(balancer.Len())
val, ok := balancer.Use()
if !ok {
t.Errorf("expected true, got false")
}
_ = val
}
func TestReport(t *testing.T) {
balancer := structures.NewBalancer[int](
structures.MaxErrsBalancerOpt(1),
)
balancer.Add(1)
res, ok := balancer.Use()
if !ok {
t.Errorf("expected true, got false")
}
res.Report()
stats, ok := balancer.Stats(res.Data())
if !ok {
t.Errorf("expected true, got false")
}
if stats.Errors() != 1 {
t.Errorf("expected 1, got %d", stats.Errors())
}
}
func TestBalancerReadyEvents(t *testing.T) {
balancer := structures.NewBalancer[int](structures.UseTimeoutBalancerOpt(1 * time.Second))
go func() {
for e := range balancer.ReadyEventCh() {
slog.Info(fmt.Sprint(e.Data()))
e.Use()
}
}()
balancer.Add(1, 2, 3)
<-make(chan struct{})
}