-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcloser_test.go
More file actions
41 lines (36 loc) · 787 Bytes
/
closer_test.go
File metadata and controls
41 lines (36 loc) · 787 Bytes
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
package otk
import (
"strconv"
"sync/atomic"
"testing"
"time"
"go.oneofone.dev/genh"
)
type closable struct {
i atomic.Int64
}
func (c *closable) Close() error {
c.i.Add(-1)
return nil
}
func TestCloser(t *testing.T) {
var cc closable
var closed genh.LSlice[string]
c := NewCloser(func(name string, took time.Duration) {
closed.Append(name)
// t.Logf("closed %s in %s", name, took)
})
for i := 0; i < 10; i++ {
cc.i.Add(1)
c.Add("closable:"+strconv.Itoa(i), cc.Close, i%2 == 0)
}
if err := c.Close(); err != nil {
t.Fatal(err)
}
syncExpected := []string{"closable:0", "closable:2", "closable:4", "closable:6", "closable:8"}
for i, v := range syncExpected {
if closed.Get(i) != v {
t.Errorf("expected %s at %d, got %s", v, i, closed.Get(i))
}
}
}