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

Ignore error if can't write back multistream protocol id #89

Merged
merged 2 commits into from
Jun 27, 2022
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
9 changes: 5 additions & 4 deletions multistream.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,11 @@ func (msm *MultistreamMuxer) Negotiate(rwc io.ReadWriteCloser) (proto string, ha
}
}()

// Send our protocol ID
if err := delimWriteBuffered(rwc, []byte(ProtocolID)); err != nil {
return "", nil, err
}
// Send the multistream protocol ID
// Ignore the error here. We want the handshake to finish, even if the
// other side has closed this rwc for writing. They may have sent us a
// message and closed. Future writers will get an error anyways.
_ = delimWriteBuffered(rwc, []byte(ProtocolID))

line, err := ReadNextToken(rwc)
if err != nil {
Expand Down
57 changes: 42 additions & 15 deletions multistream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ func (rob *readonlyBuffer) Close() error {
return nil
}

func TestNegotiateFail(t *testing.T) {
func TestNegotiatThenWriteFail(t *testing.T) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@marten-seemann highlighting this change. I'm changing this test so that it passes with the current behavior (negotiate works even if you can't write to it).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change goes makes this behavor more like negotiatelazy (wrt write errors). See the previous change here: https://github.com/multiformats/go-multistream/pull/85/files#diff-e59d8a202c91e1303878fa9a18fea9f6a2e3258b609bf4cc1874a17314cb26f8L679

buf := new(bytes.Buffer)

err := delimWrite(buf, []byte(ProtocolID))
Expand All @@ -683,9 +683,15 @@ func TestNegotiateFail(t *testing.T) {

rob := &readonlyBuffer{bytes.NewReader(buf.Bytes())}
_, _, err = mux.Negotiate(rob)
if err != nil {
t.Fatal("Negotiate should not fail here")
}

_, err = rob.Write([]byte("app data"))
if err == nil {
t.Fatal("Negotiate should fail here")
t.Fatal("Write should fail here")
}

}

type mockStream struct {
Expand Down Expand Up @@ -747,19 +753,40 @@ func TestNegotiatePeerSendsAndCloses(t *testing.T) {
t.Fatal(err)
}

s := &mockStream{
// We mock the closed stream by only expecting a single write. The
// mockstream will error on any more writes (same as writing to a closed
// stream)
expectWrite: [][]byte{delimtedProtocolID},
toRead: [][]byte{buf.Bytes()},
}

mux := NewMultistreamMuxer()
mux.AddHandler("foo", nil)
_, _, err = mux.Negotiate(s)
if err != nil {
t.Fatal("Negotiate should not fail here", err)
type testCase = struct {
name string
s *mockStream
}

testCases := []testCase{
{
name: "Able to echo multistream protocol id, but not app protocol id",
s: &mockStream{
// We mock the closed stream by only expecting a single write. The
// mockstream will error on any more writes (same as writing to a closed
// stream)
expectWrite: [][]byte{delimtedProtocolID},
toRead: [][]byte{buf.Bytes()},
},
},
{
name: "Not able to write anything. Stream closes too fast",
s: &mockStream{
expectWrite: [][]byte{},
toRead: [][]byte{buf.Bytes()},
},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
mux := NewMultistreamMuxer()
mux.AddHandler("foo", nil)
_, _, err = mux.Negotiate(tc.s)
if err != nil {
t.Fatal("Negotiate should not fail here", err)
}
})
}
}

Expand Down