Skip to content

Commit 1afb96a

Browse files
committed
fix: sync
1 parent d1e5f28 commit 1afb96a

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

src/test/java/org/casbin/jcasbin/main/EnforcerUnitTest.java

+62
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,19 @@
1717
import org.casbin.jcasbin.model.Model;
1818
import org.casbin.jcasbin.persist.Adapter;
1919
import org.casbin.jcasbin.persist.file_adapter.FileAdapter;
20+
import org.casbin.jcasbin.util.BuiltInFunctions;
2021
import org.casbin.jcasbin.util.EnforceContext;
2122
import org.casbin.jcasbin.util.Util;
2223
import org.junit.Assert;
2324
import org.junit.Test;
2425

26+
import java.io.File;
2527
import java.io.FileInputStream;
2628
import java.io.IOException;
29+
import java.nio.file.Files;
2730
import java.util.List;
2831
import java.util.Set;
32+
import java.util.concurrent.CountDownLatch;
2933

3034
import static java.util.Arrays.asList;
3135
import static org.casbin.jcasbin.main.CoreEnforcer.newModel;
@@ -689,6 +693,64 @@ public void testMultiplePolicyDefinitions() {
689693
testEnforceWithContext(e, enforceContext, new AbacAPIUnitTest.TestEvalRule("alice", 30), "/data1", "read", true);
690694
}
691695

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+
692754
public static class TestSub{
693755
private String name;
694756

0 commit comments

Comments
 (0)