Skip to content

Commit

Permalink
add a test case
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Apr 25, 2024
1 parent 1f9790e commit f3741c8
Showing 1 changed file with 64 additions and 1 deletion.
65 changes: 64 additions & 1 deletion webtransport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package webtransport_test

import (
"context"
"crypto/rand"
"crypto/tls"
"errors"
"fmt"
Expand All @@ -15,6 +14,8 @@ import (
"testing"
"time"

"golang.org/x/exp/rand"

"github.com/quic-go/webtransport-go"

"github.com/quic-go/quic-go"
Expand Down Expand Up @@ -595,3 +596,65 @@ func TestWriteCloseRace(t *testing.T) {
<-ready
close(ch)
}

func TestDatagrams(t *testing.T) {
errChan := make(chan error, 1)
sess, closeServer := establishSession(t, func(sess *webtransport.Session) {
for {
d, err := sess.ReceiveDatagram(context.Background())
if err != nil {
errChan <- err
return
}
if err := sess.SendDatagram(d); err != nil {
errChan <- err
return
}
}
})
defer closeServer()

const num = 10
m := make(map[string]bool, num)
var mx sync.Mutex
done := make(chan struct{})
var counter int
go func() {

Check failure on line 622 in webtransport_test.go

View workflow job for this annotation

GitHub Actions / go-check / All

the goroutine calls T.Fatal, which must be called in the same goroutine as the test (SA2002)
defer close(done)
for {
b, err := sess.ReceiveDatagram(context.Background())
fmt.Println("received datagram")
if err != nil {
fmt.Println(err)
return
}
mx.Lock()
if _, ok := m[string(b)]; !ok {
t.Fatal("received unexpected datagram")

Check failure on line 633 in webtransport_test.go

View workflow job for this annotation

GitHub Actions / go-check / All

call to (*T).Fatal from a non-test goroutine
}
m[string(b)] = true
mx.Unlock()
counter++
}
}()
for i := 0; i < num; i++ {
b := make([]byte, 800)
rand.Read(b)
mx.Lock()
m[string(b)] = false
mx.Unlock()
fmt.Println("sending datagram", i)
if err := sess.SendDatagram(b); err != nil {
break
}
}
time.Sleep(time.Second)
sess.CloseWithError(0, "")
select {
case <-done:
t.Logf("sent: %d, received: %d", num, counter)
require.Greater(t, counter, num*4/5)
case <-time.After(5 * time.Second):
t.Fatal("timeout")
}
}

0 comments on commit f3741c8

Please sign in to comment.