13 // MockVolumes are test doubles for Volumes, used to test handlers.
14 type MockVolume struct {
15 Store map[string][]byte
16 Timestamps map[string]time.Time
17 // Bad volumes return an error for every operation.
19 // Touchable volumes' Touch() method succeeds for a locator
20 // that has been Put().
22 // Readonly volumes return an error for Put, Delete, and
25 // Every operation (except Status) starts by receiving from
26 // Gate. Send one value to unblock one operation; close the
27 // channel to unblock all. By default, it is a closed channel,
28 // so all operations proceed without blocking.
34 // CreateMockVolume returns a non-Bad, non-Readonly, Touchable mock
36 func CreateMockVolume() *MockVolume {
37 gate := make(chan struct{})
40 Store: make(map[string][]byte),
41 Timestamps: make(map[string]time.Time),
45 called: map[string]int{},
50 // CallCount returns how many times the named method has been called.
51 func (v *MockVolume) CallCount(method string) int {
53 defer v.mutex.Unlock()
54 if c, ok := v.called[method]; !ok {
61 func (v *MockVolume) gotCall(method string) {
63 defer v.mutex.Unlock()
64 if _, ok := v.called[method]; !ok {
71 func (v *MockVolume) Get(loc string) ([]byte, error) {
75 return nil, errors.New("Bad volume")
76 } else if block, ok := v.Store[loc]; ok {
77 buf := bufs.Get(len(block))
81 return nil, os.ErrNotExist
84 func (v *MockVolume) Put(loc string, block []byte) error {
88 return errors.New("Bad volume")
91 return MethodDisabledError
97 func (v *MockVolume) Touch(loc string) error {
101 return MethodDisabledError
104 v.Timestamps[loc] = time.Now()
107 return errors.New("Touch failed")
110 func (v *MockVolume) Mtime(loc string) (time.Time, error) {
116 err = errors.New("Bad volume")
117 } else if t, ok := v.Timestamps[loc]; ok {
125 func (v *MockVolume) IndexTo(prefix string, w io.Writer) error {
128 for loc, block := range v.Store {
129 if !IsValidLocator(loc) || !strings.HasPrefix(loc, prefix) {
132 _, err := fmt.Fprintf(w, "%s+%d %d\n",
133 loc, len(block), 123456789)
141 func (v *MockVolume) Delete(loc string) error {
145 return MethodDisabledError
147 if _, ok := v.Store[loc]; ok {
148 if time.Since(v.Timestamps[loc]) < blob_signature_ttl {
154 return os.ErrNotExist
157 func (v *MockVolume) Status() *VolumeStatus {
159 for _, block := range v.Store {
160 used = used + uint64(len(block))
162 return &VolumeStatus{"/bogo", 123, 1000000 - used, used}
165 func (v *MockVolume) String() string {
166 return "[MockVolume]"
169 func (v *MockVolume) Writable() bool {