-
Notifications
You must be signed in to change notification settings - Fork 43
/
store_test.go
47 lines (39 loc) · 1021 Bytes
/
store_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package desync
var _ WriteStore = &TestStore{}
type TestStore struct {
Chunks map[ChunkID][]byte
// Override the default behavior by setting these functions
GetChunkFunc func(ChunkID) (*Chunk, error)
HasChunkFunc func(ChunkID) (bool, error)
StoreChunkFunc func(chunk *Chunk) error
}
func (s *TestStore) GetChunk(id ChunkID) (*Chunk, error) {
if s.GetChunkFunc != nil {
return s.GetChunkFunc(id)
}
b, ok := s.Chunks[id]
if !ok {
return nil, ChunkMissing{id}
}
return NewChunk(b), nil
}
func (s *TestStore) HasChunk(id ChunkID) (bool, error) {
if s.HasChunkFunc != nil {
return s.HasChunkFunc(id)
}
_, ok := s.Chunks[id]
return ok, nil
}
func (s *TestStore) StoreChunk(chunk *Chunk) error {
if s.StoreChunkFunc != nil {
return s.StoreChunkFunc(chunk)
}
if s.Chunks == nil {
s.Chunks = make(map[ChunkID][]byte)
}
b, _ := chunk.Data()
s.Chunks[chunk.ID()] = b
return nil
}
func (s *TestStore) String() string { return "TestStore" }
func (s *TestStore) Close() error { return nil }