| 1 |
package desync |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"crypto/rand" |
| 6 |
"sync/atomic" |
| 7 |
"testing" |
| 8 |
"time" |
| 9 |
|
| 10 |
"github.com/pkg/errors" |
| 11 |
"github.com/stretchr/testify/require" |
| 12 |
"golang.org/x/sync/errgroup" |
| 13 |
) |
| 14 |
|
| 15 |
func TestFailoverMissingChunk(t *testing.T) { |
| 16 |
s := &TestStore{} |
| 17 |
g := NewFailoverGroup(s) |
| 18 |
_, err := g.GetChunk(ChunkID{0}) |
| 19 |
require.IsType(t, ChunkMissing{}, err) |
| 20 |
} |
| 21 |
|
| 22 |
func TestFailoverAllError(t *testing.T) { |
| 23 |
var failed = errors.New("failed") |
| 24 |
storeFail := &TestStore{ |
| 25 |
GetChunkFunc: func(ChunkID) (*Chunk, error) { return nil, failed }, |
| 26 |
} |
| 27 |
g := NewFailoverGroup(storeFail, storeFail) |
| 28 |
_, err := g.GetChunk(ChunkID{0}) |
| 29 |
require.ErrorIs(t, err, failed) |
| 30 |
} |
| 31 |
|
| 32 |
func TestFailoverSimple(t *testing.T) { |
| 33 |
// Create two stores, one that always fails and one that works |
| 34 |
storeFail := &TestStore{ |
| 35 |
GetChunkFunc: func(ChunkID) (*Chunk, error) { return nil, errors.New("failed") }, |
| 36 |
} |
| 37 |
storeSucc := &TestStore{ |
| 38 |
GetChunkFunc: func(ChunkID) (*Chunk, error) { return nil, nil }, |
| 39 |
} |
| 40 |
|
| 41 |
// Group the two stores together, the failing ones first |
| 42 |
g := NewFailoverGroup(storeFail, storeFail, storeSucc) |
| 43 |
|
| 44 |
// Request a chunk, should succeed |
| 45 |
_, err := g.GetChunk(ChunkID{0}) |
| 46 |
require.NoError(t, err) |
| 47 |
|
| 48 |
// Look inside the group to confirm we failed over to the last one |
| 49 |
require.Equal(t, 2, g.active) |
| 50 |
} |
| 51 |
|
| 52 |
func TestFailoverMutliple(t *testing.T) { |
| 53 |
// Create two stores, one that fails when x is 1 and the other fails when x is 0 |
| 54 |
var x int64 |
| 55 |
storeA := &TestStore{ |
| 56 |
GetChunkFunc: func(id ChunkID) (*Chunk, error) { |
| 57 |
if atomic.LoadInt64(&x) == 0 { |
| 58 |
return nil, nil |
| 59 |
} |
| 60 |
return nil, errors.New("failed") |
| 61 |
}, |
| 62 |
} |
| 63 |
storeB := &TestStore{ |
| 64 |
GetChunkFunc: func(id ChunkID) (*Chunk, error) { |
| 65 |
if atomic.LoadInt64(&x) == 1 { |
| 66 |
return nil, nil |
| 67 |
} |
| 68 |
return nil, errors.New("failed") |
| 69 |
}, |
| 70 |
} |
| 71 |
|
| 72 |
// Group the two stores together, the failing ones first |
| 73 |
g := NewFailoverGroup(storeA, storeB) |
| 74 |
|
| 75 |
var ( |
| 76 |
ctx, cancel = context.WithTimeout(t.Context(), time.Second) |
| 77 |
eg, gCtx = errgroup.WithContext(ctx) |
| 78 |
failOver = time.Tick(10 * time.Millisecond) |
| 79 |
) |
| 80 |
defer cancel() |
| 81 |
|
| 82 |
// Run several goroutines querying the group in a tight loop |
| 83 |
for range 16 { |
| 84 |
eg.Go(func() error { |
| 85 |
var id ChunkID |
| 86 |
for { |
| 87 |
time.Sleep(time.Millisecond) |
| 88 |
select { |
| 89 |
case <-gCtx.Done(): |
| 90 |
return nil |
| 91 |
default: |
| 92 |
rand.Read(id[:]) |
| 93 |
if _, err := g.GetChunk(id); err != nil { |
| 94 |
return err |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
}) |
| 99 |
} |
| 100 |
|
| 101 |
// Make the stores fail over every 10 ms |
| 102 |
eg.Go(func() error { |
| 103 |
for { |
| 104 |
select { |
| 105 |
case <-gCtx.Done(): // done running |
| 106 |
return nil |
| 107 |
case <-failOver: // switch over to the other store |
| 108 |
newX := (x + 1) % 2 |
| 109 |
atomic.StoreInt64(&x, newX) |
| 110 |
} |
| 111 |
} |
| 112 |
}) |
| 113 |
|
| 114 |
require.NoError(t, eg.Wait()) |
| 115 |
} |