Skip to content

Commit

Permalink
fix: sync
Browse files Browse the repository at this point in the history
  • Loading branch information
imp2002 committed Mar 17, 2024
1 parent d1e5f28 commit 1afb96a
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions src/test/java/org/casbin/jcasbin/main/EnforcerUnitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,19 @@
import org.casbin.jcasbin.model.Model;
import org.casbin.jcasbin.persist.Adapter;
import org.casbin.jcasbin.persist.file_adapter.FileAdapter;
import org.casbin.jcasbin.util.BuiltInFunctions;
import org.casbin.jcasbin.util.EnforceContext;
import org.casbin.jcasbin.util.Util;
import org.junit.Assert;
import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CountDownLatch;

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

@Test
public void testHasLinkSynchronized(){
File testingDir = null;
try {
testingDir = Files.createTempDirectory("testingDir").toFile();
} catch (IOException e) {
throw new RuntimeException(e);
}

File f = null;
try {
f = File.createTempFile("policies", null, testingDir);
} catch (IOException e) {
throw new RuntimeException(e);
}

FileAdapter a = new FileAdapter(f.getAbsolutePath());

Enforcer e = new Enforcer("examples/haslink_synchronized_model.conf", a);

e.enableAutoSave(true);
e.addNamedMatchingFunc("g", "keyMatch4", BuiltInFunctions::keyMatch4);

// 添加 gs 角色的关系
String[][] gs = new String[1001][3];
gs[0] = new String[]{"[email protected]", "temp", "alice"};
for (int i = 0; i < 1000; i++) {
gs[i+1] = new String[]{i + "@alice.co", "temp", "alice"};
}
e.addGroupingPolicies(gs);

String[][] policy = {{"alice", "/data", "allow"}};
e.addPolicies(policy);
e.savePolicy();

int n = 100;
CountDownLatch countDownLatch = new CountDownLatch(n);

for (int i = 0; i < n; i++) {
int finalI = i;
new Thread(() -> {
boolean res = e.enforce("alice", "data");
if (!res){
System.out.println("result failure: " + finalI);
}
countDownLatch.countDown();
}).start();
}

try {
countDownLatch.await();
} catch (InterruptedException ex) {
ex.printStackTrace();
}

System.out.println("Done!");
}

public static class TestSub{
private String name;

Expand Down

0 comments on commit 1afb96a

Please sign in to comment.