|
| 1 | +import subprocess |
| 2 | +from typing import ( |
| 3 | + Optional, |
| 4 | +) |
| 5 | + |
| 6 | +import pytest |
| 7 | +import multiaddr |
| 8 | + |
| 9 | +from libp2p.transport.quic.transport import ( |
| 10 | + QuicTransport, |
| 11 | +) |
| 12 | + |
| 13 | + |
| 14 | +class ExternalLibp2pNode: |
| 15 | + """Helper class to run external libp2p nodes for interop testing.""" |
| 16 | + |
| 17 | + def __init__(self, implementation: str): |
| 18 | + """ |
| 19 | + Initialize external node runner. |
| 20 | + implementation: One of 'go', 'js' |
| 21 | + """ |
| 22 | + self.implementation = implementation |
| 23 | + self.process: Optional[subprocess.Popen] = None |
| 24 | + self.multiaddr: Optional[multiaddr.Multiaddr] = None |
| 25 | + |
| 26 | + async def start(self) -> multiaddr.Multiaddr: |
| 27 | + """Start the external node and return its multiaddr.""" |
| 28 | + if self.implementation == "go": |
| 29 | + cmd = ["go", "run", "./cmd/ping/main.go"] |
| 30 | + elif self.implementation == "js": |
| 31 | + cmd = ["node", "./examples/ping/index.js"] |
| 32 | + else: |
| 33 | + raise ValueError(f"Unknown implementation: {self.implementation}") |
| 34 | + |
| 35 | + # Start process and wait for it to output its multiaddr |
| 36 | + self.process = subprocess.Popen( |
| 37 | + cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True |
| 38 | + ) |
| 39 | + |
| 40 | + # Wait for multiaddr in output |
| 41 | + while True: |
| 42 | + line = self.process.stdout.readline() |
| 43 | + if "Listening on:" in line: |
| 44 | + self.multiaddr = multiaddr.Multiaddr( |
| 45 | + line.split("Listening on:")[1].strip() |
| 46 | + ) |
| 47 | + break |
| 48 | + |
| 49 | + # Check if process died |
| 50 | + if self.process.poll() is not None: |
| 51 | + raise RuntimeError( |
| 52 | + f"External node failed to start: {self.process.stderr.read()}" |
| 53 | + ) |
| 54 | + |
| 55 | + return self.multiaddr |
| 56 | + |
| 57 | + async def stop(self): |
| 58 | + """Stop the external node.""" |
| 59 | + if self.process: |
| 60 | + self.process.terminate() |
| 61 | + try: |
| 62 | + self.process.wait(timeout=5) |
| 63 | + except subprocess.TimeoutExpired: |
| 64 | + self.process.kill() |
| 65 | + self.process = None |
| 66 | + self.multiaddr = None |
| 67 | + |
| 68 | + |
| 69 | +@pytest.mark.interop |
| 70 | +@pytest.mark.asyncio |
| 71 | +async def test_quic_go_interop(): |
| 72 | + """Test QUIC interoperability with go-libp2p.""" |
| 73 | + # Start go-libp2p node |
| 74 | + go_node = ExternalLibp2pNode("go") |
| 75 | + go_addr = await go_node.start() |
| 76 | + |
| 77 | + try: |
| 78 | + # Create py-libp2p client |
| 79 | + client = QuicTransport() |
| 80 | + |
| 81 | + # Connect to go node |
| 82 | + protocol, peer_info = await client.dial(go_addr) |
| 83 | + |
| 84 | + # Test data transfer |
| 85 | + stream = await protocol.open_stream() |
| 86 | + test_data = b"Hello from Python!" |
| 87 | + |
| 88 | + await stream.write(test_data) |
| 89 | + response = await stream.read() |
| 90 | + |
| 91 | + assert response == test_data |
| 92 | + |
| 93 | + await stream.close() |
| 94 | + await client.close() |
| 95 | + |
| 96 | + finally: |
| 97 | + await go_node.stop() |
| 98 | + |
| 99 | + |
| 100 | +@pytest.mark.interop |
| 101 | +@pytest.mark.asyncio |
| 102 | +async def test_quic_js_interop(): |
| 103 | + """Test QUIC interoperability with js-libp2p.""" |
| 104 | + # Start js-libp2p node |
| 105 | + js_node = ExternalLibp2pNode("js") |
| 106 | + js_addr = await js_node.start() |
| 107 | + |
| 108 | + try: |
| 109 | + # Create py-libp2p client |
| 110 | + client = QuicTransport() |
| 111 | + |
| 112 | + # Connect to js node |
| 113 | + protocol, peer_info = await client.dial(js_addr) |
| 114 | + |
| 115 | + # Test data transfer |
| 116 | + stream = await protocol.open_stream() |
| 117 | + test_data = b"Hello from Python!" |
| 118 | + |
| 119 | + await stream.write(test_data) |
| 120 | + response = await stream.read() |
| 121 | + |
| 122 | + assert response == test_data |
| 123 | + |
| 124 | + await stream.close() |
| 125 | + await client.close() |
| 126 | + |
| 127 | + finally: |
| 128 | + await js_node.stop() |
| 129 | + |
| 130 | + |
| 131 | +@pytest.mark.interop |
| 132 | +@pytest.mark.asyncio |
| 133 | +async def test_quic_py_server_interop(): |
| 134 | + """Test other libp2p implementations connecting to py-libp2p QUIC server.""" |
| 135 | + # Start py-libp2p server |
| 136 | + server = QuicTransport(host="127.0.0.1", port=0) |
| 137 | + await server.listen() |
| 138 | + |
| 139 | + multiaddr.Multiaddr(f"/ip4/127.0.0.1/udp/{server.port}/quic") |
| 140 | + |
| 141 | + # Test with go client |
| 142 | + go_node = ExternalLibp2pNode("go") |
| 143 | + try: |
| 144 | + await go_node.start() |
| 145 | + # Connection test logic here |
| 146 | + finally: |
| 147 | + await go_node.stop() |
| 148 | + |
| 149 | + # Test with js client |
| 150 | + js_node = ExternalLibp2pNode("js") |
| 151 | + try: |
| 152 | + await js_node.start() |
| 153 | + # Connection test logic here |
| 154 | + finally: |
| 155 | + await js_node.stop() |
| 156 | + |
| 157 | + await server.close() |
0 commit comments