|
| 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 config |
| 19 | + |
| 20 | +import ( |
| 21 | + "testing" |
| 22 | + "time" |
| 23 | + |
| 24 | + "seata.apache.org/seata-go/pkg/datasource/sql/exec/at" |
| 25 | + "seata.apache.org/seata-go/pkg/rm" |
| 26 | +) |
| 27 | + |
| 28 | +// TestInit tests the Init function with various configurations |
| 29 | +func TestInit(t *testing.T) { |
| 30 | + tests := []struct { |
| 31 | + name string |
| 32 | + config rm.LockConfig |
| 33 | + want rm.LockConfig |
| 34 | + }{ |
| 35 | + { |
| 36 | + name: "typical configuration", |
| 37 | + config: rm.LockConfig{ |
| 38 | + RetryInterval: 10 * time.Millisecond, |
| 39 | + RetryTimes: 30, |
| 40 | + RetryPolicyBranchRollbackOnConflict: true, |
| 41 | + }, |
| 42 | + want: rm.LockConfig{ |
| 43 | + RetryInterval: 10 * time.Millisecond, |
| 44 | + RetryTimes: 30, |
| 45 | + RetryPolicyBranchRollbackOnConflict: true, |
| 46 | + }, |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "zero values", |
| 50 | + config: rm.LockConfig{ |
| 51 | + RetryInterval: 0, |
| 52 | + RetryTimes: 0, |
| 53 | + RetryPolicyBranchRollbackOnConflict: false, |
| 54 | + }, |
| 55 | + want: rm.LockConfig{ |
| 56 | + RetryInterval: 0, |
| 57 | + RetryTimes: 0, |
| 58 | + RetryPolicyBranchRollbackOnConflict: false, |
| 59 | + }, |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "edge case values", |
| 63 | + config: rm.LockConfig{ |
| 64 | + RetryInterval: 5 * time.Second, |
| 65 | + RetryTimes: 1000, |
| 66 | + RetryPolicyBranchRollbackOnConflict: true, |
| 67 | + }, |
| 68 | + want: rm.LockConfig{ |
| 69 | + RetryInterval: 5 * time.Second, |
| 70 | + RetryTimes: 1000, |
| 71 | + RetryPolicyBranchRollbackOnConflict: true, |
| 72 | + }, |
| 73 | + }, |
| 74 | + } |
| 75 | + |
| 76 | + for _, tt := range tests { |
| 77 | + t.Run(tt.name, func(t *testing.T) { |
| 78 | + at.LockConfig = rm.LockConfig{} |
| 79 | + |
| 80 | + Init(tt.config) |
| 81 | + |
| 82 | + if at.LockConfig != tt.want { |
| 83 | + t.Errorf("Init() = %+v, want %+v", at.LockConfig, tt.want) |
| 84 | + } |
| 85 | + }) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +// TestInitMultipleCalls tests that calling Init multiple times overwrites the previous configuration |
| 90 | +func TestInitMultipleCalls(t *testing.T) { |
| 91 | + firstConfig := rm.LockConfig{ |
| 92 | + RetryInterval: 10 * time.Millisecond, |
| 93 | + RetryTimes: 10, |
| 94 | + RetryPolicyBranchRollbackOnConflict: true, |
| 95 | + } |
| 96 | + Init(firstConfig) |
| 97 | + |
| 98 | + if at.LockConfig != firstConfig { |
| 99 | + t.Errorf("After first Init(), got %+v, want %+v", at.LockConfig, firstConfig) |
| 100 | + } |
| 101 | + |
| 102 | + secondConfig := rm.LockConfig{ |
| 103 | + RetryInterval: 20 * time.Millisecond, |
| 104 | + RetryTimes: 20, |
| 105 | + RetryPolicyBranchRollbackOnConflict: false, |
| 106 | + } |
| 107 | + Init(secondConfig) |
| 108 | + |
| 109 | + if at.LockConfig != secondConfig { |
| 110 | + t.Errorf("After second Init(), got %+v, want %+v", at.LockConfig, secondConfig) |
| 111 | + } |
| 112 | +} |
0 commit comments