Skip to content

Commit

Permalink
added some basic swarm tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kelindar committed Nov 16, 2017
1 parent 204fa11 commit 1037253
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
28 changes: 28 additions & 0 deletions broker/cluster/swarm_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package cluster

import (
"io"
"testing"

"github.com/emitter-io/emitter/broker/message"
"github.com/emitter-io/emitter/config"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -31,3 +33,29 @@ func TestOnGossipUnicast(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, 2, count)
}

func TestNewSwarm(t *testing.T) {
cfg := config.ClusterConfig{
NodeName: "00:00:00:00:00:01",
ListenAddr: ":4000",
AdvertiseAddr: ":4001",
}

// Create a new swarm and check if it was constructed well
s := NewSwarm(&cfg, make(chan bool))
assert.Equal(t, 0, s.NumPeers())
assert.Equal(t, uint64(1), s.ID())
assert.NotNil(t, s.Gossip())

// Gossip with empty payload should not fail
_, err := s.OnGossip([]byte{})
assert.NoError(t, err)

// Gossip with invalid data
_, err = s.OnGossip([]byte{1, 2, 3})
assert.Equal(t, io.EOF, err)

// Close the swarm
err = s.Close()
assert.NoError(t, err)
}
16 changes: 4 additions & 12 deletions broker/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"time"

"github.com/emitter-io/emitter/logging"
"github.com/emitter-io/emitter/network/address"
"github.com/kelindar/process"
)
Expand Down Expand Up @@ -43,16 +42,9 @@ func (s *Service) getStatus() (*StatusInfo, error) {

// Reports the status periodically.
func (s *Service) reportStatus() {
status, err := s.getStatus()
if err != nil {
return
if status, err := s.getStatus(); err == nil {
if b, err := json.Marshal(status); err == nil {
s.selfPublish("cluster/"+status.Addr+"/", b)
}
}

b, err := json.Marshal(status)
if err != nil {
logging.LogError("service", "reporting status", err)
return
}

s.selfPublish("cluster/"+status.Addr+"/", b)
}

0 comments on commit 1037253

Please sign in to comment.