-
-
Notifications
You must be signed in to change notification settings - Fork 536
/
Copy pathmanager_test.go
156 lines (133 loc) · 3.45 KB
/
manager_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
package disthash
import (
"fmt"
"github.com/asynkron/protoactor-go/actor"
"github.com/asynkron/protoactor-go/cluster"
"github.com/asynkron/protoactor-go/cluster/clusterproviders/test"
"github.com/asynkron/protoactor-go/remote"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"sync"
"testing"
"time"
)
func TestManagerConcurrentAccess(t *testing.T) {
system := actor.NewActorSystem()
provider := test.NewTestProvider(test.NewInMemAgent())
lookup := New()
config := cluster.Configure("test-cluster", provider, lookup, remote.Configure("127.0.0.1", 0))
c := cluster.New(system, config)
manager := newPartitionManager(c)
manager.Start()
defer manager.Stop()
// Create a WaitGroup to synchronize goroutines
var wg sync.WaitGroup
iterations := 1000
// Simulate concurrent topology updates and lookups
wg.Add(2)
// Goroutine 1: Continuously update topology
go func() {
defer wg.Done()
for i := 0; i < iterations; i++ {
members := []*cluster.Member{
{Id: "1", Host: "localhost", Port: 1},
{Id: "2", Host: "localhost", Port: 2},
}
topology := &cluster.ClusterTopology{
Members: members,
TopologyHash: uint64(i),
}
manager.onClusterTopology(topology)
}
}()
// Goroutine 2: Continuously perform lookups
go func() {
defer wg.Done()
for i := 0; i < iterations; i++ {
identity := &cluster.ClusterIdentity{
Identity: "test",
Kind: "test",
}
_ = manager.Get(identity)
}
}()
// Wait with timeout
done := make(chan struct{})
go func() {
wg.Wait()
close(done)
}()
select {
case <-done:
// Test completed successfully
case <-time.After(5 * time.Second):
t.Fatal("Test timed out")
}
}
// Integration test suite
type DistHashManagerTestSuite struct {
suite.Suite
clusters []*cluster.Cluster
}
func (suite *DistHashManagerTestSuite) SetupTest() {
// Create 3 cluster nodes for testing
suite.clusters = make([]*cluster.Cluster, 3)
inMemAgent := test.NewInMemAgent()
for i := 0; i < 3; i++ {
system := actor.NewActorSystem()
provider := test.NewTestProvider(inMemAgent)
config := cluster.Configure("test-cluster",
provider,
New(),
remote.Configure("localhost", 0),
)
c := cluster.New(system, config)
c.StartMember()
suite.clusters[i] = c
}
}
func (suite *DistHashManagerTestSuite) TearDownTest() {
for _, c := range suite.clusters {
c.Shutdown(true)
}
}
func (suite *DistHashManagerTestSuite) TestConcurrentClusterOperations() {
assert.Equal(suite.T(), 3, len(suite.clusters))
// Create multiple concurrent operations
var wg sync.WaitGroup
iterations := 100
for i := 0; i < iterations; i++ {
wg.Add(1)
go func(iteration int) {
defer wg.Done()
// Randomly select a cluster
cluster := suite.clusters[iteration%len(suite.clusters)]
// Perform a Get operation
identity := fmt.Sprintf("test-%d", iteration)
pid := cluster.Get(identity, "test-kind")
// Verify the operation completed without panicking
assert.NotPanics(suite.T(), func() {
if pid != nil {
// Optionally verify the PID properties
assert.NotEmpty(suite.T(), pid.Address)
assert.NotEmpty(suite.T(), pid.Id)
}
})
}(i)
}
// Wait with timeout
done := make(chan struct{})
go func() {
wg.Wait()
close(done)
}()
select {
case <-done:
// Test completed successfully
case <-time.After(10 * time.Second):
suite.T().Fatal("Test timed out")
}
}
func TestDistHashManager(t *testing.T) {
suite.Run(t, new(DistHashManagerTestSuite))
}