Skip to content

Commit

Permalink
add test for 1000 connection establishment
Browse files Browse the repository at this point in the history
  • Loading branch information
sukunrt committed Mar 13, 2024
1 parent 7a38e53 commit f75b7b1
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .github/actions/go-test-setup/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ runs:
- name: Run nocover tests. These are tests that require the coverage analysis to be off # See https://github.com/protocol/.github/issues/460
shell: bash
# This matches only tests with "NoCover" in their test name to avoid running all tests again.
run: go test -tags nocover -run NoCover -v ./...
run: GOLOG_LOG_LEVEL=debug go test -v -run TestTransportWebRTC ./...

3 changes: 3 additions & 0 deletions .github/workflows/go-test-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"skipOSes": ["windows", "linux", "ubuntu"]
}
2 changes: 1 addition & 1 deletion p2p/transport/webrtc/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ func addOnConnectionStateChangeCallback(pc *webrtc.PeerConnection, side string)
errC := make(chan error, 1)
var once sync.Once
pc.OnConnectionStateChange(func(state webrtc.PeerConnectionState) {
fmt.Println(side, "connection state: ", state)
fmt.Printf("%p: %s connection state: %v\n", pc, side, state)
switch state {
case webrtc.PeerConnectionStateConnected:
once.Do(func() { close(errC) })
Expand Down
76 changes: 76 additions & 0 deletions p2p/transport/webrtc/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -797,3 +797,79 @@ func TestMaxInFlightRequests(t *testing.T) {
require.Equal(t, count, int(success.Load()), "expected exactly 3 dial successes")
require.Equal(t, 1, int(fails.Load()), "expected exactly 1 dial failure")
}

func TestTransportWebRTC_ManyConnections(t *testing.T) {
tr, listeningPeer := getTransport(t)
listenMultiaddr := ma.StringCast("/ip4/127.0.0.1/udp/0/webrtc-direct")
listener, err := tr.Listen(listenMultiaddr)
require.NoError(t, err)
defer listener.Close()

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

N := 1000
errs := make(chan error, N)

// exits on listener close
go func() {
for i := 0; i < N; i++ {
lconn, err := listener.Accept()
if err != nil {
return
}
go func() {
stream, err := lconn.AcceptStream()
if err != nil {
return
}
stream.Write([]byte{1, 2, 3, 4})
buf := make([]byte, 10)
stream.Read(buf)
lconn.Close()
}()
}
}()

dialAndReceiveData := func() {
var err error
defer func() {
errs <- err
}()
tr1, _ := getTransport(t)
conn, err := tr1.Dial(ctx, listener.Multiaddr(), listeningPeer)
if !assert.NoError(t, err) {
return
}
defer conn.Close()
// create a stream
stream, err := conn.OpenStream(context.Background())
if !assert.NoError(t, err) {
return
}
stream.SetReadDeadline(time.Now().Add(30 * time.Second))
buf := make([]byte, 10)
n, err := stream.Read(buf)
if !assert.NoError(t, err) {
return
}
if !assert.Equal(t, 4, n) {
return
}
}

go func() {
for i := 0; i < N; i++ {
dialAndReceiveData()
fmt.Println("completed connection", i)
}
}()

for i := 0; i < N; i++ {
err := <-errs
if err != nil {
t.Fatal(err)
return
}
}
}

0 comments on commit f75b7b1

Please sign in to comment.