Skip to content

Commit

Permalink
fix: FastEnforcer not fast
Browse files Browse the repository at this point in the history
  • Loading branch information
mbierma committed May 20, 2024
1 parent d90b8c7 commit 55b7c9e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion casbin/fast_enforcer.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def enforce(self, *rvals):
"""decides whether a "subject" can access a "object" with the operation "action",
input parameters are usually: (sub, obj, act).
"""
if FastEnforcer._cache_key_order is None:
if self._cache_key_order is None:
result, _ = self.enforce_ex(*rvals)
else:
keys = [rvals[x] for x in self._cache_key_order]
Expand Down
19 changes: 19 additions & 0 deletions tests/test_fast_enforcer.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

import os
import time
from typing import Sequence
from unittest import TestCase

Expand All @@ -35,6 +36,24 @@ def get_enforcer(self, model=None, adapter=None, cache_key_order: Sequence[int]


class TestFastEnforcer(TestCaseBase):
def test_performance(self) -> None:
e1 = self.get_enforcer(
get_examples("performance/rbac_with_pattern_large_scale_model.conf"),
get_examples("performance/rbac_with_pattern_large_scale_policy.csv"),
)
e2 = self.get_enforcer(
get_examples("performance/rbac_with_pattern_large_scale_model.conf"),
get_examples("performance/rbac_with_pattern_large_scale_policy.csv"),
[2, 1],
)
s_e1 = time.time()
e1.enforce("alice", "data1", "read")
t_e1 = time.time() - s_e1
s_e2 = time.time()
e2.enforce("alice", "data1", "read")
t_e2 = time.time() - s_e2
assert t_e1 > t_e2 * 5

def test_creates_proper_policy(self) -> None:
e = self.get_enforcer(
get_examples("basic_model.conf"),
Expand Down

0 comments on commit 55b7c9e

Please sign in to comment.