@agoric/cosmos 0.34.2-dev-5513dea.0 → 0.34.2-dev-3679b4c.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,195 +0,0 @@
1
- package keeper
2
-
3
- import (
4
- "errors"
5
- "io"
6
- "testing"
7
-
8
- "github.com/Agoric/agoric-sdk/golang/cosmos/vm"
9
- sdk "github.com/cosmos/cosmos-sdk/types"
10
- "github.com/tendermint/tendermint/libs/log"
11
- )
12
-
13
- func newTestSnapshotter() SwingsetSnapshotter {
14
- logger := log.NewNopLogger() // log.NewTMLogger(log.NewSyncWriter( /* os.Stdout*/ io.Discard)).With("module", "sdk/app")
15
- return SwingsetSnapshotter{
16
- isConfigured: func() bool { return true },
17
- takeSnapshot: func(height int64) {},
18
- newRestoreContext: func(height int64) sdk.Context { return sdk.Context{} },
19
- logger: logger,
20
- blockingSend: func(action vm.Jsonable, mustNotBeInited bool) (string, error) { return "", nil },
21
- }
22
- }
23
-
24
- func TestSnapshotInProgress(t *testing.T) {
25
- swingsetSnapshotter := newTestSnapshotter()
26
- ch := make(chan struct{})
27
- swingsetSnapshotter.takeSnapshot = func(height int64) {
28
- <-ch
29
- }
30
- err := swingsetSnapshotter.InitiateSnapshot(123)
31
- if err != nil {
32
- t.Fatal(err)
33
- }
34
- err = swingsetSnapshotter.WaitUntilSnapshotStarted()
35
- if err != nil {
36
- t.Fatal(err)
37
- }
38
-
39
- err = swingsetSnapshotter.InitiateSnapshot(456)
40
- if err == nil {
41
- t.Error("wanted error for snapshot in progress")
42
- }
43
-
44
- err = swingsetSnapshotter.RestoreExtension(
45
- 456, SnapshotFormat,
46
- func() ([]byte, error) {
47
- return nil, io.EOF
48
- })
49
- if err == nil {
50
- t.Error("wanted error for snapshot in progress")
51
- }
52
-
53
- close(ch)
54
- <-swingsetSnapshotter.activeSnapshot.done
55
- err = swingsetSnapshotter.InitiateSnapshot(456)
56
- if err != nil {
57
- t.Fatal(err)
58
- }
59
- }
60
-
61
- func TestNotConfigured(t *testing.T) {
62
- swingsetSnapshotter := newTestSnapshotter()
63
- swingsetSnapshotter.isConfigured = func() bool { return false }
64
- err := swingsetSnapshotter.InitiateSnapshot(123)
65
- if err == nil {
66
- t.Error("wanted error for unconfigured snapshot manager")
67
- }
68
- }
69
-
70
- func TestSecondCommit(t *testing.T) {
71
- swingsetSnapshotter := newTestSnapshotter()
72
-
73
- // Use a channel to block the snapshot goroutine after it has started but before it exits.
74
- ch := make(chan struct{})
75
- swingsetSnapshotter.takeSnapshot = func(height int64) {
76
- <-ch
77
- }
78
-
79
- // First run through app.Commit()
80
- err := swingsetSnapshotter.WaitUntilSnapshotStarted()
81
- if err != nil {
82
- t.Fatal(err)
83
- }
84
- err = swingsetSnapshotter.InitiateSnapshot(123)
85
- if err != nil {
86
- t.Fatal(err)
87
- }
88
-
89
- // Second run through app.Commit() - should return right away
90
- err = swingsetSnapshotter.WaitUntilSnapshotStarted()
91
- if err != nil {
92
- t.Fatal(err)
93
- }
94
-
95
- // close the signaling channel to let goroutine exit
96
- close(ch)
97
- <-swingsetSnapshotter.activeSnapshot.done
98
- }
99
-
100
- func TestInitiateFails(t *testing.T) {
101
- swingsetSnapshotter := newTestSnapshotter()
102
- swingsetSnapshotter.blockingSend = func(action vm.Jsonable, mustNotBeInited bool) (string, error) {
103
- if action.(*snapshotAction).Request == "initiate" {
104
- return "", errors.New("initiate failed")
105
- }
106
- return "", nil
107
- }
108
-
109
- err := swingsetSnapshotter.InitiateSnapshot(123)
110
- if err != nil {
111
- t.Fatal(err)
112
- }
113
- err = swingsetSnapshotter.WaitUntilSnapshotStarted()
114
- if err == nil {
115
- t.Fatal("wanted initiation error")
116
- }
117
- if err.Error() != "initiate failed" {
118
- t.Errorf(`wanted error "initiate failed", got "%s"`, err.Error())
119
- }
120
- // another wait should succeed without error
121
- err = swingsetSnapshotter.WaitUntilSnapshotStarted()
122
- if err != nil {
123
- t.Error(err)
124
- }
125
- }
126
-
127
- func TestRetrievalFails(t *testing.T) {
128
- swingsetSnapshotter := newTestSnapshotter()
129
- swingsetSnapshotter.blockingSend = func(action vm.Jsonable, mustNotBeInited bool) (string, error) {
130
- if action.(*snapshotAction).Request == "retrieve" {
131
- return "", errors.New("retrieve failed")
132
- }
133
- return "", nil
134
- }
135
- nilWriter := func(_ []byte) error { return nil }
136
- var savedErr error
137
- ch := make(chan struct{})
138
- swingsetSnapshotter.takeSnapshot = func(height int64) {
139
- // shortcut to the snapshot manager calling the extension
140
- savedErr = swingsetSnapshotter.SnapshotExtension(uint64(height), nilWriter)
141
- close(ch)
142
- }
143
-
144
- err := swingsetSnapshotter.InitiateSnapshot(123)
145
- if err != nil {
146
- t.Fatal(err)
147
- }
148
- err = swingsetSnapshotter.WaitUntilSnapshotStarted()
149
- if err != nil {
150
- t.Fatal(err)
151
- }
152
-
153
- <-ch
154
- if savedErr == nil {
155
- t.Fatal("wanted retrieval error")
156
- }
157
- if savedErr.Error() != "retrieve failed" {
158
- t.Errorf(`wanted error "retrieve failed", got "%s"`, savedErr.Error())
159
- }
160
- }
161
-
162
- func TestDiscard(t *testing.T) {
163
- discardCalled := false
164
- swingsetSnapshotter := newTestSnapshotter()
165
- swingsetSnapshotter.blockingSend = func(action vm.Jsonable, mustNotBeInited bool) (string, error) {
166
- if action.(*snapshotAction).Request == "discard" {
167
- discardCalled = true
168
- }
169
- return "", nil
170
- }
171
-
172
- // simulate a normal Snapshot() call which calls SnapshotExtension()
173
- swingsetSnapshotter.takeSnapshot = func(height int64) {
174
- swingsetSnapshotter.activeSnapshot.retrieved = true
175
- }
176
- err := swingsetSnapshotter.InitiateSnapshot(123)
177
- if err != nil {
178
- t.Fatal(err)
179
- }
180
- <-swingsetSnapshotter.activeSnapshot.done
181
- if discardCalled {
182
- t.Error("didn't want discard called")
183
- }
184
-
185
- // simulate a Snapshot() call which doesn't call SnapshotExtension()
186
- swingsetSnapshotter.takeSnapshot = func(height int64) {}
187
- err = swingsetSnapshotter.InitiateSnapshot(456)
188
- if err != nil {
189
- t.Fatal(err)
190
- }
191
- <-swingsetSnapshotter.activeSnapshot.done
192
- if !discardCalled {
193
- t.Error("wanted discard called")
194
- }
195
- }