forked from johnnadratowski/golang-neo4j-bolt-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
recorder.go
291 lines (240 loc) · 6.02 KB
/
recorder.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package bolt
import (
"bytes"
"encoding/json"
"fmt"
"net"
"os"
"time"
"github.com/axiomzen/golang-neo4j-bolt-driver/encoding"
"github.com/axiomzen/golang-neo4j-bolt-driver/errors"
"github.com/axiomzen/golang-neo4j-bolt-driver/log"
)
// recorder records a given session with Neo4j.
// allows for playback of sessions as well
type recorder struct {
net.Conn
name string
events []*Event
connStr string
currentEvent int
}
func newRecorder(name string, connStr string) *recorder {
r := &recorder{
name: name,
connStr: connStr,
}
if r.connStr == "" {
if err := r.load(r.name); err != nil {
log.Fatalf("Couldn't load data from recording files!: %s", err)
}
}
return r
}
func (r *recorder) completedLast() bool {
event := r.lastEvent()
if event == nil {
return true
}
return event.Completed
}
func (r *recorder) lastEvent() *Event {
if len(r.events) > 0 {
return r.events[len(r.events)-1]
}
return nil
}
// Read from the net conn, recording the interaction
func (r *recorder) Read(b []byte) (n int, err error) {
if r.Conn != nil {
numRead, err := r.Conn.Read(b)
if numRead > 0 {
r.record(b[:numRead], false)
}
if err != nil {
r.recordErr(err, false)
}
return numRead, err
}
if r.currentEvent >= len(r.events) {
return 0, errors.New("Trying to read past all of the events in the recorder! %#v", r)
}
event := r.events[r.currentEvent]
if event.IsWrite {
return 0, errors.New("Recorder expected Read, got Write! %#v, Event: %#v", r, event)
}
for i := 0; i < len(b); i++ {
if len(event.Event) == 0 {
return i, errors.New("Attempted to read past current event in recorder! Bytes: %s. Recorder %#v, Event; %#v", b, r, event)
}
b[i] = event.Event[0]
event.Event = event.Event[1:]
}
if len(event.Event) == 0 {
r.currentEvent++
}
return len(b), nil
}
// Close the net conn, outputting the recording
func (r *recorder) Close() error {
if r.Conn != nil {
err := r.flush()
if err != nil {
return err
}
return r.Conn.Close()
} else if len(r.events) > 0 {
if r.currentEvent != len(r.events) {
return errors.New("Didn't read all of the events in the recorder on close! %#v", r)
}
if len(r.events[len(r.events)-1].Event) != 0 {
return errors.New("Left data in an event in the recorder on close! %#v", r)
}
return nil
}
return nil
}
// Write to the net conn, recording the interaction
func (r *recorder) Write(b []byte) (n int, err error) {
if r.Conn != nil {
numWritten, err := r.Conn.Write(b)
if numWritten > 0 {
r.record(b[:numWritten], true)
}
if err != nil {
r.recordErr(err, true)
}
return numWritten, err
}
if r.currentEvent >= len(r.events) {
return 0, errors.New("Trying to write past all of the events in the recorder! %#v", r)
}
event := r.events[r.currentEvent]
if !event.IsWrite {
return 0, errors.New("Recorder expected Write, got Read! %#v, Event: %#v", r, event)
}
for i := 0; i < len(b); i++ {
if len(event.Event) == 0 {
return i, errors.New("Attempted to write past current event in recorder! %#v, Event: %#v", r, event)
}
event.Event = event.Event[1:]
}
if len(event.Event) == 0 {
r.currentEvent++
}
return len(b), nil
}
func (r *recorder) record(data []byte, isWrite bool) {
event := r.lastEvent()
if event == nil || event.Completed || event.IsWrite != isWrite {
event = newEvent(isWrite)
r.events = append(r.events, event)
}
event.Event = append(event.Event, data...)
if data[len(data)-2] == byte(0x00) && data[len(data)-1] == byte(0x00) {
event.Completed = true
}
}
func (r *recorder) recordErr(err error, isWrite bool) {
event := r.lastEvent()
if event == nil || event.Completed || event.IsWrite != isWrite {
event = newEvent(isWrite)
r.events = append(r.events, event)
}
event.Error = err
event.Completed = true
}
func (r *recorder) load(name string) error {
file, err := os.OpenFile("./recordings/"+name+".json", os.O_RDONLY, 0660)
if err != nil {
return err
}
return json.NewDecoder(file).Decode(&r.events)
}
func (r *recorder) writeRecording() error {
file, err := os.OpenFile("./recordings/"+r.name+".json", os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0660)
if err != nil {
return err
}
return json.NewEncoder(file).Encode(r.events)
}
func (r *recorder) flush() error {
if os.Getenv("RECORD_OUTPUT") == "" {
return nil
}
return r.writeRecording()
}
func (r *recorder) print() {
fmt.Println("PRINTING RECORDING " + r.name)
for _, event := range r.events {
fmt.Println()
fmt.Println()
typee := "READ"
if event.IsWrite {
typee = "WRITE"
}
fmt.Printf("%s @ %d:\n\n", typee, event.Timestamp)
decoded, err := encoding.NewDecoder(bytes.NewBuffer(event.Event)).Decode()
if err != nil {
fmt.Printf("Error decoding data! Error: %s\n", err)
} else {
fmt.Printf("Decoded Data:\n\n%+v\n\n", decoded)
}
fmt.Print("Encoded Bytes:\n\n")
fmt.Print(sprintByteHex(event.Event))
if !event.Completed {
fmt.Println("EVENT NEVER COMPLETED!!!!!!!!!!!!!!!")
}
if event.Error != nil {
fmt.Printf("ERROR OCCURRED DURING EVENT!!!!!!!\n\nError: %s\n", event.Error)
}
fmt.Println()
fmt.Println()
}
fmt.Println("RECORDING END " + r.name)
}
func (r *recorder) LocalAddr() net.Addr {
if r.Conn != nil {
return r.Conn.LocalAddr()
}
return nil
}
func (r *recorder) RemoteAddr() net.Addr {
if r.Conn != nil {
return r.Conn.RemoteAddr()
}
return nil
}
func (r *recorder) SetDeadline(t time.Time) error {
if r.Conn != nil {
return r.Conn.SetDeadline(t)
}
return nil
}
func (r *recorder) SetReadDeadline(t time.Time) error {
if r.Conn != nil {
return r.Conn.SetReadDeadline(t)
}
return nil
}
func (r *recorder) SetWriteDeadline(t time.Time) error {
if r.Conn != nil {
return r.Conn.SetWriteDeadline(t)
}
return nil
}
// Event represents a single recording (read or write) event in the recorder
type Event struct {
Timestamp int64 `json:"-"`
Event []byte
IsWrite bool
Completed bool
Error error
}
func newEvent(isWrite bool) *Event {
return &Event{
Timestamp: time.Now().UnixNano(),
Event: []byte{},
IsWrite: isWrite,
}
}