|
17 | 17 | import org.casbin.jcasbin.model.Model;
|
18 | 18 | import org.casbin.jcasbin.persist.Adapter;
|
19 | 19 | import org.casbin.jcasbin.persist.file_adapter.FileAdapter;
|
| 20 | +import org.casbin.jcasbin.util.BuiltInFunctions; |
20 | 21 | import org.casbin.jcasbin.util.EnforceContext;
|
21 | 22 | import org.casbin.jcasbin.util.Util;
|
22 | 23 | import org.junit.Assert;
|
23 | 24 | import org.junit.Test;
|
24 | 25 |
|
| 26 | +import java.io.File; |
25 | 27 | import java.io.FileInputStream;
|
26 | 28 | import java.io.IOException;
|
| 29 | +import java.nio.file.Files; |
27 | 30 | import java.util.List;
|
28 | 31 | import java.util.Set;
|
| 32 | +import java.util.concurrent.CountDownLatch; |
29 | 33 |
|
30 | 34 | import static java.util.Arrays.asList;
|
31 | 35 | import static org.casbin.jcasbin.main.CoreEnforcer.newModel;
|
@@ -689,6 +693,64 @@ public void testMultiplePolicyDefinitions() {
|
689 | 693 | testEnforceWithContext(e, enforceContext, new AbacAPIUnitTest.TestEvalRule("alice", 30), "/data1", "read", true);
|
690 | 694 | }
|
691 | 695 |
|
| 696 | + @Test |
| 697 | + public void testHasLinkSynchronized(){ |
| 698 | + File testingDir = null; |
| 699 | + try { |
| 700 | + testingDir = Files.createTempDirectory("testingDir").toFile(); |
| 701 | + } catch (IOException e) { |
| 702 | + throw new RuntimeException(e); |
| 703 | + } |
| 704 | + |
| 705 | + File f = null; |
| 706 | + try { |
| 707 | + f = File.createTempFile("policies", null, testingDir); |
| 708 | + } catch (IOException e) { |
| 709 | + throw new RuntimeException(e); |
| 710 | + } |
| 711 | + |
| 712 | + FileAdapter a = new FileAdapter(f.getAbsolutePath()); |
| 713 | + |
| 714 | + Enforcer e = new Enforcer("examples/haslink_synchronized_model.conf", a); |
| 715 | + |
| 716 | + e.enableAutoSave(true); |
| 717 | + e.addNamedMatchingFunc("g", "keyMatch4", BuiltInFunctions::keyMatch4); |
| 718 | + |
| 719 | + // 添加 gs 角色的关系 |
| 720 | + String[][] gs = new String[1001][3]; |
| 721 | + gs[ 0] = new String[]{ "[email protected]", "temp", "alice"}; |
| 722 | + for (int i = 0; i < 1000; i++) { |
| 723 | + gs[i+1] = new String[]{i + "@alice.co", "temp", "alice"}; |
| 724 | + } |
| 725 | + e.addGroupingPolicies(gs); |
| 726 | + |
| 727 | + String[][] policy = {{"alice", "/data", "allow"}}; |
| 728 | + e.addPolicies(policy); |
| 729 | + e.savePolicy(); |
| 730 | + |
| 731 | + int n = 100; |
| 732 | + CountDownLatch countDownLatch = new CountDownLatch(n); |
| 733 | + |
| 734 | + for (int i = 0; i < n; i++) { |
| 735 | + int finalI = i; |
| 736 | + new Thread(() -> { |
| 737 | + boolean res = e.enforce("alice", "data"); |
| 738 | + if (!res){ |
| 739 | + System.out.println("result failure: " + finalI); |
| 740 | + } |
| 741 | + countDownLatch.countDown(); |
| 742 | + }).start(); |
| 743 | + } |
| 744 | + |
| 745 | + try { |
| 746 | + countDownLatch.await(); |
| 747 | + } catch (InterruptedException ex) { |
| 748 | + ex.printStackTrace(); |
| 749 | + } |
| 750 | + |
| 751 | + System.out.println("Done!"); |
| 752 | + } |
| 753 | + |
692 | 754 | public static class TestSub{
|
693 | 755 | private String name;
|
694 | 756 |
|
|
0 commit comments