@@ -3,13 +3,16 @@ package sqlds
3
3
import (
4
4
"context"
5
5
"database/sql"
6
+ "database/sql/driver"
6
7
"encoding/json"
7
8
"errors"
8
9
"fmt"
10
+ "io"
9
11
"testing"
10
12
"time"
11
13
12
14
"github.com/grafana/grafana-plugin-sdk-go/backend"
15
+ "github.com/grafana/grafana-plugin-sdk-go/data/sqlutil"
13
16
"github.com/grafana/sqlds/v2/mock"
14
17
"github.com/stretchr/testify/assert"
15
18
)
@@ -24,6 +27,14 @@ func (d fakeDriver) Connect(backend.DataSourceInstanceSettings, json.RawMessage)
24
27
return d .db , nil
25
28
}
26
29
30
+ func (d fakeDriver ) Macros () Macros {
31
+ return Macros {}
32
+ }
33
+
34
+ func (d fakeDriver ) Converters () []sqlutil.Converter {
35
+ return []sqlutil.Converter {}
36
+ }
37
+
27
38
// func (d fakeDriver) Settings(backend.DataSourceInstanceSettings) DriverSettings
28
39
29
40
func Test_getDBConnectionFromQuery (t * testing.T ) {
@@ -121,7 +132,7 @@ func Test_Dispose(t *testing.T) {
121
132
})
122
133
}
123
134
124
- func Test_retries (t * testing.T ) {
135
+ func Test_timeout_retries (t * testing.T ) {
125
136
dsUID := "timeout"
126
137
settings := backend.DataSourceInstanceSettings {UID : dsUID }
127
138
@@ -157,14 +168,67 @@ func Test_retries(t *testing.T) {
157
168
assert .Equal (t , expected , result .Message )
158
169
}
159
170
171
+ func Test_error_retries (t * testing.T ) {
172
+ testCounter = 0
173
+ dsUID := "error"
174
+ settings := backend.DataSourceInstanceSettings {UID : dsUID }
175
+
176
+ handler := testSqlHandler {
177
+ error : errors .New ("foo" ),
178
+ }
179
+ mockDriver := "sqlmock-error"
180
+ mock .RegisterDriver (mockDriver , handler )
181
+ db , err := sql .Open (mockDriver , "" )
182
+ if err != nil {
183
+ t .Errorf ("failed to connect to mock driver: %v" , err )
184
+ }
185
+ timeoutDriver := fakeDriver {
186
+ db : db ,
187
+ }
188
+ retries := 5
189
+ max := time .Duration (10 ) * time .Second
190
+ driverSettings := DriverSettings {Retries : retries , Timeout : max , Pause : 1 }
191
+ ds := & SQLDatasource {c : timeoutDriver , driverSettings : driverSettings }
192
+
193
+ key := defaultKey (dsUID )
194
+ // Add the mandatory default db
195
+ ds .storeDBConnection (key , dbConnection {db , settings })
196
+ ctx := context .Background ()
197
+
198
+ qry := `{ "rawSql": "foo" }`
199
+
200
+ req := & backend.QueryDataRequest {
201
+ PluginContext : backend.PluginContext {
202
+ DataSourceInstanceSettings : & settings ,
203
+ },
204
+ Queries : []backend.DataQuery {
205
+ {
206
+ RefID : "foo" ,
207
+ JSON : []byte (qry ),
208
+ },
209
+ },
210
+ }
211
+
212
+ data , err := ds .QueryData (ctx , req )
213
+ assert .Nil (t , err )
214
+ assert .Equal (t , retries + 1 , testCounter )
215
+ assert .NotNil (t , data .Responses )
216
+
217
+ }
218
+
160
219
var testCounter = 0
161
220
var testTimeout = 1
221
+ var testRows = 0
162
222
163
223
type testSqlHandler struct {
164
224
mock.DBHandler
225
+ error
165
226
}
166
227
167
228
func (s testSqlHandler ) Ping (ctx context.Context ) error {
229
+ if s .error != nil {
230
+ return s .error
231
+ }
168
232
testCounter ++ // track the retries for the test assertion
169
233
time .Sleep (time .Duration (testTimeout + 1 )) // simulate a connection delay
170
234
select {
@@ -173,3 +237,30 @@ func (s testSqlHandler) Ping(ctx context.Context) error {
173
237
return ctx .Err ()
174
238
}
175
239
}
240
+
241
+ func (s testSqlHandler ) Query (args []driver.Value ) (driver.Rows , error ) {
242
+ fmt .Println ("query" )
243
+ if s .error != nil {
244
+ testCounter ++
245
+ return s , s .error
246
+ }
247
+ return s , nil
248
+ }
249
+
250
+ func (s testSqlHandler ) Columns () []string {
251
+ return []string {"foo" , "bar" }
252
+ }
253
+
254
+ func (s testSqlHandler ) Next (dest []driver.Value ) error {
255
+ testRows ++
256
+ if testRows > 5 {
257
+ return io .EOF
258
+ }
259
+ dest [0 ] = "foo"
260
+ dest [1 ] = "bar"
261
+ return nil
262
+ }
263
+
264
+ func (s testSqlHandler ) Close () error {
265
+ return nil
266
+ }
0 commit comments