|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- /*
- * 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"
- "github.com/seata/seata-go/pkg/rm/tcc/fence/enum"
- )
-
- 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))
- }
-
- func TestSetFencePhase(t *testing.T) {
- ctx := InitSeataContext(context.Background())
- phase := enum.FencePhaseCommit
- SetFencePhase(ctx, phase)
- assert.Equal(t, phase,
- ctx.Value(seataContextVariable).(*ContextVariable).FencePhase)
- }
-
- func TestGetFencePhase(t *testing.T) {
- ctx := InitSeataContext(context.Background())
- phase := enum.FencePhaseCommit
- SetFencePhase(ctx, phase)
- assert.Equal(t, phase,
- GetFencePhase(ctx))
- }
|