-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathegg_test.go
More file actions
96 lines (83 loc) · 1.71 KB
/
Copy pathegg_test.go
File metadata and controls
96 lines (83 loc) · 1.71 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
package egg
import (
"errors"
"testing"
"time"
)
func TestLazyEgg(t *testing.T) {
b1 := New(func() any {
return "b1"
})
b2 := New(func() any {
<-time.After(10 * time.Millisecond)
return "b2"
})
b3 := New(func() any {
return "b3"
})
if got := <-b1(); got != "b1" {
t.Errorf("b1: got %v, want %q", got, "b1")
}
if got := <-b2(); got != "b2" {
t.Errorf("b2: got %v, want %q", got, "b2")
}
if got := <-b3(); got != "b3" {
t.Errorf("b3: got %v, want %q", got, "b3")
}
}
func TestLazyPanic(t *testing.T) {
b1 := New(func() any {
return "b1"
})
b2 := New(func() any {
panic(errors.New("panic!!"))
})
b3 := New(func() any {
return "b3"
})
if got := <-b1(); got != "b1" {
t.Errorf("b1: got %v, want %q", got, "b1")
}
got := <-b2()
err, ok := got.(error)
if !ok {
t.Fatalf("b2: got %T(%v), want error", got, got)
}
if err.Error() != "panic!!" {
t.Errorf("b2: got error %q, want %q", err.Error(), "panic!!")
}
if got := <-b3(); got != "b3" {
t.Errorf("b3: got %v, want %q", got, "b3")
}
}
// TestLazyEggUnread verifies that a Worker never blocks on send even if the
// caller never reads the Responder's channel — the buffered channel and its
// goroutine must not leak.
func TestLazyEggUnread(t *testing.T) {
done := make(chan struct{})
go func() {
New(func() any {
return "unread"
})
close(done)
}()
select {
case <-done:
case <-time.After(time.Second):
t.Fatal("New blocked despite unread Responder channel")
}
}
func BenchmarkEgg(b *testing.B) {
var bean = func(i int) Worker {
return func() any {
return i
}
}
beans := make([]Responder, 0, b.N)
for n := 0; n < b.N; n++ {
beans = append(beans, New(bean(n)))
}
for _, be := range beans {
<-be()
}
}