Skip to content

Commit

Permalink
feat: port 2 new test cases to jCasbin (#385)
Browse files Browse the repository at this point in the history
  • Loading branch information
LMay001 committed Mar 1, 2024
1 parent 76e97d7 commit c2c0df8
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/main/java/org/casbin/jcasbin/main/SyncedEnforcer.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import org.casbin.jcasbin.persist.Watcher;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
Expand All @@ -30,6 +32,8 @@
public class SyncedEnforcer extends Enforcer {

private final static ReadWriteLock READ_WRITE_LOCK = new ReentrantReadWriteLock();
private final Object stopAutoLoad = new Object();
private final AtomicInteger autoLoadRunning = new AtomicInteger(0);

/**
* ;
Expand Down Expand Up @@ -98,6 +102,45 @@ public SyncedEnforcer(String modelPath, String policyFile, boolean enableLog) {
super(modelPath, policyFile, enableLog);
}

public boolean isAutoLoadingRunning() {
return autoLoadRunning.get() != 0;
}

public void startAutoLoadPolicy(long d) {
if (!autoLoadRunning.compareAndSet(0, 1)) {
return;
}

Thread thread = new Thread(() -> {
try {
int n = 1;
while (true) {
TimeUnit.MILLISECONDS.sleep(d);
loadPolicy();
// Uncomment this line to see when the policy is loaded.
// System.out.println("Load policy for time: " + n);
n++;
if (Thread.interrupted()) {
break;
}
}
} catch (InterruptedException ignored) {
// Thread interrupted, exit the loop
} finally {
autoLoadRunning.set(0);
}
});
thread.start();
}

public void stopAutoLoadPolicy() {
if (isAutoLoadingRunning()) {
synchronized (stopAutoLoad) {
stopAutoLoad.notify();
}
}
}

/**
* setWatcher sets the current watcher.
*
Expand Down
59 changes: 59 additions & 0 deletions src/test/java/org/casbin/jcasbin/main/SyncedEnforcerUnitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,72 @@

import java.io.FileInputStream;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import static java.util.Arrays.asList;
import static org.casbin.jcasbin.main.CoreEnforcer.newModel;
import static org.casbin.jcasbin.main.TestUtil.*;
import static org.casbin.jcasbin.main.TestUtil.testEnforceEx;
import static org.junit.Assert.assertEquals;

public class SyncedEnforcerUnitTest {

public static void testEnforceSync(SyncedEnforcer e, String sub, Object obj, String act, boolean res) {
assertEquals(res, e.enforce(sub, obj, act));
}

@Test
public void testSync(){
SyncedEnforcer e = new SyncedEnforcer("examples/basic_model.conf", "examples/basic_policy.csv");
// Start reloading the policy every 200 ms.
e.startAutoLoadPolicy(TimeUnit.MILLISECONDS.toMillis(200));

testEnforceSync(e, "alice", "data1", "read", true);
testEnforceSync(e, "alice", "data1", "write", false);
testEnforceSync(e, "alice", "data2", "read", false);
testEnforceSync(e, "alice", "data2", "write", false);
testEnforceSync(e, "bob", "data1", "read", false);
testEnforceSync(e, "bob", "data1", "write", false);
testEnforceSync(e, "bob", "data2", "read", false);
testEnforceSync(e, "bob", "data2", "write", true);

// Simulate a policy change
e.clearPolicy();
testEnforceSync(e, "bob", "data2", "write", false);

// Wait for at least one sync
try {
TimeUnit.MILLISECONDS.sleep(300);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt(); // restore interrupted status
}

testEnforceSync(e, "bob", "data2", "write", true);

// Stop the reloading policy periodically.
e.stopAutoLoadPolicy();
}

@Test
public void testStopAutoLoadPolicy(){
SyncedEnforcer e = new SyncedEnforcer("examples/basic_model.conf", "examples/basic_policy.csv");
e.startAutoLoadPolicy(TimeUnit.MILLISECONDS.toMillis(5));

if (!e.isAutoLoadingRunning()){
System.err.println("auto load is not running");
}
e.stopAutoLoadPolicy();
// Need a moment, to exit goroutine
try {
TimeUnit.MILLISECONDS.sleep(10);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
if (e.isAutoLoadingRunning()) {
System.err.println("auto load is still running");
}
}

@Test
public void testKeyMatchModelInMemory() {
Model m = newModel();
Expand Down

0 comments on commit c2c0df8

Please sign in to comment.