Skip to content
This repository has been archived by the owner on May 26, 2022. It is now read-only.

Commit

Permalink
use a stream wrapper when using a version that doesn't use the new st…
Browse files Browse the repository at this point in the history
…ream interface
  • Loading branch information
marten-seemann committed Sep 3, 2020
1 parent 744231c commit 65b7ce9
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 7 deletions.
12 changes: 9 additions & 3 deletions .github/workflows/interop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,18 @@ jobs:
with:
go-version: ${{ matrix.cfg.go }}
- run: go version
- name: build transport
- name: Build transport
run: |
mkdir builder && cp integrationtests/main.go builder/
cp -r integrationtests builder
git checkout ${{ matrix.cfg.commit }}
rm -rf integrationtests || true
mv builder integrationtests
git reflog --decorate -1
go build -o transport-go${{ matrix.cfg.go }}-${{ matrix.cfg.commit }} builder/main.go
if [[ `git merge-base --is-ancestor HEAD 126c64772ba0aef0b2b6d58ff36e55a93f9253a7; echo $?` == "1" ]]; then
go build -o transport-go${{ matrix.cfg.go }}-${{ matrix.cfg.commit }} integrationtests/main.go
else
go build -tags oldstream -o transport-go${{ matrix.cfg.go }}-${{ matrix.cfg.commit }} integrationtests/main.go
fi
- name: Upload binary
uses: actions/upload-artifact@v2
with:
Expand Down
12 changes: 8 additions & 4 deletions integrationtests/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
"github.com/libp2p/go-libp2p-core/peer"
libp2pquic "github.com/libp2p/go-libp2p-quic-transport"
ma "github.com/multiformats/go-multiaddr"

"github.com/libp2p/go-libp2p-quic-transport/integrationtests/stream"
)

func main() {
Expand Down Expand Up @@ -95,10 +97,11 @@ func runServer(hostKey crypto.PrivKey, peerKey crypto.PubKey, addr ma.Multiaddr)
return fmt.Errorf("remote Peer ID mismatch. Got %s, expected %s", conn.RemotePeer().Pretty(), clientPeerID.Pretty())
}
for {
str, err := conn.AcceptStream()
st, err := conn.AcceptStream()
if err != nil {
return nil
}
str := stream.WrapStream(st)
defer str.Close()
data, err := ioutil.ReadAll(str)
if err != nil {
Expand All @@ -107,7 +110,7 @@ func runServer(hostKey crypto.PrivKey, peerKey crypto.PubKey, addr ma.Multiaddr)
if _, err := str.Write(data); err != nil {
return err
}
if err := str.Close(); err != nil {
if err := str.CloseWrite(); err != nil {
return err
}
}
Expand Down Expand Up @@ -135,16 +138,17 @@ func runClient(hostKey crypto.PrivKey, peerKey crypto.PubKey, addr ma.Multiaddr)
if conn.RemotePeer() != serverPeerID {
return fmt.Errorf("remote Peer ID mismatch. Got %s, expected %s", conn.RemotePeer().Pretty(), serverPeerID.Pretty())
}
str, err := conn.OpenStream()
st, err := conn.OpenStream()
if err != nil {
return err
}
str := stream.WrapStream(st)
data := make([]byte, 1<<15)
rand.Read(data)
if _, err := str.Write(data); err != nil {
return err
}
if err := str.Close(); err != nil {
if err := str.CloseWrite(); err != nil {
return err
}
echoed, err := ioutil.ReadAll(str)
Expand Down
30 changes: 30 additions & 0 deletions integrationtests/stream/stream.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package stream

import (
"io"
"time"

"github.com/libp2p/go-libp2p-core/mux"
)

type Stream interface {
io.Reader
io.Writer
io.Closer

CloseWrite() error
CloseRead() error
Reset() error

SetDeadline(time.Time) error
SetReadDeadline(time.Time) error
SetWriteDeadline(time.Time) error
}

type stream struct {
mux.MuxedStream
}

func WrapStream(str mux.MuxedStream) *stream {
return &stream{MuxedStream: str}
}
29 changes: 29 additions & 0 deletions integrationtests/stream/stream_old_interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// +build oldstream

package stream

import (
"log"

"github.com/lucas-clemente/quic-go"
)

func init() {
log.Println("Using old stream interface wrapper.")
}

const reset quic.ErrorCode = 0

func (s *stream) CloseWrite() error {
return s.MuxedStream.Close()
}

func (s *stream) CloseRead() error {
s.MuxedStream.(quic.Stream).CancelRead(reset)
return nil
}

func (s *stream) Close() error {
s.MuxedStream.(quic.Stream).CancelRead(reset)
return s.MuxedStream.Close()
}

0 comments on commit 65b7ce9

Please sign in to comment.