-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathimpl_test.go
118 lines (95 loc) · 2.64 KB
/
impl_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
package substrate
import (
"sync"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestFailoverMechanism(t *testing.T) {
t.Run("should failover to next URL when current node is unhealthy", func(t *testing.T) {
// Create manager with multiple URLs
urls := []string{"ws://fail1", getUrlBasedOnEnv()}
mgr := NewManager(urls...)
// Get initial substrate client
sub, err := mgr.Substrate()
require.NoError(t, err)
defer sub.Close()
// Store initial Client
initialClient := sub.cl.Client
// Force connection to become unhealthy by closing it
sub.cl.Client.Close()
// Try to use the connection - should trigger failover
_, err = sub.Time()
require.NoError(t, err)
// Check that we're now using a different URL
newClient := sub.cl.Client
assert.NotEqual(t, initialClient, newClient)
})
t.Run("should try all URLs in rotation", func(t *testing.T) {
urls := []string{
"ws://fail1",
"ws://fail2",
getUrlBasedOnEnv(),
}
mgr := NewManager(urls...)
sub, err := mgr.Substrate()
require.NoError(t, err)
defer sub.Close()
// The final URL should be the working one
assert.Equal(t, getUrlBasedOnEnv(), sub.cl.Client.URL())
})
t.Run("should reuse connection if healthy", func(t *testing.T) {
sub := startLocalConnection(t)
defer sub.Close()
initialClient := sub.cl.Client
// Use the connection multiple times
for i := 0; i < 3; i++ {
_, err := sub.Time()
require.NoError(t, err)
assert.Equal(t, initialClient, sub.cl.Client)
}
})
t.Run("should handle all nodes being down", func(t *testing.T) {
urls := []string{"ws://fail1", "ws://fail2"}
mgr := NewManager(urls...)
_, err := mgr.Substrate()
assert.Error(t, err)
})
t.Run("should handle concurrent failover attempts", func(t *testing.T) {
urls := []string{getUrlBasedOnEnv(), getUrlBasedOnEnv()}
mgr := NewManager(urls...)
sub1, err := mgr.Substrate()
require.NoError(t, err)
defer sub1.Close()
sub2, err := mgr.Substrate()
require.NoError(t, err)
defer sub2.Close()
// Force both connections to fail
sub1.cl.Client.Close()
sub2.cl.Client.Close()
// Create WaitGroup to ensure all goroutines complete before test ends
var wg sync.WaitGroup
wg.Add(2)
// Try to use both connections concurrently
errs := make(chan error, 2)
go func() {
defer wg.Done()
_, err := sub1.Time()
errs <- err
}()
go func() {
defer wg.Done()
_, err := sub2.Time()
errs <- err
}()
// Wait for both operations to complete
go func() {
wg.Wait()
close(errs)
}()
// Check errors from both goroutines
for err := range errs {
require.NoError(t, err)
}
})
}