MCPcopy Index your code
hub / github.com/apache/casbin / TestSyncCache

Function TestSyncCache

enforcer_cached_synced_test.go:30–82  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

28}
29
30func TestSyncCache(t *testing.T) {
31 e, _ := NewSyncedCachedEnforcer("examples/basic_model.conf", "examples/basic_policy.csv")
32 e.expireTime = time.Millisecond
33 // The cache is enabled by default for NewCachedEnforcer.
34 g := sync.WaitGroup{}
35 goThread := 1000
36 g.Add(goThread)
37 for i := 0; i < goThread; i++ {
38 go func() {
39 _, _ = e.AddPolicy("alice", "data2", "read")
40 testSyncEnforceCache(t, e, "alice", "data2", "read", true)
41 if e.InvalidateCache() != nil {
42 panic("never reached")
43 }
44 g.Done()
45 }()
46 }
47 g.Wait()
48 _, _ = e.RemovePolicy("alice", "data2", "read")
49
50 testSyncEnforceCache(t, e, "alice", "data1", "read", true)
51 time.Sleep(time.Millisecond * 2) // coverage for expire
52 testSyncEnforceCache(t, e, "alice", "data1", "read", true)
53
54 testSyncEnforceCache(t, e, "alice", "data1", "write", false)
55 testSyncEnforceCache(t, e, "alice", "data2", "read", false)
56 testSyncEnforceCache(t, e, "alice", "data2", "write", false)
57 // The cache is enabled, calling RemovePolicy, LoadPolicy or RemovePolicies will
58 // also operate cached items.
59 _, _ = e.RemovePolicy("alice", "data1", "read")
60
61 testSyncEnforceCache(t, e, "alice", "data1", "read", false)
62 testSyncEnforceCache(t, e, "alice", "data1", "write", false)
63 testSyncEnforceCache(t, e, "alice", "data2", "read", false)
64 testSyncEnforceCache(t, e, "alice", "data2", "write", false)
65
66 e, _ = NewSyncedCachedEnforcer("examples/rbac_model.conf", "examples/rbac_policy.csv")
67
68 testSyncEnforceCache(t, e, "alice", "data1", "read", true)
69 testSyncEnforceCache(t, e, "bob", "data2", "write", true)
70 testSyncEnforceCache(t, e, "alice", "data2", "read", true)
71 testSyncEnforceCache(t, e, "alice", "data2", "write", true)
72
73 _, _ = e.RemovePolicies([][]string{
74 {"alice", "data1", "read"},
75 {"bob", "data2", "write"},
76 })
77
78 testSyncEnforceCache(t, e, "alice", "data1", "read", false)
79 testSyncEnforceCache(t, e, "bob", "data2", "write", false)
80 testSyncEnforceCache(t, e, "alice", "data2", "read", true)
81 testSyncEnforceCache(t, e, "alice", "data2", "write", true)
82}

Callers

nothing calls this directly

Calls 6

AddPolicyMethod · 0.95
InvalidateCacheMethod · 0.95
RemovePolicyMethod · 0.95
RemovePoliciesMethod · 0.95
NewSyncedCachedEnforcerFunction · 0.85
testSyncEnforceCacheFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…