MCPcopy
hub / github.com/apache/casbin / TestTransactionConflicts

Function TestTransactionConflicts

transaction_test.go:224–285  ·  view source on GitHub ↗

Test transaction conflicts.

(t *testing.T)

Source from the content-addressed store, hash-verified

222
223// Test transaction conflicts.
224func TestTransactionConflicts(t *testing.T) {
225 adapter := NewMockTransactionalAdapter()
226 e, err := NewTransactionalEnforcer("examples/rbac_model.conf", adapter)
227 if err != nil {
228 t.Fatalf("Failed to create transactional enforcer: %v", err)
229 }
230 adapter.Enforcer = e.Enforcer
231
232 ctx := context.Background()
233
234 // Test Case 1: Two transactions commit
235 t.Run("TwoTransactionsCommit", func(t *testing.T) {
236 tx1, _ := e.BeginTransaction(ctx)
237 tx2, _ := e.BeginTransaction(ctx)
238
239 // Commit both transactions
240 if err := tx1.Commit(); err != nil {
241 t.Fatalf("Failed to commit tx1: %v", err)
242 }
243 if err := tx2.Commit(); err != nil {
244 t.Fatalf("Failed to commit tx2: %v", err)
245 }
246
247 // Verify both transactions were committed
248 if !tx1.IsCommitted() {
249 t.Error("Transaction 1 should be committed")
250 }
251 if !tx2.IsCommitted() {
252 t.Error("Transaction 2 should be committed")
253 }
254 })
255
256 // Test Case 2: Transaction rollback
257 t.Run("TransactionRollback", func(t *testing.T) {
258 tx, _ := e.BeginTransaction(ctx)
259
260 // Rollback transaction
261 if err := tx.Rollback(); err != nil {
262 t.Fatalf("Failed to rollback transaction: %v", err)
263 }
264
265 // Verify transaction was rolled back
266 if !tx.IsRolledBack() {
267 t.Error("Transaction should be rolled back")
268 }
269 })
270
271 // Test Case 3: Cannot commit after rollback
272 t.Run("NoCommitAfterRollback", func(t *testing.T) {
273 tx, _ := e.BeginTransaction(ctx)
274
275 // Rollback transaction
276 if err := tx.Rollback(); err != nil {
277 t.Fatalf("Failed to rollback transaction: %v", err)
278 }
279
280 // Try to commit
281 if err := tx.Commit(); err == nil {

Callers

nothing calls this directly

Calls 8

BeginTransactionMethod · 0.95
NewTransactionalEnforcerFunction · 0.85
IsCommittedMethod · 0.80
IsRolledBackMethod · 0.80
CommitMethod · 0.65
RollbackMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…