add more tests
This commit is contained in:
parent
176c24ad72
commit
4766038eba
@ -4,7 +4,6 @@ import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
@ -97,9 +96,7 @@ func (op *tracePlugin) Initialize(db *gorm.DB) (err error) {
|
||||
|
||||
// GetDsn get dsn from map config
|
||||
func GetDsn(conf map[string]string) string {
|
||||
if IsDockerCompose() {
|
||||
conf["host"] = strings.Replace(conf["host"], "localhost", "host.docker.internal", 1)
|
||||
}
|
||||
conf["host"] = MayReplaceLocalhost(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"])
|
||||
}
|
||||
|
||||
@ -163,9 +163,7 @@ func init() {
|
||||
// RestyClient.SetRetryCount(2)
|
||||
// RestyClient.SetRetryWaitTime(1 * time.Second)
|
||||
RestyClient.OnBeforeRequest(func(c *resty.Client, r *resty.Request) error {
|
||||
if IsDockerCompose() {
|
||||
r.URL = strings.Replace(r.URL, "localhost", "host.docker.internal", 1)
|
||||
}
|
||||
r.URL = MayReplaceLocalhost(r.URL)
|
||||
logrus.Printf("requesting: %s %s %v %v", r.Method, r.URL, r.Body, r.QueryParam)
|
||||
return nil
|
||||
})
|
||||
@ -247,7 +245,10 @@ func GetFuncName() string {
|
||||
return runtime.FuncForPC(pc).Name()
|
||||
}
|
||||
|
||||
// IsDockerCompose name is clear
|
||||
func IsDockerCompose() bool {
|
||||
return os.Getenv("IS_DOCKER_COMPOSE") != ""
|
||||
// MayReplaceLocalhost when run in docker compose, change localhost to host.docker.internal for accessing host network
|
||||
func MayReplaceLocalhost(host string) string {
|
||||
if os.Getenv("IS_DOCKER_COMPOSE") != "" {
|
||||
return strings.Replace(host, "localhost", "host.docker.internal", 1)
|
||||
}
|
||||
return host
|
||||
}
|
||||
|
||||
@ -5,6 +5,7 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@ -95,4 +96,11 @@ func TestSome(t *testing.T) {
|
||||
|
||||
func1 := GetFuncName()
|
||||
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)
|
||||
}
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
package dtmcli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"testing"
|
||||
|
||||
@ -21,4 +22,8 @@ func TestTypes(t *testing.T) {
|
||||
assert.Error(t, err)
|
||||
_, err = TransInfoFromQuery(url.Values{})
|
||||
assert.Error(t, err)
|
||||
|
||||
err2 := fmt.Errorf("an error")
|
||||
err3 := CheckDtmResponse(nil, err2)
|
||||
assert.Error(t, err2, err3)
|
||||
}
|
||||
|
||||
@ -44,7 +44,8 @@ func TestCover(t *testing.T) {
|
||||
defer handlePanic()
|
||||
checkAffected(db.DB)
|
||||
|
||||
go CronExpiredTrans(1)
|
||||
CronExpiredTrans(1)
|
||||
go sleepCronTime()
|
||||
}
|
||||
|
||||
func TestType(t *testing.T) {
|
||||
|
||||
@ -116,6 +116,7 @@ func (t *TransGlobal) Process(db *common.DB) {
|
||||
if TransProcessedTestChan != nil {
|
||||
logrus.Printf("processed: %s", t.Gid)
|
||||
TransProcessedTestChan <- t.Gid
|
||||
logrus.Printf("notified: %s", t.Gid)
|
||||
}
|
||||
}()
|
||||
logrus.Printf("processing: %s status: %s", t.Gid, t.Status)
|
||||
|
||||
@ -26,9 +26,6 @@ func (t *transTccProcessor) ExecBranch(db *common.DB, branch *TransBranch) {
|
||||
if strings.Contains(body, "SUCCESS") {
|
||||
t.touch(db, config.TransCronInterval)
|
||||
branch.changeStatus(db, "succeed")
|
||||
} else if branch.BranchType == "try" && strings.Contains(body, "FAILURE") {
|
||||
t.touch(db, config.TransCronInterval)
|
||||
branch.changeStatus(db, "failed")
|
||||
} else {
|
||||
panic(fmt.Errorf("unknown response: %s, will be retried", body))
|
||||
}
|
||||
|
||||
@ -2,6 +2,7 @@ package dtmsvr
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"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) {
|
||||
resp, rerr := tcc.CallBranch(data, Busi+"/TransOut", Busi+"/TransOutConfirm", Busi+"/TransOutRevert")
|
||||
assert.Contains(t, resp.String(), "SUCCESS")
|
||||
examples.MainSwitch.TransOutRevertResult.SetOnce("PENDING")
|
||||
_, rerr = tcc.CallBranch(data, Busi+"/TransIn", Busi+"/TransInConfirm", Busi+"/TransInRevert")
|
||||
assert.Error(t, rerr)
|
||||
return
|
||||
})
|
||||
assert.Error(t, err)
|
||||
WaitTransProcessed(gid)
|
||||
assert.Equal(t, "aborting", getTransStatus(gid))
|
||||
CronTransOnce(60 * time.Second)
|
||||
assert.Equal(t, "failed", getTransStatus(gid))
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user