watchtower/wtdb/mock: adds MockDB for debug build

This commit is contained in:
Conner Fromknecht 2018-10-23 18:28:36 -07:00
parent 8b47a96459
commit fd6024c306
No known key found for this signature in database
GPG Key ID: E7D737B67FA592C7

57
watchtower/wtdb/mock.go Normal file
View File

@ -0,0 +1,57 @@
// +build dev
package wtdb
import "sync"
type MockDB struct {
mu sync.Mutex
sessions map[SessionID]*SessionInfo
}
func NewMockDB() *MockDB {
return &MockDB{
sessions: make(map[SessionID]*SessionInfo),
}
}
func (db *MockDB) InsertStateUpdate(update *SessionStateUpdate) (uint16, error) {
db.mu.Lock()
defer db.mu.Unlock()
info, ok := db.sessions[update.ID]
if !ok {
return 0, ErrSessionNotFound
}
err := info.AcceptUpdateSequence(update.SeqNum, update.LastApplied)
if err != nil {
return info.LastApplied, err
}
return info.LastApplied, nil
}
func (db *MockDB) GetSessionInfo(id *SessionID) (*SessionInfo, error) {
db.mu.Lock()
defer db.mu.Unlock()
if info, ok := db.sessions[*id]; ok {
return info, nil
}
return nil, ErrSessionNotFound
}
func (db *MockDB) InsertSessionInfo(info *SessionInfo) error {
db.mu.Lock()
defer db.mu.Unlock()
if _, ok := db.sessions[info.ID]; ok {
return ErrSessionAlreadyExists
}
db.sessions[info.ID] = info
return nil
}