Skip to content

Commit

Permalink
fix race
Browse files Browse the repository at this point in the history
  • Loading branch information
benbjohnson committed Apr 15, 2015
1 parent 6e124f3 commit d565076
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion raft/internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"io/ioutil"
"os"
"sync"
"testing"
)

Expand Down Expand Up @@ -44,25 +45,36 @@ func CloseLog(l *Log) {

// IndexFSM represents a state machine that only records the last applied index.
type IndexFSM struct {
mu sync.Mutex
index uint64
}

// MustApply updates the index.
func (fsm *IndexFSM) Apply(entry *LogEntry) error {
fsm.mu.Lock()
fsm.index = entry.Index
fsm.mu.Unlock()
return nil
}

// Index returns the highest applied index.
func (fsm *IndexFSM) Index() uint64 { return fsm.index }
func (fsm *IndexFSM) Index() uint64 {
fsm.mu.Lock()
defer fsm.mu.Unlock()
return fsm.index
}

// WriteTo writes a snapshot of the FSM to w.
func (fsm *IndexFSM) WriteTo(w io.Writer) (n int64, err error) {
fsm.mu.Lock()
defer fsm.mu.Unlock()
return 0, binary.Write(w, binary.BigEndian, fsm.index)
}

// ReadFrom reads an FSM snapshot from r.
func (fsm *IndexFSM) ReadFrom(r io.Reader) (n int64, err error) {
fsm.mu.Lock()
defer fsm.mu.Unlock()
return 0, binary.Read(r, binary.BigEndian, &fsm.index)
}

Expand Down

0 comments on commit d565076

Please sign in to comment.