Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rename connection to session in tests #63

Merged
merged 1 commit into from
Feb 26, 2023
Merged
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
100 changes: 50 additions & 50 deletions webtransport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func runServer(t *testing.T, s *webtransport.Server) (addr *net.UDPAddr, close f
}
}

func establishConn(t *testing.T, handler func(*webtransport.Session)) (conn *webtransport.Session, close func()) {
func establishSession(t *testing.T, handler func(*webtransport.Session)) (sess *webtransport.Session, close func()) {
s := &webtransport.Server{
H3: http3.Server{
TLSConfig: tlsConf,
Expand All @@ -84,21 +84,21 @@ func establishConn(t *testing.T, handler func(*webtransport.Session)) (conn *web
}
defer d.Close()
url := fmt.Sprintf("https://localhost:%d/webtransport", addr.Port)
rsp, conn, err := d.Dial(context.Background(), url, nil)
rsp, sess, err := d.Dial(context.Background(), url, nil)
require.NoError(t, err)
require.Equal(t, 200, rsp.StatusCode)
return conn, func() {
return sess, func() {
closeServer()
s.Close()
}
}

// opens a new stream on the connection,
// sends data and checks the echoed data.
func sendDataAndCheckEcho(t *testing.T, conn *webtransport.Session) {
func sendDataAndCheckEcho(t *testing.T, sess *webtransport.Session) {
t.Helper()
data := getRandomData(5 * 1024)
str, err := conn.OpenStream()
str, err := sess.OpenStream()
require.NoError(t, err)
str.SetDeadline(time.Now().Add(time.Second))
_, err = str.Write(data)
Expand All @@ -125,9 +125,9 @@ func addHandler(t *testing.T, s *webtransport.Server, connHandler func(*webtrans
}

func newEchoHandler(t *testing.T) func(*webtransport.Session) {
return func(conn *webtransport.Session) {
return func(sess *webtransport.Session) {
for {
str, err := conn.AcceptStream(context.Background())
str, err := sess.AcceptStream(context.Background())
if err != nil {
break
}
Expand All @@ -146,7 +146,7 @@ func getRandomData(l int) []byte {

func TestBidirectionalStreamsDataTransfer(t *testing.T) {
t.Run("client-initiated", func(t *testing.T) {
conn, closeServer := establishConn(t, newEchoHandler(t))
conn, closeServer := establishSession(t, newEchoHandler(t))
defer closeServer()
defer conn.CloseWithError(0, "")

Expand All @@ -155,8 +155,8 @@ func TestBidirectionalStreamsDataTransfer(t *testing.T) {

t.Run("server-initiated", func(t *testing.T) {
done := make(chan struct{})
conn, closeServer := establishConn(t, func(conn *webtransport.Session) {
sendDataAndCheckEcho(t, conn)
conn, closeServer := establishSession(t, func(sess *webtransport.Session) {
sendDataAndCheckEcho(t, sess)
close(done) // don't defer this, the HTTP handler catches panics
})
defer closeServer()
Expand All @@ -171,7 +171,7 @@ func TestStreamsImmediateClose(t *testing.T) {
t.Run("bidirectional streams", func(t *testing.T) {
t.Run("client-initiated", func(t *testing.T) {
done := make(chan struct{})
conn, closeServer := establishConn(t, func(c *webtransport.Session) {
conn, closeServer := establishSession(t, func(c *webtransport.Session) {
str, err := c.AcceptStream(context.Background())
require.NoError(t, err)
n, err := str.Read([]byte{0})
Expand All @@ -194,7 +194,7 @@ func TestStreamsImmediateClose(t *testing.T) {

t.Run("server-initiated", func(t *testing.T) {
done := make(chan struct{})
conn, closeServer := establishConn(t, func(c *webtransport.Session) {
conn, closeServer := establishSession(t, func(c *webtransport.Session) {
str, err := c.OpenStream()
require.NoError(t, err)
require.NoError(t, str.Close())
Expand All @@ -219,7 +219,7 @@ func TestStreamsImmediateClose(t *testing.T) {

t.Run("unidirectional", func(t *testing.T) {
t.Run("client-initiated", func(t *testing.T) {
conn, closeServer := establishConn(t, func(c *webtransport.Session) {
sess, closeServer := establishSession(t, func(c *webtransport.Session) {
defer c.CloseWithError(0, "")
str, err := c.AcceptUniStream(context.Background())
require.NoError(t, err)
Expand All @@ -229,22 +229,22 @@ func TestStreamsImmediateClose(t *testing.T) {
})
defer closeServer()

str, err := conn.OpenUniStream()
str, err := sess.OpenUniStream()
require.NoError(t, err)
require.NoError(t, str.Close())
<-conn.Context().Done()
<-sess.Context().Done()
})

t.Run("server-initiated", func(t *testing.T) {
conn, closeServer := establishConn(t, func(c *webtransport.Session) {
sess, closeServer := establishSession(t, func(c *webtransport.Session) {
str, err := c.OpenUniStream()
require.NoError(t, err)
require.NoError(t, str.Close())
})
defer closeServer()
defer conn.CloseWithError(0, "")
defer sess.CloseWithError(0, "")

str, err := conn.AcceptUniStream(context.Background())
str, err := sess.AcceptUniStream(context.Background())
require.NoError(t, err)
n, err := str.Read([]byte{0})
require.Zero(t, n)
Expand All @@ -259,7 +259,7 @@ func TestStreamsImmediateReset(t *testing.T) {
// quic-go will see a bidirectional stream opened by the server, which is a connection error.
done := make(chan struct{})
defer close(done)
conn, closeServer := establishConn(t, func(c *webtransport.Session) {
sess, closeServer := establishSession(t, func(c *webtransport.Session) {
for i := 0; i < 50; i++ {
str, err := c.OpenStream()
require.NoError(t, err)
Expand All @@ -280,12 +280,12 @@ func TestStreamsImmediateReset(t *testing.T) {
}
})
defer closeServer()
defer conn.CloseWithError(0, "")
defer sess.CloseWithError(0, "")

ctx, cancel := context.WithTimeout(context.Background(), scaleDuration(100*time.Millisecond))
defer cancel()
for {
_, err := conn.AcceptStream(ctx)
_, err := sess.AcceptStream(ctx)
if err == context.DeadlineExceeded {
break
}
Expand All @@ -294,29 +294,29 @@ func TestStreamsImmediateReset(t *testing.T) {
}

func TestUnidirectionalStreams(t *testing.T) {
conn, closeServer := establishConn(t, func(conn *webtransport.Session) {
sess, closeServer := establishSession(t, func(sess *webtransport.Session) {
// Accept a unidirectional stream, read all of its contents,
// and echo it on a newly opened unidirectional stream.
str, err := conn.AcceptUniStream(context.Background())
str, err := sess.AcceptUniStream(context.Background())
require.NoError(t, err)
data, err := io.ReadAll(str)
require.NoError(t, err)
rstr, err := conn.OpenUniStream()
rstr, err := sess.OpenUniStream()
require.NoError(t, err)
_, err = rstr.Write(data)
require.NoError(t, err)
require.NoError(t, rstr.Close())
})
defer closeServer()
defer conn.CloseWithError(0, "")
defer sess.CloseWithError(0, "")

str, err := conn.OpenUniStream()
str, err := sess.OpenUniStream()
require.NoError(t, err)
data := getRandomData(10 * 1024)
_, err = str.Write(data)
require.NoError(t, err)
require.NoError(t, str.Close())
rstr, err := conn.AcceptUniStream(context.Background())
rstr, err := sess.AcceptUniStream(context.Background())
require.NoError(t, err)
rdata, err := io.ReadAll(rstr)
require.NoError(t, err)
Expand Down Expand Up @@ -358,9 +358,9 @@ func TestMultipleClients(t *testing.T) {

func TestStreamResetError(t *testing.T) {
const errorCode webtransport.StreamErrorCode = 127
conn, closeServer := establishConn(t, func(conn *webtransport.Session) {
sess, closeServer := establishSession(t, func(sess *webtransport.Session) {
for {
str, err := conn.AcceptStream(context.Background())
str, err := sess.AcceptStream(context.Background())
if err != nil {
return
}
Expand All @@ -370,7 +370,7 @@ func TestStreamResetError(t *testing.T) {
})
defer closeServer()

str, err := conn.OpenStream()
str, err := sess.OpenStream()
require.NoError(t, err)
_, err = str.Write([]byte("foobar"))
require.NoError(t, err)
Expand All @@ -383,15 +383,15 @@ func TestStreamResetError(t *testing.T) {

func TestShutdown(t *testing.T) {
done := make(chan struct{})
conn, closeServer := establishConn(t, func(conn *webtransport.Session) {
conn.CloseWithError(1337, "foobar")
sess, closeServer := establishSession(t, func(sess *webtransport.Session) {
sess.CloseWithError(1337, "foobar")
var connErr *webtransport.ConnectionError
_, err := conn.OpenStream()
_, err := sess.OpenStream()
require.True(t, errors.As(err, &connErr))
require.False(t, connErr.Remote)
require.Equal(t, webtransport.SessionErrorCode(1337), connErr.ErrorCode)
require.Equal(t, "foobar", connErr.Message)
_, err = conn.OpenUniStream()
_, err = sess.OpenUniStream()
require.True(t, errors.As(err, &connErr))
require.False(t, connErr.Remote)

Expand All @@ -400,12 +400,12 @@ func TestShutdown(t *testing.T) {
defer closeServer()

var connErr *webtransport.ConnectionError
_, err := conn.AcceptStream(context.Background())
_, err := sess.AcceptStream(context.Background())
require.True(t, errors.As(err, &connErr))
require.True(t, connErr.Remote)
require.Equal(t, webtransport.SessionErrorCode(1337), connErr.ErrorCode)
require.Equal(t, "foobar", connErr.Message)
_, err = conn.AcceptUniStream(context.Background())
_, err = sess.AcceptUniStream(context.Background())
require.Error(t, err)
<-done
}
Expand Down Expand Up @@ -440,30 +440,30 @@ func TestOpenStreamSyncShutdown(t *testing.T) {

t.Run("bidirectional streams", func(t *testing.T) {
done := make(chan struct{})
conn, closeServer := establishConn(t, func(conn *webtransport.Session) {
sess, closeServer := establishSession(t, func(sess *webtransport.Session) {
<-done
conn.CloseWithError(0, "")
sess.CloseWithError(0, "")
})
defer closeServer()

runTest(t,
func() error { _, err := conn.OpenStream(); return err },
func() error { _, err := conn.OpenStreamSync(context.Background()); return err },
func() error { _, err := sess.OpenStream(); return err },
func() error { _, err := sess.OpenStreamSync(context.Background()); return err },
done,
)
})

t.Run("unidirectional streams", func(t *testing.T) {
done := make(chan struct{})
conn, closeServer := establishConn(t, func(conn *webtransport.Session) {
sess, closeServer := establishSession(t, func(sess *webtransport.Session) {
<-done
conn.CloseWithError(0, "")
sess.CloseWithError(0, "")
})
defer closeServer()

runTest(t,
func() error { _, err := conn.OpenUniStream(); return err },
func() error { _, err := conn.OpenUniStreamSync(context.Background()); return err },
func() error { _, err := sess.OpenUniStream(); return err },
func() error { _, err := sess.OpenUniStreamSync(context.Background()); return err },
done,
)
})
Expand Down Expand Up @@ -543,17 +543,17 @@ func TestCheckOrigin(t *testing.T) {

func TestCloseStreamsOnSessionClose(t *testing.T) {
accepted := make(chan struct{})
conn, closeServer := establishConn(t, func(conn *webtransport.Session) {
str, err := conn.OpenStream()
sess, closeServer := establishSession(t, func(sess *webtransport.Session) {
str, err := sess.OpenStream()
require.NoError(t, err)
_, err = str.Write([]byte("foobar"))
require.NoError(t, err)
ustr, err := conn.OpenUniStream()
ustr, err := sess.OpenUniStream()
require.NoError(t, err)
_, err = ustr.Write([]byte("foobar"))
require.NoError(t, err)
<-accepted
conn.CloseWithError(0, "")
sess.CloseWithError(0, "")
_, err = str.Read([]byte{0})
require.Error(t, err)
_, err = ustr.Write([]byte{0})
Expand All @@ -563,9 +563,9 @@ func TestCloseStreamsOnSessionClose(t *testing.T) {
})
defer closeServer()

str, err := conn.AcceptStream(context.Background())
str, err := sess.AcceptStream(context.Background())
require.NoError(t, err)
ustr, err := conn.AcceptUniStream(context.Background())
ustr, err := sess.AcceptUniStream(context.Background())
require.NoError(t, err)
close(accepted)
str.Read(make([]byte, 6)) // read the foobar
Expand Down