Skip to content

Commit

Permalink
optimieze add ut for tm (#163)
Browse files Browse the repository at this point in the history
* add ut

* add ut for tm

* format imports

* resolve conflict in go mod

* fix loop variable v captured by func literal (govet)

* optimize ut for tm in gomonkey mock.

* format by goimports

* optimize code style

* resolve conflict.
  • Loading branch information
106umao authored Aug 1, 2022
1 parent 265d8b5 commit 52951c2
Show file tree
Hide file tree
Showing 10 changed files with 601 additions and 49 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ require (
github.com/stretchr/testify v1.7.1
go.uber.org/atomic v1.9.0
go.uber.org/zap v1.21.0
golang.org/x/tools v0.1.11 // indirect
vimagination.zapto.org/byteio v0.0.0-20200222190125-d27cba0f0b10
vimagination.zapto.org/memio v0.0.0-20200222190306-588ebc67b97d // indirect
)
8 changes: 0 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,6 @@ github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/yusufpapurcu/wmi v1.2.2 h1:KBNDSne4vP5mbSWnJbO+51IMOXJB67QiYCSBrubbPRg=
github.com/yusufpapurcu/wmi v1.2.2/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
github.com/zouyx/agollo/v3 v3.4.5 h1:7YCxzY9ZYaH9TuVUBvmI6Tk0mwMggikah+cfbYogcHQ=
Expand Down Expand Up @@ -841,7 +840,6 @@ golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8U
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20201002170205-7f63de1d35b0/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 h1:0es+/5331RGQPcXlMfP+WrnIIS6dNnNRe0WB02W0F4M=
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/exp v0.0.0-20180321215751-8460e604b9de/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
Expand Down Expand Up @@ -881,8 +879,6 @@ golang.org/x/mod v0.1.1-0.20191107180719-034126e5016b/go.mod h1:QqPTAvyqsEbceGzB
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20180530234432-1e491301e022/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down Expand Up @@ -927,7 +923,6 @@ golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/net v0.0.0-20210525063256-abc453219eb5/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20210917221730-978cfadd31cf/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211029224645-99673261e6eb/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211105192438-b53810dc28af/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 h1:CIJ76btIcR3eFI5EgSo6k1qKw9KJexJuRLI9G7Hp5wE=
Expand Down Expand Up @@ -1017,7 +1012,6 @@ golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210816074244-15123e1e1f71/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211106132015-ebca88c72f68/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211117180635-dee7805ff2e1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220111092808-5a964db01320/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down Expand Up @@ -1099,8 +1093,6 @@ golang.org/x/tools v0.0.0-20201014170642-d1624618ad65/go.mod h1:z6u4i615ZeAfBE4X
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.1.2/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.11 h1:loJ25fNOEhSXfHrpoGj91eCUThwdNX6u24rO1xnNteY=
golang.org/x/tools v0.1.11/go.mod h1:SgwaegtQh8clINPpECJMqnxLv9I09HLqnW3RMqW0CA4=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
23 changes: 11 additions & 12 deletions pkg/tm/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,24 @@ type (
)

type TransactionManager interface {
// GlobalStatusBegin a new global transaction.
// Begin a new global transaction.
Begin(applicationId, transactionServiceGroup, name string, timeout int64) (string, error)

// Global commit.
// Commit Global commit.
Commit(xid string) (message.GlobalStatus, error)

//Global rollback.
// Rollback Global rollback.
Rollback(xid string) (message.GlobalStatus, error)

// Get current status of the give transaction.
// GetStatus Get current status of the give transaction.
GetStatus(xid string) (message.GlobalStatus, error)

// Global report.
// GlobalReport Global report.
GlobalReport(xid string, globalStatus message.GlobalStatus) (message.GlobalStatus, error)
}

const (

/**
/*
* The REQUIRED.
* The default propagation.
*
Expand Down Expand Up @@ -78,7 +77,7 @@ const (
*/
REQUIRED = Propagation(0)

/**
/*
* The REQUIRES_NEW.
*
* <p>
Expand Down Expand Up @@ -111,7 +110,7 @@ const (
*/
REQUIRES_NEW = Propagation(1)

/**
/*
* The NOT_SUPPORTED.
*
* <p>
Expand All @@ -136,7 +135,7 @@ const (
*/
NOT_SUPPORTED = Propagation(2)

/**
/*
* The SUPPORTS.
*
* <p>
Expand All @@ -157,7 +156,7 @@ const (
*/
SUPPORTS = Propagation(3)

/**
/*
* The NEVER.
*
* <p>
Expand All @@ -177,7 +176,7 @@ const (
*/
NEVER = Propagation(4)

/**
/*
* The MANDATORY.
*
* <p>
Expand Down
11 changes: 5 additions & 6 deletions pkg/tm/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ import (
type ContextParam string

const (
seataContextVariable = ContextParam("seataContextVariable")
tccBusinessActionContext = ContextParam("tcc-business-action-context")
seataContextVariable = ContextParam("seataContextVariable")
)

type BusinessActionContext struct {
Expand Down Expand Up @@ -75,7 +74,7 @@ func GetTxName(ctx context.Context) string {
}

func SetTxName(ctx context.Context, name string) {
variable := ctx.Value(tccBusinessActionContext)
variable := ctx.Value(seataContextVariable)
if variable != nil {
variable.(*ContextVariable).TxName = name
}
Expand All @@ -86,15 +85,15 @@ func IsSeataContext(ctx context.Context) bool {
}

func GetBusinessActionContext(ctx context.Context) *BusinessActionContext {
variable := ctx.Value(tccBusinessActionContext)
variable := ctx.Value(seataContextVariable)
if variable == nil {
return nil
}
return variable.(*BusinessActionContext)
return variable.(*ContextVariable).BusinessActionContext
}

func SetBusinessActionContext(ctx context.Context, businessActionContext *BusinessActionContext) {
variable := ctx.Value(tccBusinessActionContext)
variable := ctx.Value(seataContextVariable)
if variable != nil {
variable.(*ContextVariable).BusinessActionContext = businessActionContext
}
Expand Down
136 changes: 136 additions & 0 deletions pkg/tm/context_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package tm

import (
"context"
"testing"

"github.com/stretchr/testify/assert"

"github.com/seata/seata-go/pkg/protocol/message"
)

func TestInitSeataContext(t *testing.T) {
ctx := InitSeataContext(context.Background())
assert.NotNil(t, ctx.Value(seataContextVariable))
}

func TestSetTxStatus(t *testing.T) {
ctx := InitSeataContext(context.Background())
SetTxStatus(ctx, message.GlobalStatusBegin)
assert.Equal(t, message.GlobalStatusBegin,
*(ctx.Value(seataContextVariable).(*ContextVariable).TxStatus))
}

func TestGetTxStatus(t *testing.T) {
ctx := InitSeataContext(context.Background())
SetTxStatus(ctx, message.GlobalStatusBegin)
assert.Equal(t, message.GlobalStatusBegin, *GetTxStatus(ctx))
}

func TestSetTxName(t *testing.T) {
ctx := InitSeataContext(context.Background())
SetTxName(ctx, "GlobalTransaction")
assert.Equal(t, "GlobalTransaction",
ctx.Value(seataContextVariable).(*ContextVariable).TxName)
}

func TestGetTxName(t *testing.T) {
ctx := InitSeataContext(context.Background())
SetTxName(ctx, "GlobalTransaction")
assert.Equal(t, "GlobalTransaction", GetTxName(ctx))
}

func TestIsSeataContext(t *testing.T) {
ctx := context.Background()
assert.False(t, IsSeataContext(ctx))
ctx = InitSeataContext(ctx)
assert.True(t, IsSeataContext(ctx))
}

func TestSetBusinessActionContext(t *testing.T) {
bac := &BusinessActionContext{}
ctx := InitSeataContext(context.Background())
SetBusinessActionContext(ctx, bac)
assert.Equal(t, bac,
ctx.Value(seataContextVariable).(*ContextVariable).BusinessActionContext)
}

func TestGetBusinessActionContext(t *testing.T) {
bac := &BusinessActionContext{}
ctx := InitSeataContext(context.Background())
SetBusinessActionContext(ctx, bac)
assert.Equal(t, bac, GetBusinessActionContext(ctx))
}

func TestSetTransactionRole(t *testing.T) {
ctx := InitSeataContext(context.Background())
SetTransactionRole(ctx, LAUNCHER)
assert.Equal(t, LAUNCHER,
*(ctx.Value(seataContextVariable).(*ContextVariable).TxRole))
}

func TestGetTransactionRole(t *testing.T) {
ctx := InitSeataContext(context.Background())
SetTransactionRole(ctx, LAUNCHER)
assert.Equal(t, LAUNCHER,
*GetTransactionRole(ctx))
}

func TestSetXID(t *testing.T) {
ctx := InitSeataContext(context.Background())
xid := "12345"
SetXID(ctx, xid)
assert.Equal(t, xid,
ctx.Value(seataContextVariable).(*ContextVariable).Xid)
}

func TestGetXID(t *testing.T) {
ctx := InitSeataContext(context.Background())
xid := "12345"
SetXID(ctx, xid)
assert.Equal(t, xid,
GetXID(ctx))
}

func TestIsTransactionOpened(t *testing.T) {
ctx := InitSeataContext(context.Background())
assert.False(t, IsTransactionOpened(ctx))
xid := "12345"
SetXID(ctx, xid)
assert.True(t, IsTransactionOpened(ctx))
}

func TestSetXIDCopy(t *testing.T) {
ctx := InitSeataContext(context.Background())
xid := "12345"
SetXIDCopy(ctx, xid)
assert.Equal(t, xid,
ctx.Value(seataContextVariable).(*ContextVariable).XidCopy)
assert.Equal(t, xid, GetXID(ctx))
}

func TestUnbindXid(t *testing.T) {
ctx := InitSeataContext(context.Background())
xid := "12345"
SetXID(ctx, xid)
assert.Equal(t, xid, GetXID(ctx))
UnbindXid(ctx)
assert.Empty(t, GetXID(ctx))
}
Loading

0 comments on commit 52951c2

Please sign in to comment.