Skip to content

Commit

Permalink
add context timeout for lib
Browse files Browse the repository at this point in the history
  • Loading branch information
mazchew committed Sep 22, 2024
1 parent d276277 commit ce19311
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions tests/unittest/querybindblocker_timeout/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package main

import (
"database/sql"
"fmt"
"os"
"testing"
"time"

"github.com/paypal/hera/tests/unittest/testutil"
"github.com/paypal/hera/utility/logger"
)

var mx testutil.Mux

func cfg() (map[string]string, map[string]string, testutil.WorkerType) {
fmt.Println("setup() begin")
appcfg := make(map[string]string)
// best to chose an "unique" port in case golang runs tests in parallel
appcfg["bind_port"] = "31002"
appcfg["log_level"] = "5"
appcfg["log_file"] = "hera.log"
appcfg["rac_sql_interval"] = "0"
appcfg["enable_query_bind_blocker"] = "true"
appcfg["management_queries_timeout_us"] = "100"

opscfg := make(map[string]string)
opscfg["opscfg.default.server.max_connections"] = "3"
opscfg["opscfg.default.server.log_level"] = "5"
if os.Getenv("WORKER") == "postgres" {
return appcfg, opscfg, testutil.PostgresWorker
}
return appcfg, opscfg, testutil.MySQLWorker
}

func teardown() {
mx.StopServer()
}

func TestMain(m *testing.M) {
os.Exit(testutil.UtilMain(m, cfg, nil))
}

func TestQueryBindBlockerWithTimeout(t *testing.T) {
testutil.DBDirect("create table hera_rate_limiter (herasqlhash numeric not null, herasqltext varchar(4000) not null, bindvarname varchar(200) not null, bindvarvalue varchar(200) not null, blockperc numeric not null, heramodule varchar(100) not null, end_time numeric not null, remarks varchar(200) not null)", os.Getenv("MYSQL_IP"), "heratestdb", testutil.MySQL)

logger.GetLogger().Log(logger.Debug, "TestQueryBindBlockerWithTimeout begin +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n")
time.Sleep(16 * time.Second)
hostname, _ := os.Hostname()
db, err := sql.Open("heraloop", hostname+":31002")
if err != nil {
t.Fatal("Error starting Mux:", err)
return
}
db.SetMaxIdleConns(0)
defer db.Close()
time.Sleep(5 * time.Second)
out := testutil.RegexCountFile("loading query bind blocker: context deadline exceeded", "hera.log")
if out < 1 {
err = nil
t.Fatalf("query bind blocker management query should fail with context timeout")
}

logger.GetLogger().Log(logger.Debug, "TestQueryBindBlockerWithTimeout done -------------------------------------------------------------")

}

0 comments on commit ce19311

Please sign in to comment.