-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfailover_test.go
More file actions
95 lines (88 loc) · 2.2 KB
/
Copy pathfailover_test.go
File metadata and controls
95 lines (88 loc) · 2.2 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package ignite
import (
"context"
"fmt"
"github.com/stretchr/testify/suite"
testing2 "github.com/source-c/go-ignit-thin/internal/testing"
"math/rand"
"sync"
"sync/atomic"
"testing"
"time"
)
type FailoverTestSuite struct {
testing2.IgniteTestSuite
}
func TestFailoverTestSuite(t *testing.T) {
suite.Run(t, new(FailoverTestSuite))
}
func (suite *FailoverTestSuite) TearDownSuite() {
suite.KillAllGrids()
}
func (suite *FailoverTestSuite) AfterTest() {
suite.KillAllGrids()
}
func (suite *FailoverTestSuite) TestFailover() {
_, err := suite.StartIgnite()
if err != nil {
suite.T().Fatal("failed to start first grid", err)
}
_, err = suite.StartIgnite()
if err != nil {
suite.T().Fatal("failed to start second grid", err)
}
cli, err := StartTestClient(context.Background(), WithAddresses(defaultAddress+":10800", defaultAddress+":10801"), WithShuffleAddresses(false))
if err != nil {
suite.T().Fatal("Failed to start client", err)
return
}
defer func() {
_ = cli.Close(context.Background())
}()
cache, err := cli.GetOrCreateCache(context.Background(), "test")
if err != nil {
suite.T().Fatal("Failed to create cache", err)
}
var errCnt atomic.Int64
var successCnt atomic.Int64
stopCh := make(chan struct{})
var wg sync.WaitGroup
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
defer func() {
wg.Done()
}()
rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
LOOP:
for {
select {
case <-stopCh:
return
default:
key := rnd.Int63n(1 << 15)
err0 := cache.Put(context.Background(), fmt.Sprintf("key-%d", key), "test")
if err0 != nil {
suite.T().Log("put failed", err0)
errCnt.Add(1)
} else {
successCnt.Add(1)
}
continue LOOP
}
}
}()
}
testing2.WaitForCondition(func() bool {
return successCnt.Load() >= 1000
}, 3*time.Second)
_ = suite.KillIgnite(0)
currOk := successCnt.Load()
testing2.WaitForCondition(func() bool {
return successCnt.Load() > 2*currOk
}, 3*time.Second)
close(stopCh)
wg.Wait()
suite.T().Logf("Total errors %d, total ok %d", errCnt.Load(), successCnt.Load())
suite.Assert().True(errCnt.Load() == 0, fmt.Sprintf("Total errors %d, total ok %d", errCnt.Load(), successCnt.Load()))
}