master
go 59 lines 967 Bytes
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package mysqlfunc
4
5 import (
6 "database/sql"
7 "errors"
8 "sync"
9 "time"
10
11 "github.com/netdata/netdata/go/plugins/pkg/confopt"
12 )
13
14 type testDeps struct {
15 mu sync.RWMutex
16 db *sql.DB
17
18 cfg FunctionsConfig
19 }
20
21 func newTestDeps() *testDeps {
22 return &testDeps{
23 cfg: FunctionsConfig{
24 Timeout: confopt.Duration(time.Second),
25 TopQueries: TopQueriesConfig{
26 Timeout: confopt.Duration(time.Second),
27 Limit: 500,
28 },
29 DeadlockInfo: DeadlockInfoConfig{
30 Timeout: confopt.Duration(time.Second),
31 },
32 ErrorInfo: ErrorInfoConfig{
33 Timeout: confopt.Duration(time.Second),
34 },
35 },
36 }
37 }
38
39 func (d *testDeps) setDB(db *sql.DB) {
40 d.mu.Lock()
41 d.db = db
42 d.mu.Unlock()
43 }
44
45 func (d *testDeps) cleanup() {
46 d.mu.Lock()
47 d.db = nil
48 d.mu.Unlock()
49 }
50
51 func (d *testDeps) DB() (Queryer, error) {
52 d.mu.RLock()
53 db := d.db
54 d.mu.RUnlock()
55 if db == nil {
56 return nil, errors.New("db unavailable")
57 }
58 return db, nil
59 }