|
21 | 21 |
|
22 | 22 | import java.io.FileInputStream; |
23 | 23 | import java.io.IOException; |
| 24 | +import java.util.concurrent.TimeUnit; |
24 | 25 |
|
25 | 26 | import static java.util.Arrays.asList; |
26 | 27 | import static org.casbin.jcasbin.main.CoreEnforcer.newModel; |
27 | 28 | import static org.casbin.jcasbin.main.TestUtil.*; |
28 | 29 | import static org.casbin.jcasbin.main.TestUtil.testEnforceEx; |
| 30 | +import static org.junit.Assert.assertEquals; |
29 | 31 |
|
30 | 32 | 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 | + |
31 | 90 | @Test |
32 | 91 | public void testKeyMatchModelInMemory() { |
33 | 92 | Model m = newModel(); |
|
0 commit comments