From 11af55fa0be7aac519fb07d5f0f35c7a3c7d9a55 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Fri, 19 May 2023 11:38:11 +1000 Subject: [PATCH] Retry some utp tests on failure --- ut-holepunching_test.go | 68 +++++++++++++++++++++++++++-------------- 1 file changed, 45 insertions(+), 23 deletions(-) diff --git a/ut-holepunching_test.go b/ut-holepunching_test.go index ba3d6ad8..6ce5589b 100644 --- a/ut-holepunching_test.go +++ b/ut-holepunching_test.go @@ -325,34 +325,44 @@ func TestUtpSimultaneousOpen(t *testing.T) { c.Assert(err, qt.IsNil) return socket } - first := newUtpSocket("localhost:3000") + first := newUtpSocket("localhost:0") defer first.Close() - second := newUtpSocket("localhost:3001") + second := newUtpSocket("localhost:0") defer second.Close() getDial := func(sock utpSocket, addr string) func() (net.Conn, error) { return func() (net.Conn, error) { return sock.DialContext(ctx, network, addr) } } - err := testSimultaneousOpen( - c.Cleanup, - getDial(first, "localhost:3001"), - getDial(second, "localhost:3000"), - ) - c.Assert(err, qt.ErrorIs, errMsgNotReceived) + t.Logf("first addr is %v. second addr is %v", first.Addr().String(), second.Addr().String()) + for range iter.N(10) { + err := testSimultaneousOpen( + c.Cleanup, + getDial(first, second.Addr().String()), + getDial(second, first.Addr().String()), + ) + if err == nil { + t.Fatal("expected utp to fail simultaneous open") + } + if errors.Is(err, errMsgNotReceived) { + return + } + t.Log(err) + time.Sleep(time.Second) + } + t.FailNow() } -func testDirectDialMsg(c *qt.C, r, w net.Conn) { +func writeAndReadMsg(r, w net.Conn) error { go writeMsg(w) - err := readMsg(r) - c.Assert(err, qt.IsNil) + return readMsg(r) } // Show that dialling one socket and accepting from the other results in them having ends of the // same connection. func TestUtpDirectDialMsg(t *testing.T) { c := qt.New(t) - const network = "udp" + const network = "udp4" ctx := context.Background() newUtpSocket := func(addr string) utpSocket { socket, err := NewUtpSocket(network, addr, func(net.Addr) bool { @@ -361,15 +371,27 @@ func TestUtpDirectDialMsg(t *testing.T) { c.Assert(err, qt.IsNil) return socket } - first := newUtpSocket("localhost:0") - defer first.Close() - second := newUtpSocket("localhost:0") - defer second.Close() - writer, err := first.DialContext(ctx, network, second.Addr().String()) - c.Assert(err, qt.IsNil) - defer writer.Close() - reader, err := second.Accept() - defer reader.Close() - c.Assert(err, qt.IsNil) - testDirectDialMsg(c, reader, writer) + for range iter.N(10) { + err := func() error { + first := newUtpSocket("localhost:0") + defer first.Close() + second := newUtpSocket("localhost:0") + defer second.Close() + writer, err := first.DialContext(ctx, network, second.Addr().String()) + if err != nil { + return err + } + defer writer.Close() + reader, err := second.Accept() + defer reader.Close() + c.Assert(err, qt.IsNil) + return writeAndReadMsg(reader, writer) + }() + if err == nil { + return + } + t.Log(err) + time.Sleep(time.Second) + } + t.FailNow() } -- 2.44.0