-
Notifications
You must be signed in to change notification settings - Fork 20
/
session.go
183 lines (158 loc) · 6.16 KB
/
session.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
// Copyright 2020 The Cockroach Authors.
//
// 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 copyist
import (
"fmt"
"os"
"github.com/pkg/errors"
)
// session is state used during copyist recording and playback to track progress
// of any currently open session.
type session struct {
// recording stores the calls made to registered drivers used in the current
// sessions so that the calls can be played back later.
recording recording
// index is the current offset into the recording slice. It is used only
// during playback mode.
index int
// recordingSource is the in-memory representation for the copyist recordingSource being read or
// written by this session.
recordingSource *recordingSource
// recordingName is the name of the recording currently being made.
recordingName string
// isInit is set to true once this session has been initialized.
isInit bool
// verificationErr is the first sessionError encountered when replaying
// this session for better error reporting later on.
verificationErr *sessionError
}
// currentSession is a global instance of session that tracks state for the
// current copyist session. It is nil if no session is currently open.
var currentSession *session
// IsOpen is true if a recording or playback session is currently in progress.
// That is, Open or OpenNamed has been called, but Close has not yet been
// called. This is useful when some tests use copyist and some don't, and
// testing utility code wants to automatically determine whether to open a
// connection using the copyist driver or the "real" driver.
func IsOpen() bool {
return currentSession != nil
}
// newSession creates a new recording or playback session. The session will
// read or write a new recording of the given name in the given source.
func newSession(source Source, recordingName string) *session {
return &session{
recording: recording{},
recordingSource: newRecordingSource(source),
recordingName: recordingName,
}
}
// OnDriverOpen is called by the proxy drivers when their Open method is called
// by the golang `sql` package to open a new connection. OnDriverOpen performs
// initialization steps for the session and for the driver.
func (s *session) OnDriverOpen(driver *proxyDriver) {
// If session has already been initialized, then no-op.
if s.isInit {
return
}
s.isInit = true
if IsRecording() {
// Invoke sessionInit callback for the driver, if defined. Only do this
// when recording, to give the callback a chance to set the database in
// a clean, well-known state.
if sessionInit != nil {
sessionInit()
}
} else {
// Need to play back a recording file, so parse it now.
if err := s.recordingSource.Parse(); err != nil && !os.IsNotExist(err) {
panicf("error parsing recording file: %v", err)
}
// Set the list of records to play back for the current session.
s.recording = s.recordingSource.GetRecording(s.recordingName)
if s.recording == nil {
panicf("no recording exists with this name: %v", s.recordingName)
}
}
// Clear any connections left over from previous sessions so that they don't
// cause non-deterministic behavior for this test.
clearPooledConnections()
}
// AddRecord adds a record to the current recording.
func (s *session) AddRecord(rec *record) {
s.recording = append(s.recording, rec)
}
// VerifyRecordWithStringArg returns one of the records in this session's
// recording, failing with a nice error if no such record exists, or if its
// first argument does not match the given string.
func (s *session) VerifyRecordWithStringArg(recordTyp recordType, arg string) (*record, error) {
rec, err := s.VerifyRecord(recordTyp)
if err != nil {
return nil, err
}
if rec.Args[0].(string) != arg {
return nil, s.sessionErr(
"mismatched argument to %s, expected %s, got %s\n\n"+
"Do you need to regenerate the recording with the -record flag?",
recordTyp.String(), rec.Args[0].(string), arg)
}
return rec, nil
}
// VerifyRecord returns one of the records in this session's recording, failing
// with a nice error if no such record exists.
func (s *session) VerifyRecord(recordTyp recordType) (*record, error) {
if s.index >= len(s.recording) {
return nil, s.sessionErr(
"too many calls to %s\n\n"+
"Do you need to regenerate the recording with the -record flag?", recordTyp.String())
}
rec := s.recording[s.index]
if rec.Typ != recordTyp {
return nil, s.sessionErr(
"unexpected call to %s\n\n"+
"Do you need to regenerate the recording with the -record flag?", recordTyp.String())
}
s.index++
return rec, nil
}
// Close ends this session, writing any recording file and clearing state.
func (s *session) Close() {
// Only create a recording file if records exist.
if IsRecording() && len(s.recording) != 0 {
// If no recording file exists, or there is parse error, then ignore the
// error and create a new file. Parse errors can happen when there's a
// Git merge conflict, and it's convenient to just silently regenerate
// the file.
_ = s.recordingSource.Parse()
// Add the recording to the in-memory file and then write the file to
// disk.
s.recordingSource.AddRecording(s.recordingName, s.recording)
s.recordingSource.WriteRecording()
}
// Clear any connections pooled during the recording process so that they
// don't leak or cause non-deterministic behavior for the next test.
clearPooledConnections()
}
func (s *session) sessionErr(format string, args ...interface{}) error {
err := &sessionError{errors.Errorf(format, args...)}
if s.verificationErr == nil {
s.verificationErr = err
}
return err
}
func panicf(format string, args ...interface{}) error {
panic(&sessionError{fmt.Errorf(format, args...)})
}
type sessionError struct {
error
}