Skip to content

Commit

Permalink
Extract protocols into a separated package (#26)
Browse files Browse the repository at this point in the history
This commit just continues extracting independent parts of the client code into separated packages. Now it is a turn of the client protocols (messages, call and handshake protocols).
  • Loading branch information
vykulakov committed Dec 29, 2019
1 parent 440c6f7 commit e97bec0
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 22 deletions.
13 changes: 7 additions & 6 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/protocols"
"github.com/myzhan/avroipc/transports"
)

Expand All @@ -19,8 +20,8 @@ type client struct {

transport transports.Transport
framingLayer FramingLayer
callProtocol CallProtocol
handshakeProtocol HandshakeProtocol
callProtocol protocols.CallProtocol
handshakeProtocol protocols.HandshakeProtocol
}

// NewClient creates an avro Client, and connect to addr immediately
Expand All @@ -43,28 +44,28 @@ func NewClient(addr string, timeout, sendTimeout time.Duration, bufferSize, comp
return nil, err
}

proto, err := NewAvroSourceProtocol()
proto, err := protocols.NewAvroSourceProtocol()
if err != nil {
return nil, err
}

return NewClientWithTrans(trans, proto, sendTimeout)
}

func NewClientWithTrans(trans transports.Transport, proto MessageProtocol, sendTimeout time.Duration) (Client, error) {
func NewClientWithTrans(trans transports.Transport, proto protocols.MessageProtocol, sendTimeout time.Duration) (Client, error) {
var err error
c := &client{}
c.sendTimeout = sendTimeout

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

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

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

import (
"bytes"
Expand Down
9 changes: 5 additions & 4 deletions call_protocol_test.go → protocols/call_test.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package avroipc_test
package protocols_test

import (
"errors"
"testing"

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

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

func prepareCallProtocol(t *testing.T) (avroipc.CallProtocol, *mocks.MockProtocol) {
func prepareCallProtocol(t *testing.T) (protocols.CallProtocol, *mocks.MockProtocol) {
m := &mocks.MockProtocol{}

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

return p, m
Expand Down
3 changes: 2 additions & 1 deletion handshake_protocol.go → protocols/handshake.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package avroipc
package protocols

import (
"bytes"
"crypto/md5"
"fmt"

"github.com/linkedin/goavro"
"github.com/sirupsen/logrus"
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package avroipc
package protocols

import (
"github.com/stretchr/testify/require"
"testing"

"github.com/stretchr/testify/require"
)

func Test_getMD5(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion message_protocol.go → protocols/message.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package avroipc
package protocols

import (
"fmt"

"github.com/linkedin/goavro"
)

Expand Down
14 changes: 8 additions & 6 deletions message_protocol_test.go → protocols/message_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package avroipc_test
package protocols_test

import (
"github.com/myzhan/avroipc"
"github.com/stretchr/testify/require"
"testing"

"github.com/stretchr/testify/require"

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

func makeDatum(body string) interface{} {
Expand All @@ -22,7 +24,7 @@ func makeArrayDatum(bodies ...string) interface{} {
}

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

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

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

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

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

t.Run("bad method", func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion schema.go → protocols/schemas.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package avroipc
package protocols

const errorsSchema = `
{
Expand Down

0 comments on commit e97bec0

Please sign in to comment.