|
| 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 | + |
| 18 | +package rm |
| 19 | + |
| 20 | +import ( |
| 21 | + "testing" |
| 22 | + "time" |
| 23 | + |
| 24 | + "github.com/stretchr/testify/assert" |
| 25 | +) |
| 26 | + |
| 27 | +func TestInitRm(t *testing.T) { |
| 28 | + originalConfig := rmConfig |
| 29 | + defer func() { |
| 30 | + rmConfig = originalConfig |
| 31 | + }() |
| 32 | + |
| 33 | + testConfig := RmConfig{ |
| 34 | + Config: Config{ |
| 35 | + AsyncCommitBufferLimit: 10000, |
| 36 | + ReportRetryCount: 5, |
| 37 | + TableMetaCheckEnable: false, |
| 38 | + ReportSuccessEnable: false, |
| 39 | + SagaBranchRegisterEnable: false, |
| 40 | + SagaJsonParser: "fastjson", |
| 41 | + SagaRetryPersistModeUpdate: false, |
| 42 | + SagaCompensatePersistModeUpdate: false, |
| 43 | + TccActionInterceptorOrder: -2147482648, |
| 44 | + SqlParserType: "druid", |
| 45 | + LockConfig: LockConfig{ |
| 46 | + RetryInterval: 30 * time.Second, |
| 47 | + RetryTimes: 10, |
| 48 | + RetryPolicyBranchRollbackOnConflict: true, |
| 49 | + }, |
| 50 | + }, |
| 51 | + ApplicationID: "test-app", |
| 52 | + TxServiceGroup: "test-service-group", |
| 53 | + } |
| 54 | + |
| 55 | + InitRm(testConfig) |
| 56 | + |
| 57 | + assert.Equal(t, testConfig.AsyncCommitBufferLimit, rmConfig.AsyncCommitBufferLimit) |
| 58 | + assert.Equal(t, testConfig.ReportRetryCount, rmConfig.ReportRetryCount) |
| 59 | + assert.Equal(t, testConfig.TableMetaCheckEnable, rmConfig.TableMetaCheckEnable) |
| 60 | + assert.Equal(t, testConfig.ReportSuccessEnable, rmConfig.ReportSuccessEnable) |
| 61 | + assert.Equal(t, testConfig.SagaBranchRegisterEnable, rmConfig.SagaBranchRegisterEnable) |
| 62 | + assert.Equal(t, testConfig.SagaJsonParser, rmConfig.SagaJsonParser) |
| 63 | + assert.Equal(t, testConfig.SagaRetryPersistModeUpdate, rmConfig.SagaRetryPersistModeUpdate) |
| 64 | + assert.Equal(t, testConfig.SagaCompensatePersistModeUpdate, rmConfig.SagaCompensatePersistModeUpdate) |
| 65 | + assert.Equal(t, testConfig.TccActionInterceptorOrder, rmConfig.TccActionInterceptorOrder) |
| 66 | + assert.Equal(t, testConfig.SqlParserType, rmConfig.SqlParserType) |
| 67 | + assert.Equal(t, testConfig.LockConfig.RetryInterval, rmConfig.LockConfig.RetryInterval) |
| 68 | + assert.Equal(t, testConfig.LockConfig.RetryTimes, rmConfig.LockConfig.RetryTimes) |
| 69 | + assert.Equal(t, testConfig.LockConfig.RetryPolicyBranchRollbackOnConflict, rmConfig.LockConfig.RetryPolicyBranchRollbackOnConflict) |
| 70 | + assert.Equal(t, testConfig.ApplicationID, rmConfig.ApplicationID) |
| 71 | + assert.Equal(t, testConfig.TxServiceGroup, rmConfig.TxServiceGroup) |
| 72 | +} |
| 73 | + |
| 74 | +func TestInitRm_ZeroConfig(t *testing.T) { |
| 75 | + originalConfig := rmConfig |
| 76 | + defer func() { |
| 77 | + rmConfig = originalConfig |
| 78 | + }() |
| 79 | + |
| 80 | + zeroConfig := RmConfig{} |
| 81 | + |
| 82 | + InitRm(zeroConfig) |
| 83 | + |
| 84 | + assert.Equal(t, 0, rmConfig.AsyncCommitBufferLimit) |
| 85 | + assert.Equal(t, 0, rmConfig.ReportRetryCount) |
| 86 | + assert.False(t, rmConfig.TableMetaCheckEnable) |
| 87 | + assert.False(t, rmConfig.ReportSuccessEnable) |
| 88 | + assert.False(t, rmConfig.SagaBranchRegisterEnable) |
| 89 | + assert.Equal(t, "", rmConfig.SagaJsonParser) |
| 90 | + assert.False(t, rmConfig.SagaRetryPersistModeUpdate) |
| 91 | + assert.False(t, rmConfig.SagaCompensatePersistModeUpdate) |
| 92 | + assert.Equal(t, 0, rmConfig.TccActionInterceptorOrder) |
| 93 | + assert.Equal(t, "", rmConfig.SqlParserType) |
| 94 | + assert.Equal(t, time.Duration(0), rmConfig.LockConfig.RetryInterval) |
| 95 | + assert.Equal(t, 0, rmConfig.LockConfig.RetryTimes) |
| 96 | + assert.False(t, rmConfig.LockConfig.RetryPolicyBranchRollbackOnConflict) |
| 97 | + assert.Equal(t, "", rmConfig.ApplicationID) |
| 98 | + assert.Equal(t, "", rmConfig.TxServiceGroup) |
| 99 | +} |
| 100 | + |
| 101 | +func TestInitRm_MultipleInits(t *testing.T) { |
| 102 | + originalConfig := rmConfig |
| 103 | + defer func() { |
| 104 | + rmConfig = originalConfig |
| 105 | + }() |
| 106 | + |
| 107 | + firstConfig := RmConfig{ |
| 108 | + Config: Config{ |
| 109 | + AsyncCommitBufferLimit: 5000, |
| 110 | + ReportRetryCount: 3, |
| 111 | + TableMetaCheckEnable: true, |
| 112 | + }, |
| 113 | + ApplicationID: "app-1", |
| 114 | + TxServiceGroup: "group-1", |
| 115 | + } |
| 116 | + |
| 117 | + secondConfig := RmConfig{ |
| 118 | + Config: Config{ |
| 119 | + AsyncCommitBufferLimit: 15000, |
| 120 | + ReportRetryCount: 10, |
| 121 | + TableMetaCheckEnable: false, |
| 122 | + }, |
| 123 | + ApplicationID: "app-2", |
| 124 | + TxServiceGroup: "group-2", |
| 125 | + } |
| 126 | + |
| 127 | + InitRm(firstConfig) |
| 128 | + assert.Equal(t, 5000, rmConfig.AsyncCommitBufferLimit) |
| 129 | + assert.Equal(t, 3, rmConfig.ReportRetryCount) |
| 130 | + assert.True(t, rmConfig.TableMetaCheckEnable) |
| 131 | + assert.Equal(t, "app-1", rmConfig.ApplicationID) |
| 132 | + assert.Equal(t, "group-1", rmConfig.TxServiceGroup) |
| 133 | + |
| 134 | + InitRm(secondConfig) |
| 135 | + assert.Equal(t, 15000, rmConfig.AsyncCommitBufferLimit) |
| 136 | + assert.Equal(t, 10, rmConfig.ReportRetryCount) |
| 137 | + assert.False(t, rmConfig.TableMetaCheckEnable) |
| 138 | + assert.Equal(t, "app-2", rmConfig.ApplicationID) |
| 139 | + assert.Equal(t, "group-2", rmConfig.TxServiceGroup) |
| 140 | +} |
| 141 | + |
| 142 | +func TestInitRm_ConfigIsolation(t *testing.T) { |
| 143 | + originalConfig := rmConfig |
| 144 | + defer func() { |
| 145 | + rmConfig = originalConfig |
| 146 | + }() |
| 147 | + |
| 148 | + testConfig := RmConfig{ |
| 149 | + Config: Config{ |
| 150 | + AsyncCommitBufferLimit: 1000, |
| 151 | + }, |
| 152 | + ApplicationID: "test-app", |
| 153 | + } |
| 154 | + |
| 155 | + InitRm(testConfig) |
| 156 | + assert.Equal(t, 1000, rmConfig.AsyncCommitBufferLimit) |
| 157 | + assert.Equal(t, "test-app", rmConfig.ApplicationID) |
| 158 | + |
| 159 | + testConfig.AsyncCommitBufferLimit = 2000 |
| 160 | + testConfig.ApplicationID = "modified-app" |
| 161 | + |
| 162 | + assert.Equal(t, 1000, rmConfig.AsyncCommitBufferLimit) |
| 163 | + assert.Equal(t, "test-app", rmConfig.ApplicationID) |
| 164 | +} |
| 165 | + |
| 166 | +func TestInitRm_ExtremeValues(t *testing.T) { |
| 167 | + originalConfig := rmConfig |
| 168 | + defer func() { |
| 169 | + rmConfig = originalConfig |
| 170 | + }() |
| 171 | + |
| 172 | + extremeConfig := RmConfig{ |
| 173 | + Config: Config{ |
| 174 | + AsyncCommitBufferLimit: 2147483647, |
| 175 | + ReportRetryCount: -2147483648, |
| 176 | + TableMetaCheckEnable: true, |
| 177 | + ReportSuccessEnable: true, |
| 178 | + SagaBranchRegisterEnable: true, |
| 179 | + SagaJsonParser: "very-long-json-parser-name-that-exceeds-normal-length", |
| 180 | + SagaRetryPersistModeUpdate: true, |
| 181 | + SagaCompensatePersistModeUpdate: true, |
| 182 | + TccActionInterceptorOrder: 2147483647, |
| 183 | + SqlParserType: "very-long-sql-parser-type-name", |
| 184 | + LockConfig: LockConfig{ |
| 185 | + RetryInterval: 24 * time.Hour, |
| 186 | + RetryTimes: 2147483647, |
| 187 | + RetryPolicyBranchRollbackOnConflict: true, |
| 188 | + }, |
| 189 | + }, |
| 190 | + ApplicationID: "very-long-application-id-that-exceeds-normal-length", |
| 191 | + TxServiceGroup: "very-long-tx-service-group-that-exceeds-normal-length", |
| 192 | + } |
| 193 | + |
| 194 | + InitRm(extremeConfig) |
| 195 | + |
| 196 | + assert.Equal(t, 2147483647, rmConfig.AsyncCommitBufferLimit) |
| 197 | + assert.Equal(t, -2147483648, rmConfig.ReportRetryCount) |
| 198 | + assert.True(t, rmConfig.TableMetaCheckEnable) |
| 199 | + assert.True(t, rmConfig.ReportSuccessEnable) |
| 200 | + assert.True(t, rmConfig.SagaBranchRegisterEnable) |
| 201 | + assert.Equal(t, "very-long-json-parser-name-that-exceeds-normal-length", rmConfig.SagaJsonParser) |
| 202 | + assert.True(t, rmConfig.SagaRetryPersistModeUpdate) |
| 203 | + assert.True(t, rmConfig.SagaCompensatePersistModeUpdate) |
| 204 | + assert.Equal(t, 2147483647, rmConfig.TccActionInterceptorOrder) |
| 205 | + assert.Equal(t, "very-long-sql-parser-type-name", rmConfig.SqlParserType) |
| 206 | + assert.Equal(t, 24*time.Hour, rmConfig.LockConfig.RetryInterval) |
| 207 | + assert.Equal(t, 2147483647, rmConfig.LockConfig.RetryTimes) |
| 208 | + assert.True(t, rmConfig.LockConfig.RetryPolicyBranchRollbackOnConflict) |
| 209 | + assert.Equal(t, "very-long-application-id-that-exceeds-normal-length", rmConfig.ApplicationID) |
| 210 | + assert.Equal(t, "very-long-tx-service-group-that-exceeds-normal-length", rmConfig.TxServiceGroup) |
| 211 | +} |
| 212 | + |
| 213 | +func TestGlobalRmConfigVariable(t *testing.T) { |
| 214 | + assert.NotNil(t, &rmConfig, "Global rmConfig variable should exist") |
| 215 | + |
| 216 | + var zeroConfig RmConfig |
| 217 | + if rmConfig == zeroConfig { |
| 218 | + assert.True(t, true, "Global rmConfig starts as zero value") |
| 219 | + } |
| 220 | +} |
0 commit comments