ducky/devices

Paddy 2015-11-20 Parent:7bc6a84ac906

9:f5a9d5f8f28d Go to Latest

ducky/devices/memstore_test.go

Separate out StorerFactory into its own interface. We had previously included a Factory and Destroy method in the Storer interface, but it makes more sense to separate those out into a StorerFactory interface. This lets us avoid the impression that we're using the same instance of a Storer for every test, separates the test methods away from the methods that are actually used in prod, and hides them from Godoc. What's not to like?

History
     1.1 --- a/memstore_test.go	Sun Nov 15 04:40:30 2015 -0800
     1.2 +++ b/memstore_test.go	Fri Nov 20 01:14:58 2015 -0800
     1.3 @@ -1,5 +1,27 @@
     1.4  package devices
     1.5  
     1.6 +import (
     1.7 +	"fmt"
     1.8 +
     1.9 +	"golang.org/x/net/context"
    1.10 +)
    1.11 +
    1.12  func init() {
    1.13 -	storers = append(storers, NewMemstore())
    1.14 +	storerFactories = append(storerFactories, MemstoreFactory{})
    1.15  }
    1.16 +
    1.17 +type MemstoreFactory struct {
    1.18 +}
    1.19 +
    1.20 +func (m MemstoreFactory) NewStorer(ctx context.Context) (Storer, error) {
    1.21 +	return NewMemstore(), nil
    1.22 +}
    1.23 +
    1.24 +func (m MemstoreFactory) TeardownStorer(storer Storer, ctx context.Context) error {
    1.25 +	memstorer, ok := storer.(*Memstore)
    1.26 +	if !ok {
    1.27 +		return fmt.Errorf("Storer was not a *Memstore, was a %T", storer)
    1.28 +	}
    1.29 +	memstorer.devices = nil
    1.30 +	return nil
    1.31 +}