MCPcopy Create free account
hub / github.com/DeAI-Artist/Linkis / TestSwitchReconnectsToOutboundPersistentPeer

Function TestSwitchReconnectsToOutboundPersistentPeer

p2p/switch_test.go:490–538  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

488}
489
490func TestSwitchReconnectsToOutboundPersistentPeer(t *testing.T) {
491 sw := MakeSwitch(cfg, 1, "testing", "123.123.123", initSwitchFunc)
492 err := sw.Start()
493 require.NoError(t, err)
494 t.Cleanup(func() {
495 if err := sw.Stop(); err != nil {
496 t.Error(err)
497 }
498 })
499
500 // 1. simulate failure by closing connection
501 rp := &remotePeer{PrivKey: ed25519.GenPrivKey(), Config: cfg}
502 rp.Start()
503 defer rp.Stop()
504
505 err = sw.AddPersistentPeers([]string{rp.Addr().String()})
506 require.NoError(t, err)
507
508 err = sw.DialPeerWithAddress(rp.Addr())
509 require.Nil(t, err)
510 require.NotNil(t, sw.Peers().Get(rp.ID()))
511
512 p := sw.Peers().List()[0]
513 err = p.(*peer).CloseConn()
514 require.NoError(t, err)
515
516 waitUntilSwitchHasAtLeastNPeers(sw, 1)
517 assert.False(t, p.IsRunning()) // old peer instance
518 assert.Equal(t, 1, sw.Peers().Size()) // new peer instance
519
520 // 2. simulate first time dial failure
521 rp = &remotePeer{
522 PrivKey: ed25519.GenPrivKey(),
523 Config: cfg,
524 // Use different interface to prevent duplicate IP filter, this will break
525 // beyond two peers.
526 listenAddr: "127.0.0.1:0",
527 }
528 rp.Start()
529 defer rp.Stop()
530
531 conf := config.DefaultP2PConfig()
532 conf.TestDialFail = true // will trigger a reconnect
533 err = sw.addOutboundPeerWithConfig(rp.Addr(), conf)
534 require.NotNil(t, err)
535 // DialPeerWithAddres - sw.peerConfig resets the dialer
536 waitUntilSwitchHasAtLeastNPeers(sw, 2)
537 assert.Equal(t, 2, sw.Peers().Size())
538}
539
540func TestSwitchReconnectsToInboundPersistentPeer(t *testing.T) {
541 sw := MakeSwitch(cfg, 1, "testing", "123.123.123", initSwitchFunc)

Callers

nothing calls this directly

Calls 15

StartMethod · 0.95
StopMethod · 0.95
AddrMethod · 0.95
IDMethod · 0.95
GenPrivKeyFunction · 0.92
DefaultP2PConfigFunction · 0.92
MakeSwitchFunction · 0.85
DialPeerWithAddressMethod · 0.80
StartMethod · 0.65
CleanupMethod · 0.65

Tested by

no test coverage detected