-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfusing_provider_test.go
166 lines (137 loc) · 5.07 KB
/
fusing_provider_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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package manifest_test
import (
"context"
"testing"
"time"
"github.com/filecoin-project/go-f3/internal/clock"
"github.com/filecoin-project/go-f3/internal/consensus"
"github.com/filecoin-project/go-f3/manifest"
"github.com/stretchr/testify/require"
)
type testManifestProvider chan *manifest.Manifest
func (testManifestProvider) Start(context.Context) error { return nil }
func (testManifestProvider) Stop(context.Context) error { return nil }
func (m testManifestProvider) ManifestUpdates() <-chan *manifest.Manifest {
return (<-chan *manifest.Manifest)(m)
}
func TestFusingManifestProvider(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
ctx, clk := clock.WithMockClock(ctx)
t.Cleanup(cancel)
initialManifest := manifest.LocalDevnetManifest()
initialManifest.BootstrapEpoch = 1000
initialManifest.EC.Finality = 900
otherManifest := *initialManifest
otherManifest.BootstrapEpoch -= 10
fakeEc := consensus.NewFakeEC(ctx)
manifestCh := make(chan *manifest.Manifest, 10)
priorityManifestCh := make(chan *manifest.Manifest, 1)
priorityManifestProvider := testManifestProvider(priorityManifestCh)
priorityManifestCh <- initialManifest
prov, err := manifest.NewFusingManifestProvider(ctx,
fakeEc, (testManifestProvider)(manifestCh), priorityManifestProvider)
require.NoError(t, err)
require.NoError(t, prov.Start(ctx))
// No updates queued initially.
require.Empty(t, prov.ManifestUpdates())
// We should get the manifest update.
select {
case manifestCh <- &otherManifest:
default:
t.Fatal("failed to enqueue manifest update")
}
require.True(t, otherManifest.Equal(<-prov.ManifestUpdates()))
// Add some time but not enough to switch manifests. We shouldn't get the update.
clk.Add(initialManifest.EC.Period * 90)
select {
case <-prov.ManifestUpdates():
t.Fatal("did not expect a manifest update")
case <-time.After(time.Second):
}
// Add enough time to switch to the static manifest (within one finality of the bootstrap
// epoch).
clk.Add(initialManifest.EC.Period * 10)
select {
case m := <-prov.ManifestUpdates():
require.True(t, m.Equal(initialManifest), "expected to receive the static manifest")
case <-time.After(time.Second):
t.Fatal("expected a manifest update")
}
// Now update the dynamic manifest. We shouldn't receive it.
select {
case manifestCh <- &otherManifest:
default:
t.Fatal("failed to enqueue manifest update")
}
select {
case <-prov.ManifestUpdates():
t.Fatal("did not expect a manifest update")
case <-time.After(time.Second):
}
}
// Test that starting and stopping the fusing manifest provider works correctly, even if we never
// "fuse".
func TestFusingManifestProviderStop(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
ctx, _ = clock.WithMockClock(ctx)
t.Cleanup(cancel)
initialManifest := manifest.LocalDevnetManifest()
initialManifest.BootstrapEpoch = 1000
fakeEc := consensus.NewFakeEC(ctx)
manifestCh := make(chan *manifest.Manifest, 1)
priorityManifestCh := make(chan *manifest.Manifest, 1)
priorityManifestProvider := testManifestProvider(priorityManifestCh)
priorityManifestCh <- initialManifest
prov, err := manifest.NewFusingManifestProvider(ctx,
fakeEc, (testManifestProvider)(manifestCh), priorityManifestProvider)
require.NoError(t, err)
require.NoError(t, prov.Start(ctx))
require.NoError(t, prov.Stop(ctx))
}
func TestFusingManifestProviderSwitchToPriority(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
ctx, clk := clock.WithMockClock(ctx)
t.Cleanup(cancel)
initialManifest := manifest.LocalDevnetManifest()
initialManifest.BootstrapEpoch = 2000
initialManifest.EC.Finality = 900
fakeEc := consensus.NewFakeEC(ctx)
manifestCh := make(chan *manifest.Manifest, 10)
priorityManifestCh := make(chan *manifest.Manifest, 1)
priorityManifestProvider := testManifestProvider(priorityManifestCh)
priorityManifestCh <- nil
prov, err := manifest.NewFusingManifestProvider(ctx,
fakeEc, (testManifestProvider)(manifestCh), priorityManifestProvider)
require.NoError(t, err)
require.NoError(t, prov.Start(ctx))
priorityManifestCh <- initialManifest
// Create and push a dynamic manifest with bootstrap epoch < 1100
dynamicManifest := *initialManifest
dynamicManifest.BootstrapEpoch = 1000
select {
case manifestCh <- &dynamicManifest:
default:
t.Fatal("failed to enqueue dynamic manifest")
}
select {
case m := <-prov.ManifestUpdates():
require.True(t, m.Equal(&dynamicManifest), "expected dynamic manifest")
case <-time.After(time.Second):
t.Fatal("expected a manifest update")
}
// Add time to reach the priority manifest switch epoch
clk.Add(initialManifest.EC.Period * 1200)
for i := 0; i < 10; i++ {
// fixes weird quirk with fake time
// where the initial manifest doesn't get processed before the initial clk.Add
// and the timer doesn't fire until another clk.Add
clk.Add(1)
}
t.Logf("clck now: %s", clk.Now())
select {
case m := <-prov.ManifestUpdates():
require.True(t, m.Equal(initialManifest), "expected to receive the priority manifest")
case <-time.After(time.Second):
t.Fatal("expected a manifest update")
}
}