Skip to content

Commit c2c0df8

Browse files
authored
feat: port 2 new test cases to jCasbin (#385)
1 parent 76e97d7 commit c2c0df8

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

src/main/java/org/casbin/jcasbin/main/SyncedEnforcer.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import org.casbin.jcasbin.persist.Watcher;
2020

2121
import java.util.List;
22+
import java.util.concurrent.TimeUnit;
23+
import java.util.concurrent.atomic.AtomicInteger;
2224
import java.util.concurrent.locks.Lock;
2325
import java.util.concurrent.locks.ReadWriteLock;
2426
import java.util.concurrent.locks.ReentrantReadWriteLock;
@@ -30,6 +32,8 @@
3032
public class SyncedEnforcer extends Enforcer {
3133

3234
private final static ReadWriteLock READ_WRITE_LOCK = new ReentrantReadWriteLock();
35+
private final Object stopAutoLoad = new Object();
36+
private final AtomicInteger autoLoadRunning = new AtomicInteger(0);
3337

3438
/**
3539
* ;
@@ -98,6 +102,45 @@ public SyncedEnforcer(String modelPath, String policyFile, boolean enableLog) {
98102
super(modelPath, policyFile, enableLog);
99103
}
100104

105+
public boolean isAutoLoadingRunning() {
106+
return autoLoadRunning.get() != 0;
107+
}
108+
109+
public void startAutoLoadPolicy(long d) {
110+
if (!autoLoadRunning.compareAndSet(0, 1)) {
111+
return;
112+
}
113+
114+
Thread thread = new Thread(() -> {
115+
try {
116+
int n = 1;
117+
while (true) {
118+
TimeUnit.MILLISECONDS.sleep(d);
119+
loadPolicy();
120+
// Uncomment this line to see when the policy is loaded.
121+
// System.out.println("Load policy for time: " + n);
122+
n++;
123+
if (Thread.interrupted()) {
124+
break;
125+
}
126+
}
127+
} catch (InterruptedException ignored) {
128+
// Thread interrupted, exit the loop
129+
} finally {
130+
autoLoadRunning.set(0);
131+
}
132+
});
133+
thread.start();
134+
}
135+
136+
public void stopAutoLoadPolicy() {
137+
if (isAutoLoadingRunning()) {
138+
synchronized (stopAutoLoad) {
139+
stopAutoLoad.notify();
140+
}
141+
}
142+
}
143+
101144
/**
102145
* setWatcher sets the current watcher.
103146
*

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,72 @@
2121

2222
import java.io.FileInputStream;
2323
import java.io.IOException;
24+
import java.util.concurrent.TimeUnit;
2425

2526
import static java.util.Arrays.asList;
2627
import static org.casbin.jcasbin.main.CoreEnforcer.newModel;
2728
import static org.casbin.jcasbin.main.TestUtil.*;
2829
import static org.casbin.jcasbin.main.TestUtil.testEnforceEx;
30+
import static org.junit.Assert.assertEquals;
2931

3032
public class SyncedEnforcerUnitTest {
33+
34+
public static void testEnforceSync(SyncedEnforcer e, String sub, Object obj, String act, boolean res) {
35+
assertEquals(res, e.enforce(sub, obj, act));
36+
}
37+
38+
@Test
39+
public void testSync(){
40+
SyncedEnforcer e = new SyncedEnforcer("examples/basic_model.conf", "examples/basic_policy.csv");
41+
// Start reloading the policy every 200 ms.
42+
e.startAutoLoadPolicy(TimeUnit.MILLISECONDS.toMillis(200));
43+
44+
testEnforceSync(e, "alice", "data1", "read", true);
45+
testEnforceSync(e, "alice", "data1", "write", false);
46+
testEnforceSync(e, "alice", "data2", "read", false);
47+
testEnforceSync(e, "alice", "data2", "write", false);
48+
testEnforceSync(e, "bob", "data1", "read", false);
49+
testEnforceSync(e, "bob", "data1", "write", false);
50+
testEnforceSync(e, "bob", "data2", "read", false);
51+
testEnforceSync(e, "bob", "data2", "write", true);
52+
53+
// Simulate a policy change
54+
e.clearPolicy();
55+
testEnforceSync(e, "bob", "data2", "write", false);
56+
57+
// Wait for at least one sync
58+
try {
59+
TimeUnit.MILLISECONDS.sleep(300);
60+
} catch (InterruptedException ex) {
61+
Thread.currentThread().interrupt(); // restore interrupted status
62+
}
63+
64+
testEnforceSync(e, "bob", "data2", "write", true);
65+
66+
// Stop the reloading policy periodically.
67+
e.stopAutoLoadPolicy();
68+
}
69+
70+
@Test
71+
public void testStopAutoLoadPolicy(){
72+
SyncedEnforcer e = new SyncedEnforcer("examples/basic_model.conf", "examples/basic_policy.csv");
73+
e.startAutoLoadPolicy(TimeUnit.MILLISECONDS.toMillis(5));
74+
75+
if (!e.isAutoLoadingRunning()){
76+
System.err.println("auto load is not running");
77+
}
78+
e.stopAutoLoadPolicy();
79+
// Need a moment, to exit goroutine
80+
try {
81+
TimeUnit.MILLISECONDS.sleep(10);
82+
} catch (InterruptedException ex) {
83+
Thread.currentThread().interrupt();
84+
}
85+
if (e.isAutoLoadingRunning()) {
86+
System.err.println("auto load is still running");
87+
}
88+
}
89+
3190
@Test
3291
public void testKeyMatchModelInMemory() {
3392
Model m = newModel();

0 commit comments

Comments
 (0)