add more tests

This commit is contained in:
yedf2 2021-07-30 11:08:11 +08:00
parent 176c24ad72
commit 4766038eba
8 changed files with 30 additions and 14 deletions

View File

@ -4,7 +4,6 @@ import (
"database/sql" "database/sql"
"fmt" "fmt"
"regexp" "regexp"
"strings"
"time" "time"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
@ -97,9 +96,7 @@ func (op *tracePlugin) Initialize(db *gorm.DB) (err error) {
// GetDsn get dsn from map config // GetDsn get dsn from map config
func GetDsn(conf map[string]string) string { func GetDsn(conf map[string]string) string {
if IsDockerCompose() { conf["host"] = MayReplaceLocalhost(conf["host"])
conf["host"] = strings.Replace(conf["host"], "localhost", "host.docker.internal", 1)
}
// logrus.Printf("is docker: %t IS_DOCKER_COMPOSE: %s and conf host: %s", IsDockerCompose(), os.Getenv("IS_DOCKER_COMPOSE"), conf["host"]) // logrus.Printf("is docker: %t IS_DOCKER_COMPOSE: %s and conf host: %s", IsDockerCompose(), os.Getenv("IS_DOCKER_COMPOSE"), conf["host"])
return fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true&loc=Local", conf["user"], conf["password"], conf["host"], conf["port"], conf["database"]) return fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true&loc=Local", conf["user"], conf["password"], conf["host"], conf["port"], conf["database"])
} }

View File

@ -163,9 +163,7 @@ func init() {
// RestyClient.SetRetryCount(2) // RestyClient.SetRetryCount(2)
// RestyClient.SetRetryWaitTime(1 * time.Second) // RestyClient.SetRetryWaitTime(1 * time.Second)
RestyClient.OnBeforeRequest(func(c *resty.Client, r *resty.Request) error { RestyClient.OnBeforeRequest(func(c *resty.Client, r *resty.Request) error {
if IsDockerCompose() { r.URL = MayReplaceLocalhost(r.URL)
r.URL = strings.Replace(r.URL, "localhost", "host.docker.internal", 1)
}
logrus.Printf("requesting: %s %s %v %v", r.Method, r.URL, r.Body, r.QueryParam) logrus.Printf("requesting: %s %s %v %v", r.Method, r.URL, r.Body, r.QueryParam)
return nil return nil
}) })
@ -247,7 +245,10 @@ func GetFuncName() string {
return runtime.FuncForPC(pc).Name() return runtime.FuncForPC(pc).Name()
} }
// IsDockerCompose name is clear // MayReplaceLocalhost when run in docker compose, change localhost to host.docker.internal for accessing host network
func IsDockerCompose() bool { func MayReplaceLocalhost(host string) string {
return os.Getenv("IS_DOCKER_COMPOSE") != "" if os.Getenv("IS_DOCKER_COMPOSE") != "" {
return strings.Replace(host, "localhost", "host.docker.internal", 1)
}
return host
} }

View File

@ -5,6 +5,7 @@ import (
"io" "io"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"os"
"strings" "strings"
"testing" "testing"
@ -95,4 +96,11 @@ func TestSome(t *testing.T) {
func1 := GetFuncName() func1 := GetFuncName()
assert.Equal(t, true, strings.HasSuffix(func1, "TestSome")) assert.Equal(t, true, strings.HasSuffix(func1, "TestSome"))
os.Setenv("IS_DOCKER_COMPOSE", "1")
s := MayReplaceLocalhost("http://localhost")
assert.Equal(t, "http://host.docker.internal", s)
os.Setenv("IS_DOCKER_COMPOSE", "")
s2 := MayReplaceLocalhost("http://localhost")
assert.Equal(t, "http://localhost", s2)
} }

View File

@ -1,6 +1,7 @@
package dtmcli package dtmcli
import ( import (
"fmt"
"net/url" "net/url"
"testing" "testing"
@ -21,4 +22,8 @@ func TestTypes(t *testing.T) {
assert.Error(t, err) assert.Error(t, err)
_, err = TransInfoFromQuery(url.Values{}) _, err = TransInfoFromQuery(url.Values{})
assert.Error(t, err) assert.Error(t, err)
err2 := fmt.Errorf("an error")
err3 := CheckDtmResponse(nil, err2)
assert.Error(t, err2, err3)
} }

View File

@ -44,7 +44,8 @@ func TestCover(t *testing.T) {
defer handlePanic() defer handlePanic()
checkAffected(db.DB) checkAffected(db.DB)
go CronExpiredTrans(1) CronExpiredTrans(1)
go sleepCronTime()
} }
func TestType(t *testing.T) { func TestType(t *testing.T) {

View File

@ -116,6 +116,7 @@ func (t *TransGlobal) Process(db *common.DB) {
if TransProcessedTestChan != nil { if TransProcessedTestChan != nil {
logrus.Printf("processed: %s", t.Gid) logrus.Printf("processed: %s", t.Gid)
TransProcessedTestChan <- t.Gid TransProcessedTestChan <- t.Gid
logrus.Printf("notified: %s", t.Gid)
} }
}() }()
logrus.Printf("processing: %s status: %s", t.Gid, t.Status) logrus.Printf("processing: %s status: %s", t.Gid, t.Status)

View File

@ -26,9 +26,6 @@ func (t *transTccProcessor) ExecBranch(db *common.DB, branch *TransBranch) {
if strings.Contains(body, "SUCCESS") { if strings.Contains(body, "SUCCESS") {
t.touch(db, config.TransCronInterval) t.touch(db, config.TransCronInterval)
branch.changeStatus(db, "succeed") branch.changeStatus(db, "succeed")
} else if branch.BranchType == "try" && strings.Contains(body, "FAILURE") {
t.touch(db, config.TransCronInterval)
branch.changeStatus(db, "failed")
} else { } else {
panic(fmt.Errorf("unknown response: %s, will be retried", body)) panic(fmt.Errorf("unknown response: %s, will be retried", body))
} }

View File

@ -2,6 +2,7 @@ package dtmsvr
import ( import (
"testing" "testing"
"time"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/yedf/dtm/dtmcli" "github.com/yedf/dtm/dtmcli"
@ -33,9 +34,14 @@ func tccRollback(t *testing.T) {
err := dtmcli.TccGlobalTransaction(examples.DtmServer, gid, func(tcc *dtmcli.Tcc) (rerr error) { err := dtmcli.TccGlobalTransaction(examples.DtmServer, gid, func(tcc *dtmcli.Tcc) (rerr error) {
resp, rerr := tcc.CallBranch(data, Busi+"/TransOut", Busi+"/TransOutConfirm", Busi+"/TransOutRevert") resp, rerr := tcc.CallBranch(data, Busi+"/TransOut", Busi+"/TransOutConfirm", Busi+"/TransOutRevert")
assert.Contains(t, resp.String(), "SUCCESS") assert.Contains(t, resp.String(), "SUCCESS")
examples.MainSwitch.TransOutRevertResult.SetOnce("PENDING")
_, rerr = tcc.CallBranch(data, Busi+"/TransIn", Busi+"/TransInConfirm", Busi+"/TransInRevert") _, rerr = tcc.CallBranch(data, Busi+"/TransIn", Busi+"/TransInConfirm", Busi+"/TransInRevert")
assert.Error(t, rerr) assert.Error(t, rerr)
return return
}) })
assert.Error(t, err) assert.Error(t, err)
WaitTransProcessed(gid)
assert.Equal(t, "aborting", getTransStatus(gid))
CronTransOnce(60 * time.Second)
assert.Equal(t, "failed", getTransStatus(gid))
} }