mock.RecordAnnounce() & mock.DeltaHistory()
This commit is contained in:
parent
7a05ab6d64
commit
10c980adff
1 changed files with 41 additions and 9 deletions
|
@ -8,6 +8,8 @@
|
|||
package mock
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/chihaya/chihaya/config"
|
||||
"github.com/chihaya/chihaya/storage"
|
||||
"github.com/chihaya/chihaya/storage/backend"
|
||||
|
@ -15,37 +17,67 @@ import (
|
|||
|
||||
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 {
|
||||
return &mock{}
|
||||
return &Mock{}
|
||||
}
|
||||
|
||||
func (m *mock) Start() error {
|
||||
// Start returns nil.
|
||||
func (m *Mock) Start() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *mock) Close() error {
|
||||
// Close returns nil.
|
||||
func (m *Mock) Close() error {
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func (m *mock) LoadAllTorrents() ([]*storage.Torrent, error) {
|
||||
// LoadAllTorrents returns (nil, nil).
|
||||
func (m *Mock) LoadAllTorrents() ([]*storage.Torrent, error) {
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue