4
4
"context"
5
5
"errors"
6
6
"fmt"
7
+ "sync"
7
8
"testing"
8
9
"time"
9
10
@@ -14,6 +15,8 @@ import (
14
15
15
16
// StoreMock implements a mock client swap store.
16
17
type StoreMock struct {
18
+ sync.RWMutex
19
+
17
20
LoopOutSwaps map [lntypes.Hash ]* LoopOutContract
18
21
LoopOutUpdates map [lntypes.Hash ][]SwapStateData
19
22
loopOutStoreChan chan LoopOutContract
@@ -50,6 +53,9 @@ func NewStoreMock(t *testing.T) *StoreMock {
50
53
//
51
54
// NOTE: Part of the SwapStore interface.
52
55
func (s * StoreMock ) FetchLoopOutSwaps (ctx context.Context ) ([]* LoopOut , error ) {
56
+ s .RLock ()
57
+ defer s .RUnlock ()
58
+
53
59
result := []* LoopOut {}
54
60
55
61
for hash , contract := range s .LoopOutSwaps {
@@ -80,6 +86,9 @@ func (s *StoreMock) FetchLoopOutSwaps(ctx context.Context) ([]*LoopOut, error) {
80
86
func (s * StoreMock ) FetchLoopOutSwap (ctx context.Context ,
81
87
hash lntypes.Hash ) (* LoopOut , error ) {
82
88
89
+ s .RLock ()
90
+ defer s .RUnlock ()
91
+
83
92
contract , ok := s .LoopOutSwaps [hash ]
84
93
if ! ok {
85
94
return nil , errors .New ("swap not found" )
@@ -110,6 +119,9 @@ func (s *StoreMock) FetchLoopOutSwap(ctx context.Context,
110
119
func (s * StoreMock ) CreateLoopOut (ctx context.Context , hash lntypes.Hash ,
111
120
swap * LoopOutContract ) error {
112
121
122
+ s .Lock ()
123
+ defer s .Unlock ()
124
+
113
125
_ , ok := s .LoopOutSwaps [hash ]
114
126
if ok {
115
127
return errors .New ("swap already exists" )
@@ -126,6 +138,9 @@ func (s *StoreMock) CreateLoopOut(ctx context.Context, hash lntypes.Hash,
126
138
func (s * StoreMock ) FetchLoopInSwaps (ctx context.Context ) ([]* LoopIn ,
127
139
error ) {
128
140
141
+ s .RLock ()
142
+ defer s .RUnlock ()
143
+
129
144
result := []* LoopIn {}
130
145
131
146
for hash , contract := range s .LoopInSwaps {
@@ -156,6 +171,9 @@ func (s *StoreMock) FetchLoopInSwaps(ctx context.Context) ([]*LoopIn,
156
171
func (s * StoreMock ) CreateLoopIn (ctx context.Context , hash lntypes.Hash ,
157
172
swap * LoopInContract ) error {
158
173
174
+ s .Lock ()
175
+ defer s .Unlock ()
176
+
159
177
_ , ok := s .LoopInSwaps [hash ]
160
178
if ok {
161
179
return errors .New ("swap already exists" )
@@ -176,6 +194,9 @@ func (s *StoreMock) CreateLoopIn(ctx context.Context, hash lntypes.Hash,
176
194
func (s * StoreMock ) UpdateLoopOut (ctx context.Context , hash lntypes.Hash ,
177
195
time time.Time , state SwapStateData ) error {
178
196
197
+ s .Lock ()
198
+ defer s .Unlock ()
199
+
179
200
updates , ok := s .LoopOutUpdates [hash ]
180
201
if ! ok {
181
202
return errors .New ("swap does not exists" )
@@ -196,6 +217,9 @@ func (s *StoreMock) UpdateLoopOut(ctx context.Context, hash lntypes.Hash,
196
217
func (s * StoreMock ) UpdateLoopIn (ctx context.Context , hash lntypes.Hash ,
197
218
time time.Time , state SwapStateData ) error {
198
219
220
+ s .Lock ()
221
+ defer s .Unlock ()
222
+
199
223
updates , ok := s .LoopInUpdates [hash ]
200
224
if ! ok {
201
225
return errors .New ("swap does not exists" )
@@ -347,6 +371,9 @@ func (b *StoreMock) BatchInsertUpdate(ctx context.Context,
347
371
func (s * StoreMock ) BatchUpdateLoopOutSwapCosts (ctx context.Context ,
348
372
costs map [lntypes.Hash ]SwapCost ) error {
349
373
374
+ s .Lock ()
375
+ defer s .Unlock ()
376
+
350
377
for hash , cost := range costs {
351
378
if _ , ok := s .LoopOutUpdates [hash ]; ! ok {
352
379
return fmt .Errorf ("swap has no updates: %v" , hash )
@@ -367,6 +394,9 @@ func (s *StoreMock) BatchUpdateLoopOutSwapCosts(ctx context.Context,
367
394
func (s * StoreMock ) HasMigration (ctx context.Context , migrationID string ) (
368
395
bool , error ) {
369
396
397
+ s .RLock ()
398
+ defer s .RUnlock ()
399
+
370
400
_ , ok := s .migrations [migrationID ]
371
401
372
402
return ok , nil
@@ -376,6 +406,9 @@ func (s *StoreMock) HasMigration(ctx context.Context, migrationID string) (
376
406
func (s * StoreMock ) SetMigration (ctx context.Context ,
377
407
migrationID string ) error {
378
408
409
+ s .Lock ()
410
+ defer s .Unlock ()
411
+
379
412
if _ , ok := s .migrations [migrationID ]; ok {
380
413
return errors .New ("migration already done" )
381
414
}
0 commit comments