ducky/devices

Paddy 2015-11-14 Parent:7bc6a84ac906 Child:34130c700842

5:408abf6e48d3 Go to Latest

ducky/devices/memstore.go

Add more interface tests. Add a test to ensure that, when retrieving Devices, no error is returned if a Device cannot be found. Add a test to ensure that, when adding Devices, adding a Device that shares an ID with a Device already in the Storer returns an ErrDeviceAlreadyExists error. This involved creating the ErrDeviceAlreadyExists error, and modifying the in-memory implementation to properly return it. Fix a go vet issue in our previous test, wherein we forgot to pass the storer to a log message, resulting in a mismatch between the number of variables expected and the number of variables provided. Rename our tests to be better reflective of what they actually test.

History
1 package devices
3 import (
4 "sync"
6 "code.secondbit.org/uuid.hg"
7 "golang.org/x/net/context"
8 )
10 type Memstore struct {
11 devices map[string]Device
12 lock sync.RWMutex
13 }
15 func NewMemstore() *Memstore {
16 return &Memstore{
17 devices: map[string]Device{},
18 }
19 }
21 func (m *Memstore) GetDevices(ids []uuid.ID, c context.Context) (map[string]Device, error) {
22 m.lock.RLock()
23 defer m.lock.RUnlock()
25 results := map[string]Device{}
27 for _, id := range ids {
28 device, ok := m.devices[id.String()]
29 if !ok {
30 continue
31 }
32 results[id.String()] = device
33 }
34 return results, nil
35 }
37 func (m *Memstore) UpdateDevice(change DeviceChange, c context.Context) error {
38 return nil
39 }
41 func (m *Memstore) DeleteDevices(id []uuid.ID, c context.Context) error {
42 return nil
43 }
45 func (m *Memstore) CreateDevices(devices []Device, c context.Context) error {
46 m.lock.Lock()
47 defer m.lock.Unlock()
49 for _, device := range devices {
50 if _, ok := m.devices[device.ID.String()]; ok {
51 return ErrDeviceAlreadyExists(device.ID)
52 }
53 }
55 for _, device := range devices {
56 m.devices[device.ID.String()] = device
57 }
58 return nil
59 }
61 func (m *Memstore) ListDevicesByOwner(user uuid.ID, c context.Context) ([]Device, error) {
62 return []Device{}, nil
63 }
65 func (m *Memstore) Factory(c context.Context) (Storer, error) {
66 return NewMemstore(), nil
67 }
69 func (m *Memstore) Destroy(c context.Context) error {
70 m.devices = nil
71 return nil
72 }