You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

context_test.go 4.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package tm
  18. import (
  19. "context"
  20. "testing"
  21. "github.com/stretchr/testify/assert"
  22. "github.com/seata/seata-go/pkg/protocol/message"
  23. "github.com/seata/seata-go/pkg/rm/tcc/fence/enum"
  24. )
  25. func TestInitSeataContext(t *testing.T) {
  26. ctx := InitSeataContext(context.Background())
  27. assert.NotNil(t, ctx.Value(seataContextVariable))
  28. }
  29. func TestSetTxStatus(t *testing.T) {
  30. ctx := InitSeataContext(context.Background())
  31. SetTxStatus(ctx, message.GlobalStatusBegin)
  32. assert.Equal(t, message.GlobalStatusBegin,
  33. *(ctx.Value(seataContextVariable).(*ContextVariable).TxStatus))
  34. }
  35. func TestGetTxStatus(t *testing.T) {
  36. ctx := InitSeataContext(context.Background())
  37. SetTxStatus(ctx, message.GlobalStatusBegin)
  38. assert.Equal(t, message.GlobalStatusBegin, *GetTxStatus(ctx))
  39. }
  40. func TestSetTxName(t *testing.T) {
  41. ctx := InitSeataContext(context.Background())
  42. SetTxName(ctx, "GlobalTransaction")
  43. assert.Equal(t, "GlobalTransaction",
  44. ctx.Value(seataContextVariable).(*ContextVariable).TxName)
  45. }
  46. func TestGetTxName(t *testing.T) {
  47. ctx := InitSeataContext(context.Background())
  48. SetTxName(ctx, "GlobalTransaction")
  49. assert.Equal(t, "GlobalTransaction", GetTxName(ctx))
  50. }
  51. func TestIsSeataContext(t *testing.T) {
  52. ctx := context.Background()
  53. assert.False(t, IsSeataContext(ctx))
  54. ctx = InitSeataContext(ctx)
  55. assert.True(t, IsSeataContext(ctx))
  56. }
  57. func TestSetBusinessActionContext(t *testing.T) {
  58. bac := &BusinessActionContext{}
  59. ctx := InitSeataContext(context.Background())
  60. SetBusinessActionContext(ctx, bac)
  61. assert.Equal(t, bac,
  62. ctx.Value(seataContextVariable).(*ContextVariable).BusinessActionContext)
  63. }
  64. func TestGetBusinessActionContext(t *testing.T) {
  65. bac := &BusinessActionContext{}
  66. ctx := InitSeataContext(context.Background())
  67. SetBusinessActionContext(ctx, bac)
  68. assert.Equal(t, bac, GetBusinessActionContext(ctx))
  69. }
  70. func TestSetTransactionRole(t *testing.T) {
  71. ctx := InitSeataContext(context.Background())
  72. SetTransactionRole(ctx, LAUNCHER)
  73. assert.Equal(t, LAUNCHER,
  74. *(ctx.Value(seataContextVariable).(*ContextVariable).TxRole))
  75. }
  76. func TestGetTransactionRole(t *testing.T) {
  77. ctx := InitSeataContext(context.Background())
  78. SetTransactionRole(ctx, LAUNCHER)
  79. assert.Equal(t, LAUNCHER,
  80. *GetTransactionRole(ctx))
  81. }
  82. func TestSetXID(t *testing.T) {
  83. ctx := InitSeataContext(context.Background())
  84. xid := "12345"
  85. SetXID(ctx, xid)
  86. assert.Equal(t, xid,
  87. ctx.Value(seataContextVariable).(*ContextVariable).Xid)
  88. }
  89. func TestGetXID(t *testing.T) {
  90. ctx := InitSeataContext(context.Background())
  91. xid := "12345"
  92. SetXID(ctx, xid)
  93. assert.Equal(t, xid,
  94. GetXID(ctx))
  95. }
  96. func TestIsTransactionOpened(t *testing.T) {
  97. ctx := InitSeataContext(context.Background())
  98. assert.False(t, IsTransactionOpened(ctx))
  99. xid := "12345"
  100. SetXID(ctx, xid)
  101. assert.True(t, IsTransactionOpened(ctx))
  102. }
  103. func TestSetXIDCopy(t *testing.T) {
  104. ctx := InitSeataContext(context.Background())
  105. xid := "12345"
  106. SetXIDCopy(ctx, xid)
  107. assert.Equal(t, xid,
  108. ctx.Value(seataContextVariable).(*ContextVariable).XidCopy)
  109. assert.Equal(t, xid, GetXID(ctx))
  110. }
  111. func TestUnbindXid(t *testing.T) {
  112. ctx := InitSeataContext(context.Background())
  113. xid := "12345"
  114. SetXID(ctx, xid)
  115. assert.Equal(t, xid, GetXID(ctx))
  116. UnbindXid(ctx)
  117. assert.Empty(t, GetXID(ctx))
  118. }
  119. func TestSetFencePhase(t *testing.T) {
  120. ctx := InitSeataContext(context.Background())
  121. phase := enum.FencePhaseCommit
  122. SetFencePhase(ctx, phase)
  123. assert.Equal(t, phase,
  124. ctx.Value(seataContextVariable).(*ContextVariable).FencePhase)
  125. }
  126. func TestGetFencePhase(t *testing.T) {
  127. ctx := InitSeataContext(context.Background())
  128. phase := enum.FencePhaseCommit
  129. SetFencePhase(ctx, phase)
  130. assert.Equal(t, phase,
  131. GetFencePhase(ctx))
  132. }