Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion integrate_test/at/insert/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@


run:
go run $(DIRECTORY)/main.go
go run $(DIRECTORY)/main.go
21 changes: 21 additions & 0 deletions integrate_test/at/insert_on_update/Makefile
Original file line number Diff line number Diff line change
@@ -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
139 changes: 139 additions & 0 deletions integrate_test/at/insert_on_update/main.go
Original file line number Diff line number Diff line change
@@ -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
}
21 changes: 21 additions & 0 deletions integrate_test/at/select_for_update/Makefile
Original file line number Diff line number Diff line change
@@ -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
104 changes: 104 additions & 0 deletions integrate_test/at/select_for_update/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* 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")
}
}

// 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
}
Loading