Skip to content
This repository has been archived by the owner on Sep 6, 2018. It is now read-only.

Commit

Permalink
add unit test for statemachine
Browse files Browse the repository at this point in the history
  • Loading branch information
xiang90 committed Jun 6, 2013
1 parent 4fc43b6 commit 86bff03
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 11 deletions.
15 changes: 7 additions & 8 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"time"
"os"
"sort"
"bufio"
"path"
"io/ioutil"
)

//------------------------------------------------------------------------------
Expand Down Expand Up @@ -862,7 +862,6 @@ func (s *Server) SnapshotRecovery(req *SnapshotRequest) (*SnapshotResponse, erro
s.mutex.Lock()
defer s.mutex.Unlock()

//recovery machine state
s.stateMachine.Recovery(req.State)

//update term and index
Expand Down Expand Up @@ -912,21 +911,21 @@ func (s *Server) LoadSnapshot() error {
// TODO check checksum first
// TODO recovery state machine

var machineState int
var state []byte
var checksum, lastIndex, lastTerm uint64
reader := bufio.NewReader(file)
n , err := fmt.Fscanf(reader, "%08x\n%v\n%v\n%v", &checksum, &machineState,
&lastIndex, &lastTerm)

n , err := fmt.Fscanf(file, "%08x\n%v\n%v", &checksum, &lastIndex, &lastTerm)

if err != nil {
return err
}

if n != 4 {
if n != 3 {
return errors.New("Bad snapshot file")
}

state, err := s.stateMachine.Save()
state, _ = ioutil.ReadAll(file)

if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (ss *Snapshot) Save() error {
return err
}

if _, err = file.Write(ss.state); err != nil {
if _, err = file.Write(ss.state); err != nil {
return err
}

Expand Down
29 changes: 28 additions & 1 deletion snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"sync"
"testing"
"time"
//"fmt"
"bytes"
)

// test take and send snapshot
Expand Down Expand Up @@ -41,9 +41,20 @@ func TestTakeAndSendSnapshot(t *testing.T) {
return resp, err
}

stateMachine := &testStateMachine{}

stateMachine.saveFunc = func() ([]byte,error) {
return []byte{0x8},nil
}

stateMachine.recoveryFunc = func(state []byte) error {
return nil
}

var leader *Server
for _, name := range names {
server := newTestServer(name, transporter)
server.stateMachine = stateMachine
server.SetElectionTimeout(testElectionTimeout)
server.SetHeartbeatTimeout(testHeartbeatTimeout)
if err := server.Start(); err != nil {
Expand Down Expand Up @@ -102,6 +113,7 @@ func TestTakeAndSendSnapshot(t *testing.T) {
// test send snapshot to a new node
// send from heartbeat
newServer := newTestServer("4", transporter)
newServer.stateMachine = stateMachine
if err := newServer.Start(); err != nil {
t.Fatalf("Unable to start server[4]: %v", err)
}
Expand All @@ -128,6 +140,20 @@ func TestTakeAndSendSnapshot(t *testing.T) {

func TestStartFormSnapshot(t *testing.T) {
server := newTestServer("1", &testTransporter{})

stateMachine := &testStateMachine{}
stateMachine.saveFunc = func() ([]byte,error) {
return []byte{0x60,0x61,0x62,0x63,0x64,0x65},nil
}

stateMachine.recoveryFunc = func(state []byte) error {
expect := []byte{0x60,0x61,0x62,0x63,0x64,0x65}
if !(bytes.Equal(state, expect)) {
t.Fatalf("Invalid State [Expcet=%v, Actual=%v]", expect, state)
}
return nil
}
server.stateMachine = stateMachine
oldPath := server.path
server.Start()

Expand Down Expand Up @@ -156,6 +182,7 @@ func TestStartFormSnapshot(t *testing.T) {
server.Stop()

server = newTestServer("1", &testTransporter{})
server.stateMachine = stateMachine
// reset the oldPath
server.path = oldPath

Expand Down
1 change: 0 additions & 1 deletion statemachine.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@ package raft
type StateMachine interface {
Save() ([]byte, error)
Recovery([]byte) error

}
15 changes: 15 additions & 0 deletions test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,21 @@ func (t *testTransporter) SendSnapshotRequest(server *Server, peer *Peer, req *S
return t.sendSnapshotRequestFunc(server, peer, req)
}


type testStateMachine struct {
saveFunc func() ([]byte, error)
recoveryFunc func([]byte) error
}

func (sm *testStateMachine) Save() ([]byte, error) {
return sm.saveFunc()
}

func (sm *testStateMachine) Recovery(state []byte) error {
return sm.recoveryFunc(state)
}


//--------------------------------------
// Join Command
//--------------------------------------
Expand Down

0 comments on commit 86bff03

Please sign in to comment.