-
Notifications
You must be signed in to change notification settings - Fork 107
/
callbacks_test.go
49 lines (42 loc) 路 936 Bytes
/
callbacks_test.go
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
package beacon
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/drand/drand/chain"
"github.com/drand/drand/chain/boltdb"
"github.com/drand/drand/test"
)
func TestStoreCallback(t *testing.T) {
dir := t.TempDir()
ctx := context.Background()
l := test.Logger(t)
bbstore, err := boltdb.NewBoltStore(l, dir, nil)
require.NoError(t, err)
cb := NewCallbackStore(bbstore)
id1 := "superid"
doneCh := make(chan bool, 1)
cb.AddCallback(id1, func(b *chain.Beacon) {
doneCh <- true
})
cb.Put(ctx, &chain.Beacon{
Round: 1,
})
require.True(t, checkOne(doneCh))
cb.AddCallback(id1, func(*chain.Beacon) {})
cb.Put(ctx, &chain.Beacon{
Round: 1,
})
require.False(t, checkOne(doneCh))
cb.RemoveCallback(id1)
require.False(t, checkOne(doneCh))
}
func checkOne(ch chan bool) bool {
select {
case <-ch:
return true
case <-time.After(100 * time.Millisecond):
return false
}
}