types: add AnyDisconnected() method on ConnectivityGroup

This commit is contained in:
c9s 2024-09-27 13:31:37 +08:00
parent c07661af57
commit fb35a2b79f
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 32 additions and 5 deletions

View File

@ -24,14 +24,36 @@ func (g *ConnectivityGroup) Add(con *Connectivity) {
g.connections = append(g.connections, con) g.connections = append(g.connections, con)
} }
func (g *ConnectivityGroup) AnyDisconnected(ctx context.Context) bool {
g.mu.Lock()
conns := g.connections
g.mu.Unlock()
for _, conn := range conns {
select {
case <-ctx.Done():
return false
case <-conn.connectedC:
continue
case <-conn.disconnectedC:
return true
}
}
return false
}
func (g *ConnectivityGroup) waitAllAuthed(ctx context.Context, c chan struct{}, allTimeoutDuration time.Duration) { func (g *ConnectivityGroup) waitAllAuthed(ctx context.Context, c chan struct{}, allTimeoutDuration time.Duration) {
g.mu.Lock() g.mu.Lock()
defer g.mu.Unlock() conns := g.connections
g.mu.Unlock()
authedConns := make([]bool, len(g.connections)) authedConns := make([]bool, len(conns))
allTimeout := time.After(allTimeoutDuration) allTimeout := time.After(allTimeoutDuration)
for { for {
for idx, con := range g.connections { for idx, con := range conns {
// if the connection is not authed, mark it as false // if the connection is not authed, mark it as false
if !con.authed { if !con.authed {
// authedConns[idx] = false // authedConns[idx] = false

View File

@ -39,6 +39,7 @@ func TestConnectivity(t *testing.T) {
func TestConnectivityGroupAuthC(t *testing.T) { func TestConnectivityGroupAuthC(t *testing.T) {
timeout := 100 * time.Millisecond timeout := 100 * time.Millisecond
delay := timeout * 2
ctx := context.Background() ctx := context.Background()
conn1 := NewConnectivity() conn1 := NewConnectivity()
@ -46,13 +47,13 @@ func TestConnectivityGroupAuthC(t *testing.T) {
group := NewConnectivityGroup(conn1, conn2) group := NewConnectivityGroup(conn1, conn2)
allAuthedC := group.AllAuthedC(ctx, time.Second) allAuthedC := group.AllAuthedC(ctx, time.Second)
time.Sleep(timeout) time.Sleep(delay)
conn1.handleConnect() conn1.handleConnect()
assert.True(t, waitSigChan(conn1.ConnectedC(), timeout)) assert.True(t, waitSigChan(conn1.ConnectedC(), timeout))
conn1.handleAuth() conn1.handleAuth()
assert.True(t, waitSigChan(conn1.AuthedC(), timeout)) assert.True(t, waitSigChan(conn1.AuthedC(), timeout))
time.Sleep(timeout) time.Sleep(delay)
conn2.handleConnect() conn2.handleConnect()
assert.True(t, waitSigChan(conn2.ConnectedC(), timeout)) assert.True(t, waitSigChan(conn2.ConnectedC(), timeout))
@ -82,10 +83,14 @@ func TestConnectivityGroupReconnect(t *testing.T) {
assert.True(t, waitSigChan(group.AllAuthedC(ctx, time.Second), timeout), "all connections are authenticated") assert.True(t, waitSigChan(group.AllAuthedC(ctx, time.Second), timeout), "all connections are authenticated")
assert.False(t, group.AnyDisconnected(ctx))
// this should re-allocate authedC // this should re-allocate authedC
conn1.handleDisconnect() conn1.handleDisconnect()
assert.NotEqual(t, conn1authC, conn1.authedC) assert.NotEqual(t, conn1authC, conn1.authedC)
assert.True(t, group.AnyDisconnected(ctx))
assert.False(t, waitSigChan(group.AllAuthedC(ctx, time.Second), timeout), "one connection should be un-authed") assert.False(t, waitSigChan(group.AllAuthedC(ctx, time.Second), timeout), "one connection should be un-authed")
time.Sleep(delay) time.Sleep(delay)