Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for hashicorp/raft Peer #112

Merged
merged 1 commit into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cluster/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ func TestCluster(t *testing.T) {
BindPort: bindPort1,
RaftPort: raftPort1,
RaftBootstrap: true,
RaftDir: t.TempDir(),
GrpcEnable: false,
Members: members,
DiscoveryWay: config.DiscoveryWaySerf,
NodesFileDir: t.TempDir(),
}
agent1 := NewAgent(conf1)
err = agent1.Start()
Expand All @@ -66,9 +68,11 @@ func TestCluster(t *testing.T) {
BindPort: bindPort2,
RaftPort: raftPort2,
RaftBootstrap: false,
RaftDir: t.TempDir(),
GrpcEnable: false,
Members: members,
DiscoveryWay: config.DiscoveryWaySerf,
NodesFileDir: t.TempDir(),
}
agent2 := NewAgent(conf2)
err = agent2.Start()
Expand All @@ -82,9 +86,11 @@ func TestCluster(t *testing.T) {
BindPort: bindPort3,
RaftPort: raftPort3,
RaftBootstrap: false,
RaftDir: t.TempDir(),
GrpcEnable: false,
Members: members,
DiscoveryWay: config.DiscoveryWaySerf,
NodesFileDir: t.TempDir(),
}
agent3 := NewAgent(conf3)
err = agent3.Start()
Expand Down
2 changes: 1 addition & 1 deletion cluster/raft/hashicorp/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func (p *Peer) Stop() {
}

// snapshot
if err := p.snapshot().Error(); err != "" {
if err := p.snapshot(); err != nil {
log.Warn("failed to create snapshot!")
}

Expand Down
133 changes: 133 additions & 0 deletions cluster/raft/hashicorp/peer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package hashicorp

import (
"os"
"path/filepath"
"testing"
"time"

"github.com/hashicorp/raft"
"github.com/stretchr/testify/require"
"github.com/wind-c/comqtt/v2/cluster/message"
"github.com/wind-c/comqtt/v2/config"
"github.com/wind-c/comqtt/v2/mqtt/packets"
)

func createTestPeer(t *testing.T) *Peer {
conf := &config.Cluster{
NodeName: "node1",
BindAddr: "127.0.0.1",
RaftImpl: config.RaftImplHashicorp,
RaftPort: 8946,
RaftDir: t.TempDir(),
RaftBootstrap: true,
}
notifyCh := make(chan *message.Message, 1)
peer, err := Setup(conf, notifyCh)
require.Nil(t, err)
return peer
}

func TestJoinAndLeave(t *testing.T) {
peer := createTestPeer(t)
defer peer.Stop()

nodeID := "node2"
nodeAddr := "127.0.0.1:8947"

// Test Join
err := peer.Join(nodeID, nodeAddr)
require.NoError(t, err)

configFuture := peer.raft.GetConfiguration()
require.NoError(t, configFuture.Error())

var found bool
for _, server := range configFuture.Configuration().Servers {
if server.ID == raft.ServerID(nodeID) && server.Address == raft.ServerAddress(nodeAddr) {
found = true
break
}
}
require.True(t, found)

// Test Leave
err = peer.Leave(nodeID)
require.NoError(t, err)

configFuture = peer.raft.GetConfiguration()
require.NoError(t, configFuture.Error())

found = false
for _, server := range configFuture.Configuration().Servers {
if server.ID == raft.ServerID(nodeID) && server.Address == raft.ServerAddress(nodeAddr) {
found = true
break
}
}
require.False(t, found)
}

func TestProposeAndLookup(t *testing.T) {
peer := createTestPeer(t)
defer peer.Stop()

msg := &message.Message{
Type: packets.Subscribe,
NodeID: "node1",
Payload: []byte("filter"),
}

err := peer.Propose(msg)
require.NoError(t, err)

key := "filter"
expectedValue := "node1"

time.Sleep(2 * time.Second)

result := peer.Lookup(key)
require.Equal(t, []string{expectedValue}, result)
}

func TestIsApplyRight(t *testing.T) {
peer := createTestPeer(t)
defer peer.Stop()

require.True(t, peer.IsApplyRight())
}

func TestGetLeader(t *testing.T) {
peer := createTestPeer(t)
defer peer.Stop()

addr, id := peer.GetLeader()
require.Equal(t, "127.0.0.1:8946", addr)
require.Equal(t, "node1", id)
}

func TestGenPeersFile(t *testing.T) {
peer := createTestPeer(t)
defer peer.Stop()

file := filepath.Join(t.TempDir(), "peers.json")

err := peer.GenPeersFile(file)
require.NoError(t, err)

_, err = os.Stat(file)
require.False(t, os.IsNotExist(err))

content, err := os.ReadFile(file)
require.NoError(t, err)

expectedContent := `[
{
"id": "node1",
"address": "127.0.0.1:8946",
"non_voter":false
}
]`

require.JSONEq(t, expectedContent, string(content))
}
Loading