|
| 1 | +package basichost |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/libp2p/go-libp2p" |
| 9 | + "github.com/libp2p/go-libp2p/core/network" |
| 10 | + "github.com/libp2p/go-libp2p/core/peer" |
| 11 | + "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/client" |
| 12 | + "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/relay" |
| 13 | + ma "github.com/multiformats/go-multiaddr" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | +) |
| 16 | + |
| 17 | +func TestNoStreamOverTransientConnection(t *testing.T) { |
| 18 | + h1, err := libp2p.New( |
| 19 | + libp2p.NoListenAddrs, |
| 20 | + libp2p.EnableRelay(), |
| 21 | + ) |
| 22 | + require.NoError(t, err) |
| 23 | + |
| 24 | + h2, err := libp2p.New( |
| 25 | + libp2p.NoListenAddrs, |
| 26 | + libp2p.EnableRelay(), |
| 27 | + ) |
| 28 | + require.NoError(t, err) |
| 29 | + |
| 30 | + relay1, err := libp2p.New() |
| 31 | + require.NoError(t, err) |
| 32 | + |
| 33 | + _, err = relay.New(relay1) |
| 34 | + require.NoError(t, err) |
| 35 | + |
| 36 | + relay1info := peer.AddrInfo{ |
| 37 | + ID: relay1.ID(), |
| 38 | + Addrs: relay1.Addrs(), |
| 39 | + } |
| 40 | + err = h1.Connect(context.Background(), relay1info) |
| 41 | + require.NoError(t, err) |
| 42 | + |
| 43 | + err = h2.Connect(context.Background(), relay1info) |
| 44 | + require.NoError(t, err) |
| 45 | + |
| 46 | + h2.SetStreamHandler("/testprotocol", func(s network.Stream) { |
| 47 | + fmt.Println("testprotocol") |
| 48 | + |
| 49 | + // End the example |
| 50 | + s.Close() |
| 51 | + }) |
| 52 | + |
| 53 | + _, err = client.Reserve(context.Background(), h2, relay1info) |
| 54 | + require.NoError(t, err) |
| 55 | + |
| 56 | + relayaddr := ma.StringCast("/p2p/" + relay1info.ID.String() + "/p2p-circuit/p2p/" + h2.ID().String()) |
| 57 | + |
| 58 | + h2Info := peer.AddrInfo{ |
| 59 | + ID: h2.ID(), |
| 60 | + Addrs: []ma.Multiaddr{relayaddr}, |
| 61 | + } |
| 62 | + err = h1.Connect(context.Background(), h2Info) |
| 63 | + require.NoError(t, err) |
| 64 | + |
| 65 | + ctx := network.WithNoDial(context.Background(), "test") |
| 66 | + _, err = h1.NewStream(ctx, h2.ID(), "/testprotocol") |
| 67 | + |
| 68 | + require.ErrorIs(t, err, network.ErrTransientConn) |
| 69 | + |
| 70 | + _, err = h1.NewStream(network.WithUseTransient(context.Background(), "test"), h2.ID(), "/testprotocol") |
| 71 | + require.NoError(t, err) |
| 72 | +} |
0 commit comments