Skip to content

Commit

Permalink
Extract the framing layer into a separate package (#27)
Browse files Browse the repository at this point in the history
It is not really necessary because there will be only one layer but it will be convenient to have a layers package among other regular packages.
  • Loading branch information
vykulakov committed Dec 29, 2019
1 parent e97bec0 commit ef6451f
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 27 deletions.
11 changes: 6 additions & 5 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"time"

"github.com/myzhan/avroipc/layers"
"github.com/myzhan/avroipc/protocols"
"github.com/myzhan/avroipc/transports"
)
Expand All @@ -19,7 +20,7 @@ type client struct {
sendTimeout time.Duration

transport transports.Transport
framingLayer FramingLayer
framingLayer layers.FramingLayer
callProtocol protocols.CallProtocol
handshakeProtocol protocols.HandshakeProtocol
}
Expand All @@ -44,7 +45,7 @@ func NewClient(addr string, timeout, sendTimeout time.Duration, bufferSize, comp
return nil, err
}

proto, err := protocols.NewAvroSourceProtocol()
proto, err := protocols.NewAvroSource()
if err != nil {
return nil, err
}
Expand All @@ -58,14 +59,14 @@ func NewClientWithTrans(trans transports.Transport, proto protocols.MessageProto
c.sendTimeout = sendTimeout

c.transport = trans
c.framingLayer = NewFramingLayer(trans)
c.framingLayer = layers.NewFraming(trans)

c.callProtocol, err = protocols.NewCallProtocol(proto)
c.callProtocol, err = protocols.NewCall(proto)
if err != nil {
return nil, err
}

c.handshakeProtocol, err = protocols.NewHandshakeProtocol()
c.handshakeProtocol, err = protocols.NewHandshake()
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions framing.go → layers/framing.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package avroipc
package layers

import (
"bytes"
Expand Down Expand Up @@ -26,7 +26,7 @@ type framingLayer struct {
serial uint32
}

func NewFramingLayer(trans transports.Transport) FramingLayer {
func NewFraming(trans transports.Transport) FramingLayer {
return &framingLayer{
trans: trans,
}
Expand Down
9 changes: 5 additions & 4 deletions framing_test.go → layers/framing_test.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package avroipc_test
package layers_test

import (
"bytes"
"fmt"
"testing"

"github.com/myzhan/avroipc"
"github.com/myzhan/avroipc/mocks"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"

"github.com/myzhan/avroipc/layers"
)

func prepareFramingLayer() (avroipc.FramingLayer, *mocks.MockTransport) {
func prepareFramingLayer() (layers.FramingLayer, *mocks.MockTransport) {
m := &mocks.MockTransport{}
f := avroipc.NewFramingLayer(m)
f := layers.NewFraming(m)

return f, m
}
Expand Down
2 changes: 1 addition & 1 deletion protocols/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type сallProtocol struct {
booleanCodec *goavro.Codec
}

func NewCallProtocol(proto MessageProtocol) (CallProtocol, error) {
func NewCall(proto MessageProtocol) (CallProtocol, error) {
p := &сallProtocol{
proto: proto,
}
Expand Down
2 changes: 1 addition & 1 deletion protocols/call_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
func prepareCallProtocol(t *testing.T) (protocols.CallProtocol, *mocks.MockProtocol) {
m := &mocks.MockProtocol{}

p, err := protocols.NewCallProtocol(m)
p, err := protocols.NewCall(m)
require.NoError(t, err)

return p, m
Expand Down
2 changes: 1 addition & 1 deletion protocols/handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type handshakeProtocol struct {
handshakeResponseCodec *goavro.Codec
}

func NewHandshakeProtocol() (HandshakeProtocol, error) {
func NewHandshake() (HandshakeProtocol, error) {
p := &handshakeProtocol{
serverHash: getMD5(messageProtocol),
clientHash: getMD5(messageProtocol),
Expand Down
18 changes: 9 additions & 9 deletions protocols/handshake_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestHandshakeProtocol_PrepareRequest(t *testing.T) {
0x0, 0x0,
}

p, err := NewHandshakeProtocol()
p, err := NewHandshake()
require.NoError(t, err)

actual, err := p.PrepareRequest()
Expand All @@ -59,7 +59,7 @@ func TestHandshakeProtocol_PrepareRequest(t *testing.T) {
0x0, 0x0,
}

p, err := NewHandshakeProtocol()
p, err := NewHandshake()
require.NoError(t, err)

h := p.(*handshakeProtocol)
Expand All @@ -79,7 +79,7 @@ func TestHandshakeProtocol_ProcessResponse(t *testing.T) {
0x7,
}

p, err := NewHandshakeProtocol()
p, err := NewHandshake()
require.NoError(t, err)

needResend, err := p.ProcessResponse(response)
Expand All @@ -94,7 +94,7 @@ func TestHandshakeProtocol_ProcessResponse(t *testing.T) {
0x0,
}

p, err := NewHandshakeProtocol()
p, err := NewHandshake()
require.NoError(t, err)

needResend, err := p.ProcessResponse(response)
Expand All @@ -115,7 +115,7 @@ func TestHandshakeProtocol_ProcessResponse(t *testing.T) {
0x0,
}

p, err := NewHandshakeProtocol()
p, err := NewHandshake()
require.NoError(t, err)

needResend, err := p.ProcessResponse(response)
Expand All @@ -135,7 +135,7 @@ func TestHandshakeProtocol_ProcessResponse(t *testing.T) {
0x0,
}

p, err := NewHandshakeProtocol()
p, err := NewHandshake()
require.NoError(t, err)

needResend, err := p.ProcessResponse(response)
Expand All @@ -155,7 +155,7 @@ func TestHandshakeProtocol_ProcessResponse(t *testing.T) {
0x0,
}

p, err := NewHandshakeProtocol()
p, err := NewHandshake()
require.NoError(t, err)

h := p.(*handshakeProtocol)
Expand All @@ -179,7 +179,7 @@ func TestHandshakeProtocol_ProcessResponse(t *testing.T) {
0x0,
}

p, err := NewHandshakeProtocol()
p, err := NewHandshake()
require.NoError(t, err)

needResend, err := p.ProcessResponse(response)
Expand All @@ -199,7 +199,7 @@ func TestHandshakeProtocol_ProcessResponse(t *testing.T) {
0x0,
}

p, err := NewHandshakeProtocol()
p, err := NewHandshake()
require.NoError(t, err)

h := p.(*handshakeProtocol)
Expand Down
2 changes: 1 addition & 1 deletion protocols/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type AvroSourceProtocol struct {
messages map[string]message
}

func NewAvroSourceProtocol() (MessageProtocol, error) {
func NewAvroSource() (MessageProtocol, error) {
p := &AvroSourceProtocol{
messages: make(map[string]message),
}
Expand Down
6 changes: 3 additions & 3 deletions protocols/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func makeArrayDatum(bodies ...string) interface{} {
}

func TestAvroSourceProtocol_PrepareMessage(t *testing.T) {
p, err := protocols.NewAvroSourceProtocol()
p, err := protocols.NewAvroSource()
require.NoError(t, err)

t.Run("bad method", func(t *testing.T) {
Expand Down Expand Up @@ -67,7 +67,7 @@ func TestAvroSourceProtocol_PrepareMessage(t *testing.T) {
}

func TestAvroSourceProtocol_ParseMessage(t *testing.T) {
p, err := protocols.NewAvroSourceProtocol()
p, err := protocols.NewAvroSource()
require.NoError(t, err)

t.Run("bad method", func(t *testing.T) {
Expand Down Expand Up @@ -97,7 +97,7 @@ func TestAvroSourceProtocol_ParseMessage(t *testing.T) {
}

func TestAvroSourceProtocol_ParseError(t *testing.T) {
p, err := protocols.NewAvroSourceProtocol()
p, err := protocols.NewAvroSource()
require.NoError(t, err)

t.Run("bad method", func(t *testing.T) {
Expand Down

0 comments on commit ef6451f

Please sign in to comment.