Skip to content

Commit

Permalink
feat: add concurrent support to Transaction and upgrade gorm version (
Browse files Browse the repository at this point in the history
#229)

* feat: add transaction race test

* feat: upgrade gorm to v1.25.8

* feat: upgrade gorm driver version
  • Loading branch information
MuZhou233 authored Mar 27, 2024
1 parent ffe1951 commit c442abe
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 197 deletions.
24 changes: 24 additions & 0 deletions adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import (
_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/sync/errgroup"
"gorm.io/driver/mysql"
"gorm.io/driver/postgres"
"gorm.io/driver/sqlserver"
Expand Down Expand Up @@ -698,3 +700,25 @@ func TestTransaction(t *testing.T) {
return
}
}

func TestTransactionRace(t *testing.T) {
a := initAdapter(t, "mysql", "root:@tcp(127.0.0.1:3306)/", "casbin", "casbin_rule")
e, _ := casbin.NewEnforcer("examples/rbac_model.conf", a)

concurrency := 10

var g errgroup.Group
for i := 0; i < concurrency; i++ {
i := i
g.Go(func() error {
return e.GetAdapter().(*Adapter).Transaction(e, func(e casbin.IEnforcer) error {
_, err := e.AddPolicy("jack", fmt.Sprintf("data%d", i), "write")
if err != nil {
return err
}
return nil
})
})
}
require.NoError(t, g.Wait())
}
32 changes: 15 additions & 17 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ require (
github.com/agiledragon/gomonkey/v2 v2.2.0
github.com/casbin/casbin/v2 v2.77.1
github.com/glebarez/sqlite v1.7.0
github.com/go-sql-driver/mysql v1.6.0
github.com/go-sql-driver/mysql v1.7.0
github.com/lib/pq v1.10.2
github.com/stretchr/testify v1.8.0
gorm.io/driver/mysql v1.4.1
gorm.io/driver/postgres v1.4.4
gorm.io/driver/sqlserver v1.4.1
gorm.io/gorm v1.24.5
github.com/stretchr/testify v1.8.4
golang.org/x/sync v0.1.0
gorm.io/driver/mysql v1.5.6
gorm.io/driver/postgres v1.5.7
gorm.io/driver/sqlserver v1.5.3
gorm.io/gorm v1.25.8
gorm.io/plugin/dbresolver v1.3.0
)

Expand All @@ -24,26 +25,23 @@ require (
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect
github.com/golang-sql/sqlexp v0.1.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.13.0 // indirect
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgproto3/v2 v2.3.1 // indirect
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
github.com/jackc/pgtype v1.12.0 // indirect
github.com/jackc/pgx/v4 v4.17.2 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgx/v5 v5.4.3 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/kr/text v0.1.0 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/microsoft/go-mssqldb v0.17.0 // indirect
github.com/microsoft/go-mssqldb v1.6.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230126093431-47fa9a501578 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/tidwall/gjson v1.14.4 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
golang.org/x/crypto v0.0.0-20221005025214-4161e89ecf1b // indirect
golang.org/x/sys v0.4.0 // indirect
golang.org/x/text v0.3.8 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
modernc.org/libc v1.22.2 // indirect
modernc.org/mathutil v1.5.0 // indirect
Expand Down
Loading

0 comments on commit c442abe

Please sign in to comment.