Skip to content

Commit

Permalink
feat: change test format and logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ElrondfromRussia committed May 23, 2024
1 parent fc24b82 commit 14ee7ee
Showing 1 changed file with 56 additions and 37 deletions.
93 changes: 56 additions & 37 deletions dbpool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,49 +6,68 @@ import (
"testing"
"time"

. "github.com/NGRsoftlab/ngr-logging"

"github.com/jmoiron/sqlx"
)

func TestDbPoolCache(t *testing.T) {
LocalCache := New(30*time.Second, 5*time.Second)
defer LocalCache.ClearAll()

connStr1 := fmt.Sprintf("%s://%s:%s@%s/%s",
"testD1",
"testI",
"testP",
"testU",
"testPS")
connStr2 := fmt.Sprintf("%s://%s:%s@%s/%s",
"testD2",
"testI",
"testP",
"testU",
"testPS")

db := &sqlx.DB{
DB: &sql.DB{},
Mapper: nil,
tests := []struct {
name string
okResTimeout time.Duration
noResTimeout time.Duration
checkData map[string]*sqlx.DB
}{
{
name: "valid (nil dn.connections in data)",
okResTimeout: 1 * time.Second,
noResTimeout: 6 * time.Second,
checkData: map[string]*sqlx.DB{
fmt.Sprintf("%s://%s:%s@%s/%s",
"testD1",
"testI",
"testP",
"testU",
"testPS"): {
DB: &sql.DB{},
Mapper: nil,
},
fmt.Sprintf("%s://%s:%s@%s/%s",
"testD2",
"testI",
"testP",
"testU",
"testPS"): {
DB: &sql.DB{},
Mapper: nil,
},
},
},
}

LocalCache.Set(connStr1, db, 10*time.Second)
LocalCache.Set(connStr2, db, 10*time.Second)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
LocalCache := New(10*time.Second, 5*time.Second)
defer LocalCache.ClearAll()

cachedRes, ok := LocalCache.Get(connStr1)
if ok {
Logger.Debugf("cached db is here: %+v", cachedRes)
} else {
Logger.Debugf("no res: %s", connStr1)
}
for k, v := range tt.checkData {
LocalCache.Set(k, v, 5*time.Second)
}

time.Sleep(tt.okResTimeout)
for k := range tt.checkData {
_, ok := LocalCache.Get(k)
if !ok {
t.Error("no cached result found")
}
}

//time.Sleep(5 * time.Second)
//
//cachedRes, ok = LocalCache.Get(connStr)
//if ok {
// Logger.Debugf("cached db is here: %+v", cachedRes)
//} else {
// Logger.Debugf("no res: %s", connStr)
//}
time.Sleep(tt.noResTimeout)
for k := range tt.checkData {
_, ok := LocalCache.Get(k)
if ok {
t.Error("cached result was not deleted after timeout")
}
}

})
}
}

0 comments on commit 14ee7ee

Please sign in to comment.