-
Notifications
You must be signed in to change notification settings - Fork 83
/
decode.go
163 lines (140 loc) · 3.88 KB
/
decode.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
package tracer
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
const ReadOperation = "read"
const WriteOperation = "write"
const DeleteOperation = "delete"
type TraceOperation struct {
Operation string `json:"operation"`
Key string `json:"key"`
Value string `json:"value"`
Metadata map[string]interface{} `json:"metadata"`
}
type TraceDecoder struct {
bytes.Buffer
}
func (td *TraceDecoder) GetOperations() (operations []TraceOperation) {
// Trim the string of trailing newlines.
opString := strings.TrimSuffix(td.String(), "\n")
if len(opString) == 0 {
return operations
}
// Split the string on newlines and parse the JSON.
operationStrings := strings.Split(opString, "\n")
for _, operationString := range operationStrings {
var operation = TraceOperation{}
err := json.Unmarshal([]byte(operationString), &operation)
if err != nil {
panic(err)
}
operations = append(operations, operation)
}
return operations
}
func (td *TraceDecoder) GetWriteOperations() (operations []TraceOperation) {
for _, operation := range td.GetOperations() {
if operation.Operation == WriteOperation || operation.Operation == DeleteOperation {
operations = append(operations, operation)
}
}
return operations
}
func (td *TraceDecoder) RequireKeyPrefixWrittenInSequence(
t *testing.T,
keys []string,
) {
writeOperations := td.GetWriteOperations()
decodedWriteOperations := make([]string, 0, len(writeOperations))
for _, operation := range writeOperations {
s, err := base64.StdEncoding.DecodeString(operation.Key)
require.NoError(t, err)
decodedWriteOperations = append(decodedWriteOperations, string(s))
}
require.Len(
t,
decodedWriteOperations,
len(keys),
fmt.Sprintf(
"Different number of write operations (%+v) performed than expected (%+v)",
len(decodedWriteOperations),
len(keys),
),
)
for i, dwo := range decodedWriteOperations {
require.True(
t,
strings.HasPrefix(
dwo,
keys[i],
),
fmt.Sprintf(
"Keys were not written in sequence.\nExpected:\n%s\nFound:\n%s\nWrite index: %d",
keys[i],
dwo,
i,
),
)
}
}
func (td *TraceDecoder) RequireReadWriteInSequence(
t *testing.T,
expectedOperations []TraceOperation,
) {
operations := td.GetOperations()
require.Len(
t,
operations,
len(expectedOperations),
"Different number of operations performed than expected",
)
for i, op := range operations {
require.Equal(t, expectedOperations[i].Operation, op.Operation)
key, err := base64.StdEncoding.DecodeString(op.Key)
require.NoError(t, err)
require.Equal(t, expectedOperations[i].Key, string(key))
value, err := base64.StdEncoding.DecodeString(op.Value)
require.NoError(t, err)
require.Equal(t, expectedOperations[i].Value, string(value))
}
}
// RequireKeyPrefixesWritten checks if the list of key prefixes were
// written to. This should be used when the order of writes do not have
// a deterministic ordering, i.e when state is branched and written back to.
// TODO(CLOB-851) update this function to assert ordering of key prefixes within stores.
func (td *TraceDecoder) RequireKeyPrefixesWritten(
t *testing.T,
keys []string,
) {
writeOperations := td.GetWriteOperations()
decodedWriteOperations := make([]string, 0, len(writeOperations))
for _, operation := range writeOperations {
s, err := base64.StdEncoding.DecodeString(operation.Key)
require.NoError(t, err)
decodedWriteOperations = append(decodedWriteOperations, string(s))
}
for _, key := range keys {
foundMatchingWriteOperation := false
for _, dwo := range decodedWriteOperations {
if strings.HasPrefix(
dwo,
key,
) {
foundMatchingWriteOperation = true
break
}
}
require.Truef(
t,
foundMatchingWriteOperation,
"Did not find a matching write operation for key %+v",
key,
)
}
}