Skip to content

Commit

Permalink
Add gossip agent tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jose Luis Lucas committed Nov 27, 2018
1 parent db25d9c commit 673ca18
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 0 deletions.
6 changes: 6 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ require (
github.com/coocood/freecache v1.0.1
github.com/coreos/bbolt v1.3.0
github.com/dgraph-io/badger v1.5.4
github.com/fatih/color v1.7.0
github.com/go-redis/redis v6.14.2+incompatible
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c
github.com/hashicorp/go-msgpack v0.0.0-20150518234257-fa3f63826f7c
Expand All @@ -19,14 +21,18 @@ require (
github.com/kr/pty v1.1.3 // indirect
github.com/lucasb-eyer/go-colorful v0.0.0-20180709185858-c7842319cf3a // indirect
github.com/mailru/easyjson v0.0.0-20180823135443-60711f1a8329 // indirect
github.com/mattn/go-colorable v0.0.9 // indirect
github.com/mattn/go-isatty v0.0.4 // indirect
github.com/miekg/dns v1.0.15 // indirect
github.com/pborman/uuid v1.2.0
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529 // indirect
github.com/spf13/cobra v0.0.3
github.com/spf13/pflag v1.0.3 // indirect
github.com/streadway/quantile v0.0.0-20150917103942-b0c588724d25 // indirect
github.com/stretchr/testify v1.2.2
github.com/tsenart/vegeta v8.0.1-0.20181009223527-f25ac6abb70a+incompatible
github.com/valyala/fasthttp v1.0.0
github.com/vmihailenco/msgpack v4.0.1+incompatible
github.com/wcharczuk/go-chart v2.0.1+incompatible
golang.org/x/crypto v0.0.0-20180910181607-0e37d006457b
golang.org/x/image v0.0.0-20181102021609-63626fb251ce // indirect
Expand Down
14 changes: 14 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,18 @@ github.com/dgraph-io/badger v1.5.4 h1:gVTrpUTbbr/T24uvoCaqY2KSHfNLVGm0w+hbee2HMe
github.com/dgraph-io/badger v1.5.4/go.mod h1:VZxzAIRPHRVNRKRo6AXrX9BJegn6il06VMTZVJYCIjQ=
github.com/dgryski/go-farm v0.0.0-20180109070241-2de33835d102 h1:afESQBXJEnj3fu+34X//E8Wg3nEbMJxJkwSc0tPePK0=
github.com/dgryski/go-farm v0.0.0-20180109070241-2de33835d102/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw=
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/go-redis/redis v6.14.2+incompatible h1:UE9pLhzmWf+xHNmZsoccjXosPicuiNaInPgym8nzfg0=
github.com/go-redis/redis v6.14.2+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c h1:964Od4U6p2jUkFxvCydnIczKteheJEzHRToSGK3Bnlw=
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/uuid v1.0.0 h1:b4Gk+7WdP/d3HZH8EJsZpvV7EtDOgaZLtnaNGIu1adA=
github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-immutable-radix v1.0.0 h1:AKDB1HM5PWEA7i4nhcpwOrO2byshxBjXVn/J/3+z5/0=
Expand Down Expand Up @@ -73,10 +79,16 @@ github.com/lucasb-eyer/go-colorful v0.0.0-20180709185858-c7842319cf3a h1:B2QfFRl
github.com/lucasb-eyer/go-colorful v0.0.0-20180709185858-c7842319cf3a/go.mod h1:NXg0ArsFk0Y01623LgUqoqcouGDB+PwCCQlrwrG6xJ4=
github.com/mailru/easyjson v0.0.0-20180823135443-60711f1a8329 h1:2gxZ0XQIU/5z3Z3bUBu+FXuk2pFbkN6tcwi/pjyaDic=
github.com/mailru/easyjson v0.0.0-20180823135443-60711f1a8329/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRUIY4=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-isatty v0.0.4 h1:bnP0vzxcAdeI1zdubAl5PjU6zsERjGZb7raWodagDYs=
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/miekg/dns v1.0.15 h1:9+UupePBQCG6zf1q/bGmTO1vumoG13jsrbWOSX1W6Tw=
github.com/miekg/dns v1.0.15/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c h1:Lgl0gzECD8GnQ5QCWA8o6BtfL6mDH5rQgM4/fX3avOs=
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pborman/uuid v1.2.0 h1:J7Q5mO4ysT1dv8hyrUGHb9+ooztCXu1D8MY8DZYsu3g=
github.com/pborman/uuid v1.2.0/go.mod h1:X/NO0urCmaxf9VXbdlT7C2Yzkj2IKimNn4k+gtPdI/k=
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand All @@ -103,6 +115,8 @@ github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyC
github.com/valyala/fasthttp v1.0.0 h1:BwIoZQbBsTo3v2F5lz5Oy3TlTq4wLKTLV260EVTEWco=
github.com/valyala/fasthttp v1.0.0/go.mod h1:4vX61m6KN+xDduDNwXrhIAVZaZaZiQ1luJk8LWSxF3s=
github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a/go.mod h1:v3UYOV9WzVtRmSR+PDvWpU/qWl4Wa5LApYYX4ZtKbio=
github.com/vmihailenco/msgpack v4.0.1+incompatible h1:RMF1enSPeKTlXrXdOcqjFUElywVZjjC6pqse21bKbEU=
github.com/vmihailenco/msgpack v4.0.1+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
github.com/wcharczuk/go-chart v2.0.1+incompatible h1:0pz39ZAycJFF7ju/1mepnk26RLVLBCWz1STcD3doU0A=
github.com/wcharczuk/go-chart v2.0.1+incompatible/go.mod h1:PF5tmL4EIx/7Wf+hEkpCqYi5He4u90sw+0+6FhrryuE=
golang.org/x/crypto v0.0.0-20180910181607-0e37d006457b h1:2b9XGzhjiYsYPnKXoEfL7klWZQIt8IfyRCz62gCqqlQ=
Expand Down
149 changes: 149 additions & 0 deletions gossip/agent_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
package gossip

import (
"fmt"
"testing"

"github.com/stretchr/testify/require"

"github.com/bbva/qed/gossip/member"
)

func TestStart(t *testing.T) {

}

func TestJoin(t *testing.T) {
conf := DefaultConfig()
conf.NodeName = "testNode"
conf.Role = member.Auditor
a, _ := NewAgent(conf, []Processor{FakeProcessor{}})

testCases := []struct {
agentState member.Status
addrs []string
expectedContactedHosts int
expectedErr error
}{
{
member.Alive,
[]string{},
0,
nil,
},
{
member.Failed,
[]string{},
0,
fmt.Errorf("Agent can't join after Leave or Shutdown"),
},
{
member.Alive,
[]string{"127.0.0.1:12345"},
1,
nil,
},
}

for i, c := range testCases {
a.Self.Status = c.agentState
result, err := a.Join(c.addrs)
require.Equal(t, c.expectedContactedHosts, result, "Wrong expected contacted hosts in test %d.", i)
require.Equal(t, c.expectedErr, err, "Wrong expected error in test %d.", i)
}
}

func TestLeave(t *testing.T) {
conf := DefaultConfig()
conf.NodeName = "testNode"
conf.Role = member.Auditor
conf.BindAddr = "0.0.0.0:12346"
a, _ := NewAgent(conf, []Processor{FakeProcessor{}})

testCases := []struct {
agentState member.Status
expectedErr error
finalStatus member.Status
}{
{
member.Left,
nil,
member.Left,
},
{
member.Leaving,
fmt.Errorf("Leave already in progress"),
member.Leaving,
},
{
member.Shutdown,
fmt.Errorf("Leave called after Shutdown"),
member.Shutdown,
},
{
member.Alive,
nil,
member.Left,
},
{
member.Failed,
nil,
member.Left,
},
}

for i, c := range testCases {
a.Self.Status = c.agentState
err := a.Leave()
require.Equal(t, c.expectedErr, err, "Wrong expected error in test %d.", i)
require.Equal(t, c.finalStatus, a.Self.Status, "Wrong expected status in test %d.", i)
}
}

func TestShutdown(t *testing.T) {

conf := DefaultConfig()
conf.NodeName = "testNode"
conf.Role = member.Auditor
conf.BindAddr = "0.0.0.0:12347"
a, _ := NewAgent(conf, []Processor{FakeProcessor{}})

testCases := []struct {
agentState member.Status
expectedErr error
finalStatus member.Status
}{
{
member.Shutdown,
nil,
member.Shutdown,
},
{
member.Left,
nil,
member.Shutdown,
},
{
member.Alive,
nil,
member.Shutdown,
},
{
member.Failed,
nil,
member.Shutdown,
},
{
member.Leaving,
nil,
member.Shutdown,
},
}

for i, c := range testCases {
a.Self.Status = c.agentState
err := a.Shutdown()
require.Equal(t, c.expectedErr, err, "Wrong expected error in test %d.", i)
require.Equal(t, c.finalStatus, a.Self.Status, "Wrong expected status in test %d.", i)
}
}
6 changes: 6 additions & 0 deletions gossip/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ type Processor interface {
Process(*protocol.BatchSnapshots)
}

type FakeProcessor struct {
}

func (d FakeProcessor) Process(b *protocol.BatchSnapshots) {
}

type DummyProcessor struct {
}

Expand Down

0 comments on commit 673ca18

Please sign in to comment.