github.com/uber/kraken@v0.1.4/tracker/metainfoclient/testing.go (about) 1 // Copyright (c) 2016-2019 Uber Technologies, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 package metainfoclient 15 16 import ( 17 "errors" 18 "sync" 19 20 "github.com/uber/kraken/core" 21 ) 22 23 // TestClient is a thread-safe, in-memory client for simulating downloads. 24 type TestClient struct { 25 sync.Mutex 26 m map[core.Digest]*core.MetaInfo 27 } 28 29 // NewTestClient returns a new TestClient. 30 func NewTestClient() *TestClient { 31 return &TestClient{m: make(map[core.Digest]*core.MetaInfo)} 32 } 33 34 // Upload "uploads" metainfo that can then be subsequently downloaded. Upload 35 // is not supported in the Client interface and exists soley for testing purposes. 36 func (c *TestClient) Upload(mi *core.MetaInfo) error { 37 c.Lock() 38 defer c.Unlock() 39 if _, ok := c.m[mi.Digest()]; ok { 40 return errors.New("metainfo already exists") 41 } 42 c.m[mi.Digest()] = mi 43 return nil 44 } 45 46 // Download returns the metainfo for digest. Ignores namespace. 47 func (c *TestClient) Download(namespace string, d core.Digest) (*core.MetaInfo, error) { 48 c.Lock() 49 defer c.Unlock() 50 mi, ok := c.m[d] 51 if !ok { 52 return nil, ErrNotFound 53 } 54 return mi, nil 55 }