-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
webrtc: run onDone callback immediately on close #2729
Merged
+101
−74
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,10 +11,12 @@ import ( | |
|
||
"github.com/libp2p/go-libp2p/p2p/transport/webrtc/pb" | ||
"github.com/libp2p/go-msgio/pbio" | ||
"google.golang.org/protobuf/proto" | ||
|
||
"github.com/libp2p/go-libp2p/core/network" | ||
|
||
"github.com/pion/datachannel" | ||
"github.com/pion/sctp" | ||
"github.com/pion/webrtc/v3" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
@@ -98,6 +100,50 @@ func getDetachedDataChannels(t *testing.T) (detachedChan, detachedChan) { | |
return <-answerChan, <-offerRWCChan | ||
} | ||
|
||
// checkDataChannelClosed checks if the datachannel has been closed. | ||
// It sends empty messages on the data channel to check if the channel is still open. | ||
// The control message reader goroutine depends on exclusive access to datachannel.Read | ||
// so we have to depend on Write to determine whether the channel has been closed. | ||
func checkDataChannelOpen(t *testing.T, dc *datachannel.DataChannel) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit rename these to |
||
t.Helper() | ||
emptyMsg := &pb.Message{} | ||
msg, err := proto.Marshal(emptyMsg) | ||
if err != nil { | ||
t.Fatal("unexpected mashalling error", err) | ||
} | ||
for i := 0; i < 3; i++ { | ||
_, err := dc.Write(msg) | ||
if err != nil { | ||
t.Fatal("unexpected write err: ", err) | ||
} | ||
time.Sleep(50 * time.Millisecond) | ||
} | ||
} | ||
|
||
// checkDataChannelClosed checks if the datachannel has been closed. | ||
// It sends empty messages on the data channel to check if the channel has been closed. | ||
// The control message reader goroutine depends on exclusive access to datachannel.Read | ||
// so we have to depend on Write to determine whether the channel has been closed. | ||
func checkDataChannelClosed(t *testing.T, dc *datachannel.DataChannel) { | ||
t.Helper() | ||
emptyMsg := &pb.Message{} | ||
msg, err := proto.Marshal(emptyMsg) | ||
if err != nil { | ||
t.Fatal("unexpected mashalling error", err) | ||
} | ||
for i := 0; i < 5; i++ { | ||
_, err := dc.Write(msg) | ||
if err != nil { | ||
if errors.Is(err, sctp.ErrStreamClosed) { | ||
return | ||
} else { | ||
t.Fatal("unexpected write err: ", err) | ||
} | ||
} | ||
time.Sleep(50 * time.Millisecond) | ||
} | ||
} | ||
|
||
func TestStreamSimpleReadWriteClose(t *testing.T) { | ||
client, server := getDetachedDataChannels(t) | ||
|
||
|
@@ -357,27 +403,22 @@ func TestStreamCloseAfterFINACK(t *testing.T) { | |
serverStr := newStream(server.dc, server.rwc, func() {}) | ||
|
||
go func() { | ||
done <- true | ||
err := clientStr.Close() | ||
assert.NoError(t, err) | ||
}() | ||
<-done | ||
|
||
select { | ||
case <-done: | ||
t.Fatalf("Close should not have completed without processing FIN_ACK") | ||
case <-time.After(200 * time.Millisecond): | ||
t.Fatalf("Close should signal OnDone immediately") | ||
} | ||
|
||
// Reading FIN_ACK on server should trigger data channel close on the client | ||
b := make([]byte, 1) | ||
_, err := serverStr.Read(b) | ||
require.Error(t, err) | ||
require.ErrorIs(t, err, io.EOF) | ||
select { | ||
case <-done: | ||
case <-time.After(3 * time.Second): | ||
t.Errorf("Close should have completed") | ||
} | ||
checkDataChannelClosed(t, client.rwc.(*datachannel.DataChannel)) | ||
} | ||
|
||
// TestStreamFinAckAfterStopSending tests that FIN_ACK is sent even after the write half | ||
|
@@ -400,8 +441,8 @@ func TestStreamFinAckAfterStopSending(t *testing.T) { | |
|
||
select { | ||
case <-done: | ||
t.Errorf("Close should not have completed without processing FIN_ACK") | ||
case <-time.After(500 * time.Millisecond): | ||
t.Errorf("Close should signal onDone immediately") | ||
} | ||
|
||
// serverStr has write half closed and read half open | ||
|
@@ -410,11 +451,8 @@ func TestStreamFinAckAfterStopSending(t *testing.T) { | |
_, err := serverStr.Read(b) | ||
require.NoError(t, err) | ||
serverStr.Close() // Sends stop_sending, fin | ||
select { | ||
case <-done: | ||
case <-time.After(5 * time.Second): | ||
t.Fatalf("Close should have completed") | ||
} | ||
checkDataChannelClosed(t, server.rwc.(*datachannel.DataChannel)) | ||
checkDataChannelClosed(t, client.rwc.(*datachannel.DataChannel)) | ||
} | ||
|
||
func TestStreamConcurrentClose(t *testing.T) { | ||
|
@@ -446,26 +484,35 @@ func TestStreamConcurrentClose(t *testing.T) { | |
case <-time.After(2 * time.Second): | ||
t.Fatalf("concurrent close should succeed quickly") | ||
} | ||
|
||
// Wait for FIN_ACK AND datachannel close | ||
checkDataChannelClosed(t, client.rwc.(*datachannel.DataChannel)) | ||
checkDataChannelClosed(t, server.rwc.(*datachannel.DataChannel)) | ||
|
||
} | ||
|
||
func TestStreamResetAfterClose(t *testing.T) { | ||
client, _ := getDetachedDataChannels(t) | ||
client, server := getDetachedDataChannels(t) | ||
|
||
done := make(chan bool, 2) | ||
clientStr := newStream(client.dc, client.rwc, func() { done <- true }) | ||
clientStr.Close() | ||
|
||
select { | ||
case <-done: | ||
t.Fatalf("Close shouldn't run cleanup immediately") | ||
case <-time.After(500 * time.Millisecond): | ||
t.Fatalf("Close should run cleanup immediately") | ||
} | ||
|
||
// The server data channel should still be open | ||
checkDataChannelOpen(t, server.rwc.(*datachannel.DataChannel)) | ||
clientStr.Reset() | ||
// Reset closes the datachannels | ||
checkDataChannelClosed(t, server.rwc.(*datachannel.DataChannel)) | ||
checkDataChannelClosed(t, client.rwc.(*datachannel.DataChannel)) | ||
select { | ||
case <-done: | ||
case <-time.After(2 * time.Second): | ||
t.Fatalf("Reset should run callback immediately") | ||
t.Fatalf("onDone should not be called twice") | ||
case <-time.After(50 * time.Millisecond): | ||
} | ||
} | ||
|
||
|
@@ -478,30 +525,18 @@ func TestStreamDataChannelCloseOnFINACK(t *testing.T) { | |
clientStr.Close() | ||
|
||
select { | ||
case <-done: | ||
t.Fatalf("Close shouldn't run cleanup immediately") | ||
case <-time.After(500 * time.Millisecond): | ||
t.Fatalf("Close should run cleanup immediately") | ||
case <-done: | ||
} | ||
|
||
// sending FIN_ACK closes the datachannel | ||
serverWriter := pbio.NewDelimitedWriter(server.rwc) | ||
err := serverWriter.WriteMsg(&pb.Message{Flag: pb.Message_FIN_ACK.Enum()}) | ||
require.NoError(t, err) | ||
select { | ||
case <-done: | ||
case <-time.After(2 * time.Second): | ||
t.Fatalf("Callback should be run on reading FIN_ACK") | ||
} | ||
b := make([]byte, 100) | ||
N := 0 | ||
for { | ||
n, err := server.rwc.Read(b) | ||
N += n | ||
if err != nil { | ||
require.ErrorIs(t, err, io.EOF) | ||
break | ||
} | ||
} | ||
require.Less(t, N, 10) | ||
|
||
checkDataChannelClosed(t, server.rwc.(*datachannel.DataChannel)) | ||
checkDataChannelClosed(t, client.rwc.(*datachannel.DataChannel)) | ||
} | ||
|
||
func TestStreamChunking(t *testing.T) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment is wrong, can you please update it to describe
checkDataChannelOpen
?