Revert abstracting copyloop

This commit is contained in:
Arlo Breault 2019-11-21 19:33:39 -05:00
parent 30b5ef8a9e
commit 7092b2cb2c
5 changed files with 69 additions and 53 deletions

View file

@ -2,8 +2,6 @@ package websocketconn
import (
"io"
"log"
"sync"
"time"
"github.com/gorilla/websocket"
@ -70,20 +68,3 @@ func NewWebSocketConn(ws *websocket.Conn) WebSocketConn {
conn.Ws = ws
return conn
}
// Copy from WebSocket to socket and vice versa.
func CopyLoop(c1 io.ReadWriteCloser, c2 io.ReadWriteCloser) {
var wg sync.WaitGroup
copyer := func(dst io.ReadWriteCloser, src io.ReadWriteCloser) {
defer wg.Done()
if _, err := io.Copy(dst, src); err != nil {
log.Printf("io.Copy inside CopyLoop generated an error: %v", err)
}
dst.Close()
src.Close()
}
wg.Add(2)
go copyer(c1, c2)
go copyer(c2, c1)
wg.Wait()
}

View file

@ -1,30 +0,0 @@
package websocketconn
import (
"net"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestWebsocketConn(t *testing.T) {
Convey("CopyLoop", t, func() {
c1, s1 := net.Pipe()
c2, s2 := net.Pipe()
go CopyLoop(s1, s2)
go func() {
bytes := []byte("Hello!")
c1.Write(bytes)
}()
bytes := make([]byte, 6)
n, err := c2.Read(bytes)
So(n, ShouldEqual, 6)
So(err, ShouldEqual, nil)
So(bytes, ShouldResemble, []byte("Hello!"))
s1.Close()
// Check that copy loop has closed other connection
_, err = s2.Write(bytes)
So(err, ShouldNotBeNil)
})
}