-
Notifications
You must be signed in to change notification settings - Fork 114
/
datasync_mock.go
237 lines (209 loc) · 5.47 KB
/
datasync_mock.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// Copyright (c) 2018 Cisco and/or its affiliates.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package datasync
import (
"github.com/gogo/protobuf/proto"
"github.com/ligato/cn-infra/datasync"
"strings"
)
// MockDataSync can be used to generate datasync events from provided data.
type MockDataSync struct {
data map[string]*ProtoData
anyError error
}
// ProtoData is used to store proto message with revision.
type ProtoData struct {
val proto.Message
rev int64
}
// MockKeyVal implements KeyVal interface.
type MockKeyVal struct {
key string
val proto.Message
rev int64
}
// MockChangeEvent implements ChangeEvent interface.
type MockChangeEvent struct {
mds *MockDataSync
eventType datasync.Op
MockKeyVal
prevVal proto.Message
}
// MockResyncEvent implements ResyncEvent interface.
type MockResyncEvent struct {
mds *MockDataSync
data map[string]*ProtoData
keyPrefixes []string
}
// MockKeyValIterator implements KeyValIterator interface.
type MockKeyValIterator struct {
mre *MockResyncEvent
keys []string
cursor int
}
//// Data Sync ////
// NewMockDataSync is a constructor for MockDataSync.
func NewMockDataSync() *MockDataSync {
return &MockDataSync{
data: make(map[string]*ProtoData),
}
}
// Put allows to put a new value under the given key and to get the corresponding
// data change event.
func (mds *MockDataSync) Put(key string, value proto.Message) datasync.ChangeEvent {
var prevValue proto.Message
if value == nil {
return mds.Delete(key)
}
if _, modify := mds.data[key]; modify {
prevValue = mds.data[key].val
mds.data[key].val = value
mds.data[key].rev++
} else {
mds.data[key] = &ProtoData{
val: value,
rev: 0,
}
}
return &MockChangeEvent{
mds: mds,
eventType: datasync.Put,
MockKeyVal: MockKeyVal{
key: key,
val: value,
rev: mds.data[key].rev,
},
prevVal: prevValue,
}
}
// Delete allows to remove value under the given key and to get the corresponding
// data change event.
func (mds *MockDataSync) Delete(key string) datasync.ChangeEvent {
if _, found := mds.data[key]; !found {
return nil
}
rev := mds.data[key].rev
val := mds.data[key].val
delete(mds.data, key)
return &MockChangeEvent{
mds: mds,
eventType: datasync.Delete,
MockKeyVal: MockKeyVal{
key: key,
rev: rev,
val: val,
},
}
}
// Resync returns resync event corresponding to a given list of key prefixes
// and the current state of the mocked data store.
func (mds *MockDataSync) Resync(keyPrefix ...string) datasync.ResyncEvent {
mre := &MockResyncEvent{
keyPrefixes: keyPrefix,
data: make(map[string]*ProtoData),
}
// copy datastore
for key, data := range mds.data {
mre.data[key] = &ProtoData{
val: proto.Clone(data.val),
rev: data.rev,
}
}
return mre
}
// AnyError returns non-nil if any data change or resync event was processed
// unsuccessfully.
func (mds *MockDataSync) AnyError() error {
return mds.anyError
}
//// Key-Value ////
// GetValue returns the associated value.
func (mkv *MockKeyVal) GetValue(value proto.Message) error {
tmp, err := proto.Marshal(mkv.val)
if err != nil {
return err
}
return proto.Unmarshal(tmp, value)
}
// GetRevision returns the associated revision.
func (mkv *MockKeyVal) GetRevision() (rev int64) {
return mkv.rev
}
// GetKey returns the associated key.
func (mkv *MockKeyVal) GetKey() string {
return mkv.key
}
//// Change Event ////
// Done stores non-nil error to MockDataSync.
func (mche *MockChangeEvent) Done(err error) {
if err != nil {
mche.mds.anyError = err
}
}
// GetChangeType returns either "Put" or "Delete".
func (mche *MockChangeEvent) GetChangeType() datasync.Op {
return mche.eventType
}
// GetPrevValue returns the previous value.
func (mche *MockChangeEvent) GetPrevValue(prevValue proto.Message) (prevValueExist bool, err error) {
if mche.prevVal == nil {
return false, nil
}
tmp, err := proto.Marshal(mche.prevVal)
if err != nil {
return true, err
}
return true, proto.Unmarshal(tmp, prevValue)
}
//// Resync Event ////
// Done stores non-nil error to MockDataSync.
func (mre *MockResyncEvent) Done(err error) {
if err != nil {
mre.mds.anyError = err
}
}
// GetValues returns map "key-prefix->iterator".
func (mre *MockResyncEvent) GetValues() map[ /*keyPrefix*/ string]datasync.KeyValIterator {
values := make(map[string]datasync.KeyValIterator)
for _, prefix := range mre.keyPrefixes {
var keys []string
for key := range mre.data {
if strings.HasPrefix(key, prefix) {
keys = append(keys, key)
}
}
if len(keys) > 0 {
values[prefix] = &MockKeyValIterator{
mre: mre,
keys: keys,
}
}
}
return values
}
//// Key Value Iterator ////
// GetNext returns the next item in the list.
func (mkvi *MockKeyValIterator) GetNext() (kv datasync.KeyVal, allReceived bool) {
if mkvi.cursor == len(mkvi.keys) {
return nil, true
}
key := mkvi.keys[mkvi.cursor]
kv = &MockKeyVal{
key: key,
val: mkvi.mre.data[key].val,
rev: mkvi.mre.data[key].rev,
}
mkvi.cursor++
return kv, false
}