Skip to content

Commit

Permalink
Migrate to Go modules
Browse files Browse the repository at this point in the history
Also added some simple tests to freeze the current behaviour of utils methods.
  • Loading branch information
Vyacheslav Kulakov committed Dec 14, 2019
1 parent 5a8ad23 commit d61181e
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# IDEs
.idea
9 changes: 9 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module github.com/myzhan/avroipc

go 1.13

require (
github.com/golang/snappy v0.0.1 // indirect
github.com/linkedin/goavro v2.1.0+incompatible
github.com/stretchr/testify v1.4.0
)
15 changes: 15 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4=
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/linkedin/goavro v2.1.0+incompatible h1:DV2aUlj2xZiuxQyvag8Dy7zjY69ENjS66bWkSfdpddY=
github.com/linkedin/goavro v2.1.0+incompatible/go.mod h1:bBCwI2eGYpUI/4820s67MElg9tdeLbINjLjiM2xZFYM=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
1 change: 1 addition & 0 deletions ipc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
)

func TestSend(t *testing.T) {
t.Skip("Skip the real test")
log.SetFlags(log.LstdFlags | log.Lshortfile)

// flume avro instance address
Expand Down
7 changes: 2 additions & 5 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ package avroipc
import (
"bytes"
"crypto/md5"
"github.com/linkedin/goavro"
"io"
"log"
"net"

"github.com/linkedin/goavro"
)

func recvBytes(conn *net.TCPConn, length int) []byte {
func recvBytes(conn io.Reader, length int) []byte {
buf := make([]byte, length)
for length > 0 {
n, err := conn.Read(buf)
Expand All @@ -36,7 +34,6 @@ func encodeInt(n int) []byte {
}

func messageHeader() []byte {

buf := new(bytes.Buffer)
// meta header isn't supported so far, write an empty meta header, which is 0
buf.WriteByte(0)
Expand Down
78 changes: 78 additions & 0 deletions utils_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package avroipc

import (
"bytes"
"testing"

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

func Test_recvBytes(t *testing.T) {
t.Run("zero value", func(t *testing.T) {
buf := bytes.NewBuffer([]byte("test"))
actual := recvBytes(buf, 2)
expected := []byte{0x74, 0x65}

require.Equal(t, expected, actual)
})

t.Run("normal value", func(t *testing.T) {
buf := bytes.NewBuffer([]byte("test"))
actual := recvBytes(buf, 4)
expected := []byte{0x74, 0x65, 0x73, 0x74}

require.Equal(t, expected, actual)
})
}

func Test_encodeInt(t *testing.T) {
t.Run("zero value", func(t *testing.T) {
actual := encodeInt(0)
expected := []byte{0x0}

require.Equal(t, expected, actual)
})

t.Run("normal value", func(t *testing.T) {
actual := encodeInt(7)
expected := []byte{0xe}

require.Equal(t, expected, actual)
})
}

func Test_messageHeader(t *testing.T) {
actual := messageHeader()
expected := []byte{
// Meta header
0x0,
// Method name length
0xc,
// Method name: append
0x61, 0x70, 0x70, 0x65, 0x6e, 0x64}

require.Equal(t, expected, actual)
}

func Test_getMD5(t *testing.T) {
t.Run("no values", func(t *testing.T) {
actual := getMD5()
expected := []byte{0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x0, 0xb2, 0x4, 0xe9, 0x80, 0x9, 0x98, 0xec, 0xf8, 0x42, 0x7e}

require.Equal(t, expected, actual)
})

t.Run("single value", func(t *testing.T) {
actual := getMD5("test string")
expected := []byte{0x6f, 0x8d, 0xb5, 0x99, 0xde, 0x98, 0x6f, 0xab, 0x7a, 0x21, 0x62, 0x5b, 0x79, 0x16, 0x58, 0x9c}

require.Equal(t, expected, actual)
})

t.Run("multiple values", func(t *testing.T) {
actual := getMD5("string 1", "string 2")
expected := []byte{0x60, 0x29, 0x64, 0x59, 0xb, 0x60, 0x61, 0x92, 0x57, 0x8c, 0xf3, 0x2b, 0xdb, 0x3a, 0xa8, 0x58}

require.Equal(t, expected, actual)
})
}

0 comments on commit d61181e

Please sign in to comment.