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

Function TestConcurrentTransactions

transaction_test.go:170–221  ·  view source on GitHub ↗

Test concurrent transactions.

(t *testing.T)

Source from the content-addressed store, hash-verified

168
169// Test concurrent transactions.
170func TestConcurrentTransactions(t *testing.T) {
171 adapter := NewMockTransactionalAdapter()
172 e, err := NewTransactionalEnforcer("examples/rbac_model.conf", adapter)
173 if err != nil {
174 t.Fatalf("Failed to create transactional enforcer: %v", err)
175 }
176 adapter.Enforcer = e.Enforcer
177
178 ctx := context.Background()
179
180 // Start first transaction
181 tx1, err := e.BeginTransaction(ctx)
182 if err != nil {
183 t.Fatalf("Failed to begin transaction 1: %v", err)
184 }
185
186 // Add policy in first transaction
187 ok, err := tx1.AddPolicy("alice", "data1", "read")
188 if !ok || err != nil {
189 t.Fatalf("Failed to add policy in transaction 1: %v", err)
190 }
191
192 // Start second transaction
193 tx2, err := e.BeginTransaction(ctx)
194 if err != nil {
195 t.Fatalf("Failed to begin transaction 2: %v", err)
196 }
197
198 // Add different policy in second transaction
199 ok, err = tx2.AddPolicy("bob", "data2", "write")
200 if !ok || err != nil {
201 t.Fatalf("Failed to add policy in transaction 2: %v", err)
202 }
203
204 // Commit first transaction
205 if err := tx1.Commit(); err != nil {
206 t.Fatalf("Failed to commit transaction 1: %v", err)
207 }
208
209 // Commit second transaction
210 if err := tx2.Commit(); err != nil {
211 t.Fatalf("Failed to commit transaction 2: %v", err)
212 }
213
214 // Verify transactions were committed
215 if !tx1.IsCommitted() {
216 t.Error("Transaction 1 should be committed")
217 }
218 if !tx2.IsCommitted() {
219 t.Error("Transaction 2 should be committed")
220 }
221}
222
223// Test transaction conflicts.
224func TestTransactionConflicts(t *testing.T) {

Callers

nothing calls this directly

Calls 7

BeginTransactionMethod · 0.95
NewTransactionalEnforcerFunction · 0.85
IsCommittedMethod · 0.80
AddPolicyMethod · 0.65
CommitMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…