Skip to content

Commit

Permalink
Merge pull request #535 from iulianpascalau/fix/race-mock-conn-stream
Browse files Browse the repository at this point in the history
Fixed race conditions in mock package mock_stream and mock_conn
  • Loading branch information
Stebalien authored Feb 14, 2019
2 parents ee3d862 + b8e47c5 commit 74b74b1
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
6 changes: 1 addition & 5 deletions p2p/net/mock/mock_conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,7 @@ func (c *conn) NewStream() (inet.Stream, error) {
}

func (c *conn) GetStreams() []inet.Stream {
var out []inet.Stream
for e := c.streams.Front(); e != nil; e = e.Next() {
out = append(out, e.Value.(*stream))
}
return out
return c.allStreams()
}

// LocalMultiaddr is the Multiaddr on this side
Expand Down
9 changes: 6 additions & 3 deletions p2p/net/mock/mock_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"io"
"net"
"sync/atomic"
"time"

inet "github.com/libp2p/go-libp2p-net"
Expand All @@ -24,7 +25,7 @@ type stream struct {

writeErr error

protocol protocol.ID
protocol atomic.Value
stat inet.Stat
}

Expand Down Expand Up @@ -70,15 +71,17 @@ func (s *stream) Write(p []byte) (n int, err error) {
}

func (s *stream) Protocol() protocol.ID {
return s.protocol
// Ignore type error. It means that the protocol is unset.
p, _ := s.protocol.Load().(protocol.ID)
return p
}

func (s *stream) Stat() inet.Stat {
return s.stat
}

func (s *stream) SetProtocol(proto protocol.ID) {
s.protocol = proto
s.protocol.Store(proto)
}

func (s *stream) Close() error {
Expand Down

0 comments on commit 74b74b1

Please sign in to comment.