From 10c980adffd4a0b174aa9934dfb468a007c5918d Mon Sep 17 00:00:00 2001 From: Jimmy Zelinskie Date: Wed, 7 May 2014 08:42:41 -0400 Subject: [PATCH] mock.RecordAnnounce() & mock.DeltaHistory() --- storage/backend/mock/driver.go | 50 ++++++++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 9 deletions(-) diff --git a/storage/backend/mock/driver.go b/storage/backend/mock/driver.go index f3b1deb..7e98625 100644 --- a/storage/backend/mock/driver.go +++ b/storage/backend/mock/driver.go @@ -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 }