MCPcopy Index your code
hub / github.com/garethgeorge/backrest / waitForConnectionState

Function waitForConnectionState

internal/api/syncapi/syncapi_test.go:638–679  ·  view source on GitHub ↗
(t *testing.T, ctx context.Context, peer *peerUnderTest, hostPeer *v1.Multihost_Peer, wantState v1sync.ConnectionState)

Source from the content-addressed store, hash-verified

636}
637
638func waitForConnectionState(t *testing.T, ctx context.Context, peer *peerUnderTest, hostPeer *v1.Multihost_Peer, wantState v1sync.ConnectionState) {
639 ctx, cancel := testutil.WithDeadlineFromTest(t, ctx)
640 defer cancel()
641
642 // Important that we subscribe to the state change before we try the initial check to avoid races.
643 onStateChanged := peer.manager.peerStateManager.OnStateChanged().Subscribe()
644 defer peer.manager.peerStateManager.OnStateChanged().Unsubscribe(onStateChanged)
645
646 // First check if the peer is already connected.
647 state := peer.manager.peerStateManager.GetPeerState(hostPeer.Keyid)
648 if state != nil && state.ConnectionState == v1sync.ConnectionState_CONNECTION_STATE_CONNECTED {
649 return // Already connected, nothing to do
650 }
651
652 // If not connected, wait for a connection event
653 var lastState *PeerState
654 stop := false
655 for !stop {
656 select {
657 case state, ok := <-onStateChanged:
658 if !ok {
659 stop = true
660 continue
661 }
662 if state.KeyID == hostPeer.Keyid && state.InstanceID == hostPeer.InstanceId {
663 lastState = state
664 if state.ConnectionState == v1sync.ConnectionState_CONNECTION_STATE_CONNECTED {
665 stop = true
666 continue
667 }
668 }
669 case <-ctx.Done():
670 stop = true
671 continue
672 }
673 }
674 if lastState == nil {
675 t.Fatalf("timeout waiting for connection to host peer %s", hostPeer.InstanceId)
676 } else if lastState.ConnectionState != v1sync.ConnectionState_CONNECTION_STATE_CONNECTED {
677 t.Fatalf("expected connection state to be CONNECTED, got %v (reason: %q)", lastState.ConnectionState, lastState.ConnectionStateMessage)
678 }
679}
680
681func tryConnect(t *testing.T, ctx context.Context, peer *peerUnderTest, hostPeer *v1.Multihost_Peer) {
682 waitForConnectionState(t, ctx, peer, hostPeer, v1sync.ConnectionState_CONNECTION_STATE_CONNECTED)

Callers 5

tryConnectFunction · 0.85

Calls 6

WithDeadlineFromTestFunction · 0.92
DoneMethod · 0.80
SubscribeMethod · 0.65
OnStateChangedMethod · 0.65
UnsubscribeMethod · 0.65
GetPeerStateMethod · 0.65

Tested by

no test coverage detected