This is an automated email from the ASF dual-hosted git repository.
zfeng pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-seata-go-samples.git
The following commit(s) were added to refs/heads/main by this push:
new ae26760 feat: add at insert_on_update and select_for_update integrate
test case (#72)
ae26760 is described below
commit ae267606ad08c9e0f92fac5147d297a434b3db4d
Author: Zonglei Dong <[email protected]>
AuthorDate: Sat Dec 20 19:45:20 2025 +0800
feat: add at insert_on_update and select_for_update integrate test case
(#72)
* feat: add at insert_on_update and select_for_update integrate test case
* feat: at integrate
---
integrate_test/at/insert_on_update/Makefile | 21 ++++
integrate_test/at/insert_on_update/main.go | 139 +++++++++++++++++++++++++++
integrate_test/at/select_for_update/Makefile | 21 ++++
integrate_test/at/select_for_update/main.go | 114 ++++++++++++++++++++++
start_integrate_test.sh | 2 +
5 files changed, 297 insertions(+)
diff --git a/integrate_test/at/insert_on_update/Makefile
b/integrate_test/at/insert_on_update/Makefile
new file mode 100644
index 0000000..83ef710
--- /dev/null
+++ b/integrate_test/at/insert_on_update/Makefile
@@ -0,0 +1,21 @@
+# 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.
+
+
+
+run:
+ go run $(DIRECTORY)/main.go
\ No newline at end of file
diff --git a/integrate_test/at/insert_on_update/main.go
b/integrate_test/at/insert_on_update/main.go
new file mode 100644
index 0000000..ef442ee
--- /dev/null
+++ b/integrate_test/at/insert_on_update/main.go
@@ -0,0 +1,139 @@
+/*
+ * 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 main
+
+import (
+ "context"
+ "database/sql"
+ "fmt"
+ "log"
+ "time"
+
+ "gorm.io/driver/mysql"
+ "gorm.io/gorm"
+ "gorm.io/gorm/clause"
+ "seata.apache.org/seata-go/pkg/client"
+ sql2 "seata.apache.org/seata-go/pkg/datasource/sql"
+ "seata.apache.org/seata-go/pkg/tm"
+)
+
+type OrderTblModel struct {
+ Id int64 `gorm:"column:id" json:"id"`
+ UserId string `gorm:"column:user_id" json:"user_id"`
+ CommodityCode string `gorm:"commodity_code" json:"commodity_code"`
+ Count int64 `gorm:"count" json:"count"`
+ Money int64 `gorm:"money" json:"money"`
+ Descs string `gorm:"descs" json:"descs"`
+}
+
+func main() {
+ initConfig()
+
+ // test: insert on update
+ err := tm.WithGlobalTx(context.Background(), &tm.GtxConfig{
+ Name: "ATSampleLocalGlobalTx_InsertOnUpdate",
+ Timeout: time.Second * 30,
+ }, insertOnUpdateData)
+
+ if err != nil {
+ log.Fatalf("failed to init transaction: %v", err)
+ return
+ }
+
+ ctx := context.Background()
+
+ // check
+ if checkData(ctx) != nil {
+ panic("failed")
+ }
+
+ // wait clean undo log
+ time.Sleep(time.Second * 10)
+ if checkUndoLogData(ctx) != nil {
+ panic("failed")
+ }
+}
+
+func initConfig() {
+ client.InitPath("./conf/seatago.yml")
+ initDB()
+}
+
+var gormDB *gorm.DB
+
+func initDB() {
+ sqlDB, err := sql.Open(sql2.SeataATMySQLDriver,
"root:12345678@tcp(127.0.0.1:3306)/seata_client?multiStatements=true&interpolateParams=true")
+ if err != nil {
+ panic("init service error")
+ }
+
+ gormDB, err = gorm.Open(mysql.New(mysql.Config{
+ Conn: sqlDB,
+ }), &gorm.Config{})
+ if err != nil {
+ panic("open DB error")
+ }
+}
+
+func getData() OrderTblModel {
+ return OrderTblModel{
+ Id: 1,
+ UserId: "NO-100003",
+ CommodityCode: "C100001",
+ Count: 101,
+ Money: 11,
+ Descs: "insert desc",
+ }
+}
+
+// insertOnUpdateData insert on update one data
+func insertOnUpdateData(ctx context.Context) error {
+ data := getData()
+
+ return
gormDB.WithContext(ctx).Table("order_tbl").Clauses(clause.OnConflict{
+ Columns: []clause.Column{{Name: "id"}},
+ DoUpdates: clause.Assignments(map[string]interface{}{
+ "descs": data.Descs,
+ }),
+ }).Create(&data).Error
+}
+
+func checkData(ctx context.Context) error {
+ query := getData()
+ var count int64
+ err :=
gormDB.WithContext(ctx).Table("order_tbl").Where(query).Count(&count).Error
+ if err != nil {
+ return err
+ }
+ if count != 1 {
+ return fmt.Errorf("check data failed")
+ }
+ return nil
+}
+
+func checkUndoLogData(ctx context.Context) error {
+ var count int64
+ err := gormDB.WithContext(ctx).Table("undo_log").Count(&count).Error
+ if err != nil {
+ return err
+ }
+ if count != 0 {
+ return fmt.Errorf("check undolog failed")
+ }
+ return nil
+}
diff --git a/integrate_test/at/select_for_update/Makefile
b/integrate_test/at/select_for_update/Makefile
new file mode 100644
index 0000000..83ef710
--- /dev/null
+++ b/integrate_test/at/select_for_update/Makefile
@@ -0,0 +1,21 @@
+# 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.
+
+
+
+run:
+ go run $(DIRECTORY)/main.go
\ No newline at end of file
diff --git a/integrate_test/at/select_for_update/main.go
b/integrate_test/at/select_for_update/main.go
new file mode 100644
index 0000000..0ef2caa
--- /dev/null
+++ b/integrate_test/at/select_for_update/main.go
@@ -0,0 +1,114 @@
+/*
+ * 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 main
+
+import (
+ "context"
+ "database/sql"
+ "fmt"
+ "log"
+ "time"
+
+ "gorm.io/driver/mysql"
+ "gorm.io/gorm"
+ "gorm.io/gorm/clause"
+ "seata.apache.org/seata-go/pkg/client"
+ sql2 "seata.apache.org/seata-go/pkg/datasource/sql"
+ "seata.apache.org/seata-go/pkg/tm"
+)
+
+type OrderTblModel struct {
+ Id int64 `gorm:"column:id" json:"id"`
+ UserId string `gorm:"column:user_id" json:"user_id"`
+ CommodityCode string `gorm:"commodity_code" json:"commodity_code"`
+ Count int64 `gorm:"count" json:"count"`
+ Money int64 `gorm:"money" json:"money"`
+ Descs string `gorm:"descs" json:"descs"`
+}
+
+func main() {
+ initConfig()
+
+ // test: select for update
+ err := tm.WithGlobalTx(context.Background(), &tm.GtxConfig{
+ Name: "ATSampleLocalGlobalTx_SelectForUpdate",
+ Timeout: time.Second * 30,
+ }, selectForUpdateData)
+
+ if err != nil {
+ log.Fatalf("failed to init transaction: %v", err)
+ return
+ }
+
+ ctx := context.Background()
+
+ // wait clean undo log
+ time.Sleep(time.Second * 10)
+ if checkUndoLogData(ctx) != nil {
+ panic("failed")
+ }
+}
+
+func initConfig() {
+ client.InitPath("./conf/seatago.yml")
+ initDB()
+}
+
+var gormDB *gorm.DB
+
+func initDB() {
+ sqlDB, err := sql.Open(sql2.SeataATMySQLDriver,
"root:12345678@tcp(127.0.0.1:3306)/seata_client?multiStatements=true&interpolateParams=true")
+ if err != nil {
+ panic("init service error")
+ }
+
+ gormDB, err = gorm.Open(mysql.New(mysql.Config{
+ Conn: sqlDB,
+ }), &gorm.Config{})
+ if err != nil {
+ panic("open DB error")
+ }
+}
+
+func getData() OrderTblModel {
+ return OrderTblModel{
+ UserId: "NO-100003",
+ CommodityCode: "C100001",
+ Count: 101,
+ Money: 11,
+ Descs: "insert desc",
+ }
+}
+
+// selectForUpdateData select for update
+func selectForUpdateData(ctx context.Context) error {
+ var data OrderTblModel
+ return
gormDB.WithContext(ctx).Table("order_tbl").Where(&OrderTblModel{Id:
333}).Clauses(clause.Locking{Strength: "UPDATE"}).Find(&data).Error
+}
+
+func checkUndoLogData(ctx context.Context) error {
+ var count int64
+ err := gormDB.WithContext(ctx).Table("undo_log").Count(&count).Error
+ if err != nil {
+ return err
+ }
+ if count != 0 {
+ return fmt.Errorf("check undolog failed")
+ }
+ return nil
+}
diff --git a/start_integrate_test.sh b/start_integrate_test.sh
index 9cc7aa8..87e926b 100755
--- a/start_integrate_test.sh
+++ b/start_integrate_test.sh
@@ -17,6 +17,8 @@
# tests
array+=("integrate_test/at/insert")
+array+=("integrate_test/at/insert_on_update")
+array+=("integrate_test/at/select_for_update")
array+=("integrate_test/tcc/insert")
array+=("integrate_test/tcc/insert_on_update")
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]