103 lines
2.8 KiB
Go
103 lines
2.8 KiB
Go
package stardb
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestStarDB_RuntimeConfigConcurrent(t *testing.T) {
|
|
db := NewStarDB()
|
|
|
|
before := func(ctx context.Context, query string, args []interface{}) {}
|
|
after := func(ctx context.Context, query string, args []interface{}, duration time.Duration, err error) {}
|
|
|
|
var wg sync.WaitGroup
|
|
for i := 0; i < 16; i++ {
|
|
wg.Add(1)
|
|
go func(i int) {
|
|
defer wg.Done()
|
|
for j := 0; j < 1000; j++ {
|
|
if (i+j)%2 == 0 {
|
|
db.SetPlaceholderStyle(PlaceholderDollar)
|
|
} else {
|
|
db.SetPlaceholderStyle(PlaceholderQuestion)
|
|
}
|
|
db.SetSQLSlowThreshold(time.Duration((i+j)%5) * time.Millisecond)
|
|
db.SetSQLFingerprintEnabled((i+j)%3 == 0)
|
|
db.SetSQLFingerprintMode(SQLFingerprintMode((i + j) % 3))
|
|
db.SetSQLFingerprintKeepComments((i+j)%4 == 0)
|
|
db.SetSQLFingerprintCounterEnabled((i+j)%5 == 0)
|
|
if (i+j)%7 == 0 {
|
|
db.ResetSQLFingerprintCounters()
|
|
}
|
|
db.SetSQLHooks(before, after)
|
|
_ = db.PlaceholderStyle()
|
|
_ = db.SQLSlowThreshold()
|
|
_ = db.SQLFingerprintEnabled()
|
|
_ = db.SQLFingerprintMode()
|
|
_ = db.SQLFingerprintKeepComments()
|
|
_ = db.SQLFingerprintCounterEnabled()
|
|
_ = db.SQLFingerprintCounters()
|
|
_, _, _, _ = db.runtimeOptions()
|
|
}
|
|
}(i)
|
|
}
|
|
wg.Wait()
|
|
}
|
|
|
|
func TestStarDB_SQLFingerprintMode(t *testing.T) {
|
|
db := NewStarDB()
|
|
|
|
if got := db.SQLFingerprintMode(); got != SQLFingerprintBasic {
|
|
t.Fatalf("expected default mode SQLFingerprintBasic, got %v", got)
|
|
}
|
|
|
|
db.SetSQLFingerprintMode(SQLFingerprintMaskLiterals)
|
|
if got := db.SQLFingerprintMode(); got != SQLFingerprintMaskLiterals {
|
|
t.Fatalf("expected SQLFingerprintMaskLiterals, got %v", got)
|
|
}
|
|
|
|
db.SetSQLFingerprintMode(SQLFingerprintMode(99))
|
|
if got := db.SQLFingerprintMode(); got != SQLFingerprintBasic {
|
|
t.Fatalf("expected invalid mode fallback to SQLFingerprintBasic, got %v", got)
|
|
}
|
|
}
|
|
|
|
func TestStarDB_SQLFingerprintKeepComments(t *testing.T) {
|
|
db := NewStarDB()
|
|
|
|
if db.SQLFingerprintKeepComments() {
|
|
t.Fatal("expected default keep comments to be false")
|
|
}
|
|
|
|
db.SetSQLFingerprintKeepComments(true)
|
|
if !db.SQLFingerprintKeepComments() {
|
|
t.Fatal("expected keep comments to be true")
|
|
}
|
|
|
|
db.SetSQLFingerprintKeepComments(false)
|
|
if db.SQLFingerprintKeepComments() {
|
|
t.Fatal("expected keep comments to be false")
|
|
}
|
|
}
|
|
|
|
func TestStarDB_SQLFingerprintCounterSwitch(t *testing.T) {
|
|
db := NewStarDB()
|
|
|
|
if db.SQLFingerprintCounterEnabled() {
|
|
t.Fatal("expected default counter switch to be false")
|
|
}
|
|
|
|
db.SetSQLFingerprintCounterEnabled(true)
|
|
if !db.SQLFingerprintCounterEnabled() {
|
|
t.Fatal("expected counter switch to be true")
|
|
}
|
|
|
|
db.ResetSQLFingerprintCounters()
|
|
if got := len(db.SQLFingerprintCounters()); got != 0 {
|
|
t.Fatalf("expected empty counters after reset, got %d", got)
|
|
}
|
|
}
|