Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

chore: fix dial test assertions #428

Merged
merged 1 commit into from
Sep 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions wsnet/dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,8 @@ func (d *Dialer) negotiate(ctx context.Context) (err error) {
return <-errCh
}

// ActiveConnections returns the amount of active connections.
// DialContext opens a connection, and close will end it.
// ActiveConnections returns the amount of active connections. DialContext
// opens a connection, and close will end it.
func (d *Dialer) activeConnections() int {
stats, ok := d.rtc.GetStats().GetConnectionStats(d.rtc)
if !ok {
Expand Down
33 changes: 18 additions & 15 deletions wsnet/dial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ import (
"testing"
"time"

"cdr.dev/slog/sloggers/slogtest"
"github.com/pion/ice/v2"
"github.com/pion/webrtc/v3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"cdr.dev/slog/sloggers/slogtest"
)

func ExampleDial_basic() {
Expand Down Expand Up @@ -260,33 +261,35 @@ func TestDial(t *testing.T) {
log := slogtest.Make(t, nil)

listener, err := net.Listen("tcp", "0.0.0.0:0")
if err != nil {
t.Error(err)
return
}
require.NoError(t, err)

go func() {
_, _ = listener.Accept()
}()

connectAddr, listenAddr := createDumbBroker(t)
_, err = Listen(context.Background(), slogtest.Make(t, nil), listenAddr, "")
if err != nil {
t.Error(err)
return
}
require.NoError(t, err)

dialer, err := DialWebsocket(context.Background(), connectAddr, &DialOptions{
Log: &log,
}, nil)
if err != nil {
t.Error(err)
}
conn, _ := dialer.DialContext(context.Background(), listener.Addr().Network(), listener.Addr().String())
require.NoError(t, err)

conn, err := dialer.DialContext(context.Background(), listener.Addr().Network(), listener.Addr().String())
require.NoError(t, err)
assert.Equal(t, 1, dialer.activeConnections())

_ = conn.Close()
assert.Equal(t, 0, dialer.activeConnections())
_, _ = dialer.DialContext(context.Background(), listener.Addr().Network(), listener.Addr().String())
conn, _ = dialer.DialContext(context.Background(), listener.Addr().Network(), listener.Addr().String())

_, err = dialer.DialContext(context.Background(), listener.Addr().Network(), listener.Addr().String())
require.NoError(t, err)

conn, err = dialer.DialContext(context.Background(), listener.Addr().Network(), listener.Addr().String())
require.NoError(t, err)
assert.Equal(t, 2, dialer.activeConnections())

_ = conn.Close()
assert.Equal(t, 1, dialer.activeConnections())
})
Expand Down