-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathepoch_drain_test.go
More file actions
76 lines (60 loc) · 2.05 KB
/
Copy pathepoch_drain_test.go
File metadata and controls
76 lines (60 loc) · 2.05 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
package chainsource
import (
"testing"
"github.com/stretchr/testify/require"
)
// TestDrainToLatestEpochCoalesces verifies that a backlog of queued block
// epochs collapses to the most recent one, so finality synthesis evaluates
// the highest observed height rather than re-checking once per stale epoch.
func TestDrainToLatestEpochCoalesces(t *testing.T) {
t.Parallel()
ch := make(chan *BlockEpoch, 8)
cur := &BlockEpoch{Height: 100}
// Queue several newer epochs behind the one already dequeued.
for h := int32(101); h <= 105; h++ {
ch <- &BlockEpoch{Height: h}
}
got, closed := drainToLatestEpoch(ch, cur)
require.False(t, closed)
require.NotNil(t, got)
require.Equal(t, int32(105), got.Height)
// The channel must be fully drained afterwards.
require.Len(t, ch, 0)
}
// TestDrainToLatestEpochEmpty verifies that with nothing queued the helper
// returns the current epoch unchanged and reports the channel open.
func TestDrainToLatestEpochEmpty(t *testing.T) {
t.Parallel()
ch := make(chan *BlockEpoch, 4)
cur := &BlockEpoch{Height: 42}
got, closed := drainToLatestEpoch(ch, cur)
require.False(t, closed)
require.Same(t, cur, got)
}
// TestDrainToLatestEpochSkipsNil verifies that nil epochs in the backlog are
// ignored: the most recent non-nil epoch wins.
func TestDrainToLatestEpochSkipsNil(t *testing.T) {
t.Parallel()
ch := make(chan *BlockEpoch, 4)
cur := &BlockEpoch{Height: 10}
ch <- &BlockEpoch{Height: 11}
ch <- nil
got, closed := drainToLatestEpoch(ch, cur)
require.False(t, closed)
require.NotNil(t, got)
require.Equal(t, int32(11), got.Height)
}
// TestDrainToLatestEpochClosed verifies that a closed channel is reported so
// the caller can park its receive, while still returning the latest epoch
// observed before the close.
func TestDrainToLatestEpochClosed(t *testing.T) {
t.Parallel()
ch := make(chan *BlockEpoch, 4)
cur := &BlockEpoch{Height: 7}
ch <- &BlockEpoch{Height: 8}
close(ch)
got, closed := drainToLatestEpoch(ch, cur)
require.True(t, closed)
require.NotNil(t, got)
require.Equal(t, int32(8), got.Height)
}