1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package mockstore
16
17 import (
18 "time"
19
20 "go.etcd.io/etcd/client/pkg/v3/testutil"
21 "go.etcd.io/etcd/server/v3/etcdserver/api/v2store"
22 )
23
24
25 type StoreRecorder struct {
26 v2store.Store
27 testutil.Recorder
28 }
29
30
31
32
33 type storeRecorder struct {
34 v2store.Store
35 testutil.Recorder
36 }
37
38 func NewNop() v2store.Store { return &storeRecorder{Recorder: &testutil.RecorderBuffered{}} }
39 func NewRecorder() *StoreRecorder {
40 sr := &storeRecorder{Recorder: &testutil.RecorderBuffered{}}
41 return &StoreRecorder{Store: sr, Recorder: sr.Recorder}
42 }
43 func NewRecorderStream() *StoreRecorder {
44 sr := &storeRecorder{Recorder: testutil.NewRecorderStream()}
45 return &StoreRecorder{Store: sr, Recorder: sr.Recorder}
46 }
47
48 func (s *storeRecorder) Version() int { return 0 }
49 func (s *storeRecorder) Index() uint64 { return 0 }
50 func (s *storeRecorder) Get(path string, recursive, sorted bool) (*v2store.Event, error) {
51 s.Record(testutil.Action{
52 Name: "Get",
53 Params: []interface{}{path, recursive, sorted},
54 })
55 return &v2store.Event{}, nil
56 }
57 func (s *storeRecorder) Set(path string, dir bool, val string, expireOpts v2store.TTLOptionSet) (*v2store.Event, error) {
58 s.Record(testutil.Action{
59 Name: "Set",
60 Params: []interface{}{path, dir, val, expireOpts},
61 })
62 return &v2store.Event{}, nil
63 }
64 func (s *storeRecorder) Update(path, val string, expireOpts v2store.TTLOptionSet) (*v2store.Event, error) {
65 s.Record(testutil.Action{
66 Name: "Update",
67 Params: []interface{}{path, val, expireOpts},
68 })
69 return &v2store.Event{}, nil
70 }
71 func (s *storeRecorder) Create(path string, dir bool, val string, uniq bool, expireOpts v2store.TTLOptionSet) (*v2store.Event, error) {
72 s.Record(testutil.Action{
73 Name: "Create",
74 Params: []interface{}{path, dir, val, uniq, expireOpts},
75 })
76 return &v2store.Event{}, nil
77 }
78 func (s *storeRecorder) CompareAndSwap(path, prevVal string, prevIdx uint64, val string, expireOpts v2store.TTLOptionSet) (*v2store.Event, error) {
79 s.Record(testutil.Action{
80 Name: "CompareAndSwap",
81 Params: []interface{}{path, prevVal, prevIdx, val, expireOpts},
82 })
83 return &v2store.Event{}, nil
84 }
85 func (s *storeRecorder) Delete(path string, dir, recursive bool) (*v2store.Event, error) {
86 s.Record(testutil.Action{
87 Name: "Delete",
88 Params: []interface{}{path, dir, recursive},
89 })
90 return &v2store.Event{}, nil
91 }
92 func (s *storeRecorder) CompareAndDelete(path, prevVal string, prevIdx uint64) (*v2store.Event, error) {
93 s.Record(testutil.Action{
94 Name: "CompareAndDelete",
95 Params: []interface{}{path, prevVal, prevIdx},
96 })
97 return &v2store.Event{}, nil
98 }
99 func (s *storeRecorder) Watch(_ string, _, _ bool, _ uint64) (v2store.Watcher, error) {
100 s.Record(testutil.Action{Name: "Watch"})
101 return v2store.NewNopWatcher(), nil
102 }
103 func (s *storeRecorder) Save() ([]byte, error) {
104 s.Record(testutil.Action{Name: "Save"})
105 return nil, nil
106 }
107 func (s *storeRecorder) Recovery(b []byte) error {
108 s.Record(testutil.Action{Name: "Recovery"})
109 return nil
110 }
111
112 func (s *storeRecorder) SaveNoCopy() ([]byte, error) {
113 s.Record(testutil.Action{Name: "SaveNoCopy"})
114 return nil, nil
115 }
116
117 func (s *storeRecorder) Clone() v2store.Store {
118 s.Record(testutil.Action{Name: "Clone"})
119 return s
120 }
121
122 func (s *storeRecorder) JsonStats() []byte { return nil }
123 func (s *storeRecorder) DeleteExpiredKeys(cutoff time.Time) {
124 s.Record(testutil.Action{
125 Name: "DeleteExpiredKeys",
126 Params: []interface{}{cutoff},
127 })
128 }
129
130 func (s *storeRecorder) HasTTLKeys() bool {
131 s.Record(testutil.Action{
132 Name: "HasTTLKeys",
133 })
134 return true
135 }
136
137
138
139 type errStoreRecorder struct {
140 storeRecorder
141 err error
142 }
143
144 func NewErrRecorder(err error) *StoreRecorder {
145 sr := &errStoreRecorder{err: err}
146 sr.Recorder = &testutil.RecorderBuffered{}
147 return &StoreRecorder{Store: sr, Recorder: sr.Recorder}
148 }
149
150 func (s *errStoreRecorder) Get(path string, recursive, sorted bool) (*v2store.Event, error) {
151 s.storeRecorder.Get(path, recursive, sorted)
152 return nil, s.err
153 }
154 func (s *errStoreRecorder) Watch(path string, recursive, sorted bool, index uint64) (v2store.Watcher, error) {
155 s.storeRecorder.Watch(path, recursive, sorted, index)
156 return nil, s.err
157 }
158
View as plain text