github.com/olivere/camlistore@v0.0.0-20140121221811-1b7ac2da0199/pkg/test/loader.go (about) 1 /* 2 Copyright 2014 The Camlistore Authors 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package test 18 19 import ( 20 "errors" 21 "log" 22 "strings" 23 "sync" 24 25 "camlistore.org/pkg/blobserver" 26 ) 27 28 // NewLoader 29 func NewLoader() *Loader { 30 return &Loader{} 31 } 32 33 type Loader struct { 34 mu sync.Mutex 35 sto map[string]blobserver.Storage 36 } 37 38 var _ blobserver.Loader = (*Loader)(nil) 39 40 func (ld *Loader) FindHandlerByType(handlerType string) (prefix string, handler interface{}, err error) { 41 panic("NOIMPL") 42 } 43 44 func (ld *Loader) MyPrefix() string { 45 return "/lies/" 46 } 47 48 func (ld *Loader) GetHandlerType(prefix string) string { 49 log.Printf("test.Loader: GetHandlerType called but not implemented.") 50 return "" 51 } 52 53 func (ld *Loader) GetHandler(prefix string) (interface{}, error) { 54 log.Printf("test.Loader: GetHandler called but not implemented.") 55 return nil, errors.New("doesn't exist") 56 } 57 58 func (ld *Loader) SetStorage(prefix string, s blobserver.Storage) { 59 ld.mu.Lock() 60 defer ld.mu.Unlock() 61 if ld.sto == nil { 62 ld.sto = make(map[string]blobserver.Storage) 63 } 64 ld.sto[prefix] = s 65 } 66 67 func (ld *Loader) GetStorage(prefix string) (blobserver.Storage, error) { 68 ld.mu.Lock() 69 defer ld.mu.Unlock() 70 if bs, ok := ld.sto[prefix]; ok { 71 return bs, nil 72 } 73 if ld.sto == nil { 74 ld.sto = make(map[string]blobserver.Storage) 75 } 76 sto, err := ld.genStorage(prefix) 77 if err != nil { 78 return nil, err 79 } 80 ld.sto[prefix] = sto 81 return sto, nil 82 } 83 84 func (ld *Loader) genStorage(prefix string) (blobserver.Storage, error) { 85 if strings.HasPrefix(prefix, "/good") { 86 return &Fetcher{}, nil 87 } 88 if strings.HasPrefix(prefix, "/fail") { 89 return &Fetcher{ReceiveErr: errors.New("test.Loader intentional failure for /fail storage handler")}, nil 90 } 91 panic("test.Loader.GetStorage: unrecognized prefix type") 92 }