代码拉取完成,页面将自动刷新
// +build go1.13
package gbase8s
import (
"context"
"testing"
)
// TestStatementCaching tests to ensure statement caching is working
func TestStatementCaching(t *testing.T) {
if TestDisableDatabase {
t.SkipNow()
}
t.Parallel()
var err error
db := testGetDB("?stmt_cache_size=10")
if db == nil {
t.Fatal("db is null")
}
defer func() {
err = db.Close()
if err != nil {
t.Fatal("db close error:", err)
}
}()
ctx, cancel := context.WithTimeout(context.Background(), TestContextTimeout)
conn, err := db.Conn(ctx)
cancel()
// we need to get access to the raw connection so we can access the different fields on the gbase8s.Stmt
var rawConn *Conn
// NOTE that conn.Raw() is only available with Go >= 1.13
_ = conn.Raw(func(driverConn interface{}) error {
rawConn = driverConn.(*Conn)
return nil
})
ctx, cancel = context.WithTimeout(context.Background(), TestContextTimeout)
stmt, err := rawConn.PrepareContext(ctx, "select ?, ?, ? from dual")
cancel()
if err != nil {
t.Fatal("prepare error:", err)
}
rawStmt := stmt.(*Stmt)
if rawStmt.cacheKey != "select ?, ?, ? from dual" {
err := stmt.Close()
if err != nil {
t.Fatal("stmt close error:", err)
}
t.Fatalf("cacheKey not equal: expected %s, but got %s", "select ?, ?, ? from dual", rawStmt.cacheKey)
}
// closing the statement should put the statement into the cache
err = stmt.Close()
if err != nil {
t.Fatal("stmt close error:", err)
}
ctx, cancel = context.WithTimeout(context.Background(), TestContextTimeout)
stmt, err = rawConn.PrepareContext(ctx, "select ?, ?, ? from dual")
cancel()
if err != nil {
t.Fatal("prepare error:", err)
}
rawStmt = stmt.(*Stmt)
if rawStmt.cacheKey != "select ?, ?, ? from dual" {
err := stmt.Close()
if err != nil {
t.Fatal("stmt close error:", err)
}
t.Fatalf("cacheKey not equal: expected %s, but got %s", "select ?, ?, ? from dual", rawStmt.cacheKey)
}
err = stmt.Close()
if err != nil {
t.Fatal("stmt close error:", err)
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。