// Construct a new RaftLock. // // It is expected that raft.ConsensusModule.Start() will be called later using the // returned RaftLock as the raft.ChangeListener parameter. // func NewRaftLock( raftICMAC ICM_AppendCommand, raftLog raft.LogReadOnly, initialLocks []string, initialCommitIndex raft.LogIndex, ) *RaftLock { // TODO: copy initial state rl := &RaftLock{ raftICMAC, raftLog, statemachine.NewInMemoryLSM(), statemachine.NewInMemoryLSM(), initialCommitIndex, initialCommitIndex, nil, // commitApplier make(map[raft.LogIndex]chan struct{}), } applyLocks(initialLocks, rl.committedLocks) applyLocks(initialLocks, rl.uncommittedLocks) // Start commitApplier goroutine rl.commitApplier = runner.NewTriggeredRunner(rl.applyPendingCommits) return rl }
func TestInMemoryLSM(t *testing.T) { var lsm statemachine.LockStateMachine = statemachine.NewInMemoryLSM() fExpectResult := func(f func(name string) bool) f_name_expect_bool { return func(name string, expected bool) { actual := f(name) if actual != expected { t.Fatal(actual) } } } lsm_IsLocked := fExpectResult(lsm.IsLocked) lsm_Lock := fExpectResult(lsm.Lock) lsm_Unlock := fExpectResult(lsm.Unlock) lsm_IsLocked("foo", false) lsm_IsLocked("bar", false) lsm_Lock("foo", true) lsm_IsLocked("foo", true) lsm_IsLocked("bar", false) lsm_Lock("foo", false) lsm_Lock("bar", true) lsm_IsLocked("bar", true) lsm_Unlock("bar", true) lsm_Unlock("foo", true) lsm_Unlock("bar", false) }