mock.RecordAnnounce() & mock.DeltaHistory()

This commit is contained in:
Jimmy Zelinskie 2014-05-07 08:42:41 -04:00
parent 7a05ab6d64
commit 10c980adff

View file

@ -8,6 +8,8 @@
package mock package mock
import ( import (
"sync"
"github.com/chihaya/chihaya/config" "github.com/chihaya/chihaya/config"
"github.com/chihaya/chihaya/storage" "github.com/chihaya/chihaya/storage"
"github.com/chihaya/chihaya/storage/backend" "github.com/chihaya/chihaya/storage/backend"
@ -15,37 +17,67 @@ import (
type driver struct{} type driver struct{}
type mock struct{} // Mock is a concrete implementation of the backend.Conn interface (plus some
// debugging methods) that stores deltas in memory.
type Mock struct {
deltaHistory []*backend.AnnounceDelta
deltaHistoryM sync.RWMutex
}
func (d *driver) New(conf *config.DataStore) backend.Conn { func (d *driver) New(conf *config.DataStore) backend.Conn {
return &mock{} return &Mock{}
} }
func (m *mock) Start() error { // Start returns nil.
func (m *Mock) Start() error {
return nil return nil
} }
func (m *mock) Close() error { // Close returns nil.
func (m *Mock) Close() error {
return nil return nil
} }
func (m *mock) RecordAnnounce(delta *backend.AnnounceDelta) error { // RecordAnnounce adds a delta to the history.
func (m *Mock) RecordAnnounce(delta *backend.AnnounceDelta) error {
m.deltaHistoryM.Lock()
defer m.deltaHistoryM.Unlock()
m.deltaHistory = append(m.deltaHistory, delta)
return nil return nil
} }
func (m *mock) LoadTorrents(ids []uint64) ([]*storage.Torrent, error) { // DeltaHistory safely copies and returns the history of recorded deltas.
func (m *Mock) DeltaHistory() []backend.AnnounceDelta {
m.deltaHistoryM.Lock()
defer m.deltaHistoryM.Unlock()
cp := make([]backend.AnnounceDelta, len(m.deltaHistory))
for index, delta := range m.deltaHistory {
cp[index] = *delta
}
return cp
}
// LoadTorrents returns (nil, nil).
func (m *Mock) LoadTorrents(ids []uint64) ([]*storage.Torrent, error) {
return nil, nil return nil, nil
} }
func (m *mock) LoadAllTorrents() ([]*storage.Torrent, error) { // LoadAllTorrents returns (nil, nil).
func (m *Mock) LoadAllTorrents() ([]*storage.Torrent, error) {
return nil, nil return nil, nil
} }
func (m *mock) LoadUsers(ids []uint64) ([]*storage.User, error) { // LoadUsers returns (nil, nil).
func (m *Mock) LoadUsers(ids []uint64) ([]*storage.User, error) {
return nil, nil return nil, nil
} }
func (m *mock) LoadAllUsers(ids []uint64) ([]*storage.User, error) { // LoadAllUsers returns (nil, nil).
func (m *Mock) LoadAllUsers(ids []uint64) ([]*storage.User, error) {
return nil, nil return nil, nil
} }