Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: refactor the test module and fix ci bugs #9

Merged
merged 4 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:

steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v2

- name: Set up JDK 1.8
uses: actions/setup-java@v1
Expand Down
10 changes: 9 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<slf4j.version>1.7.25</slf4j.version>
<!--<slf4j.version>1.7.25</slf4j.version>-->
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<distributionManagement>
Expand Down Expand Up @@ -80,6 +82,12 @@
<version>2.12.4</version>
</dependency>

<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<version>4.1.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
90 changes: 50 additions & 40 deletions src/test/java/org/casbin/test/RedisWatcherExTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.casbin.test;

import io.lettuce.core.RedisURI;
import org.awaitility.Awaitility;
import org.casbin.jcasbin.main.Enforcer;
import org.casbin.jcasbin.model.Model;
import org.casbin.jcasbin.persist.WatcherEx;
Expand All @@ -13,8 +14,10 @@

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

import static org.awaitility.Durations.TEN_SECONDS;


public class RedisWatcherExTest {
private Enforcer enforcer;
Expand All @@ -24,78 +27,83 @@ public class RedisWatcherExTest {
public void initWatcher() {
WatcherOptions options = new WatcherOptions();
options.setChannel("jcasbin-channel");
options.setOptions(RedisURI.builder().withHost("192.168.101.65").withPort(6379).withPassword("redis").build());

options.setOptions(RedisURI.builder()
.withHost("127.0.0.1")
.withPort(6379)
.withPassword("foobared")
.build());
enforcer = new Enforcer("examples/rbac_model.conf", "examples/rbac_policy.csv");
watcher = new RedisWatcherEx(options);
enforcer.setWatcher(watcher);
}

@Test
public void testUpdate() throws InterruptedException {
CountDownLatch countDownLatch = new CountDownLatch(1);
watcher.setUpdateCallback((msg)-> {
countDownLatch.countDown();
System.out.println("test method : " + msg);
public void testUpdate() {
AtomicReference<String> message = new AtomicReference<>(null);
watcher.setUpdateCallback((msg) -> {
message.set(msg);
});
watcher.update();
Assert.assertTrue(countDownLatch.await(5, TimeUnit.SECONDS));
}

Awaitility.await().atMost(TEN_SECONDS).until(() -> message.get() != null);
System.out.println("test method : " + message.get());
}
@Test
public void testUpdateForAddPolicy() throws InterruptedException {
CountDownLatch countDownLatch = new CountDownLatch(1);
AtomicReference<String> message = new AtomicReference<>(null);
watcher.setUpdateCallback((msg)-> {
countDownLatch.countDown();
System.out.println("test method : " + msg);
message.set(msg);
});
watcher.updateForAddPolicy("alice", "data1", "read");
Assert.assertTrue(countDownLatch.await(5, TimeUnit.SECONDS));

Awaitility.await().atMost(TEN_SECONDS).until(() -> message.get() != null);
System.out.println("test method : " + message.get());

}

@Test
public void testUpdateForRemovePolicy() throws InterruptedException {
CountDownLatch countDownLatch = new CountDownLatch(1);
watcher.setUpdateCallback((msg)-> {
countDownLatch.countDown();
System.out.println("test method : " + msg);
AtomicReference<String> message = new AtomicReference<>(null);
watcher.setUpdateCallback((msg) -> {
message.set(msg);
});
watcher.updateForRemovePolicy("alice", "data1", "read");
Assert.assertTrue(countDownLatch.await(5, TimeUnit.SECONDS));

Awaitility.await().atMost(TEN_SECONDS).until(() -> message.get() != null);
System.out.println("test method : " + message.get());

}

@Test
public void testUpdateForRemoveFilteredPolicy() throws InterruptedException {
CountDownLatch countDownLatch = new CountDownLatch(1);
watcher.setUpdateCallback((msg)-> {
countDownLatch.countDown();
System.out.println("test method :" + msg);
AtomicReference<String> message = new AtomicReference<>(null);
watcher.setUpdateCallback((msg) -> {
message.set(msg);
});
watcher.updateForRemoveFilteredPolicy("alice", "data1", 1,"read");
Assert.assertTrue(countDownLatch.await(5, TimeUnit.SECONDS));

Awaitility.await().atMost(TEN_SECONDS).until(() -> message.get() != null);
System.out.println("test method : " + message.get());

}

@Test
public void testUpdateForSavePolicy() throws InterruptedException {
CountDownLatch countDownLatch = new CountDownLatch(1);
watcher.setUpdateCallback((msg)-> {
countDownLatch.countDown();
System.out.println("test method :" + msg);
AtomicReference<String> message = new AtomicReference<>(null);
watcher.setUpdateCallback((msg) -> {
message.set(msg);
});
watcher.updateForSavePolicy(new Model());
Assert.assertTrue(countDownLatch.await(5, TimeUnit.SECONDS));
Awaitility.await().atMost(TEN_SECONDS).until(() -> message.get() != null);
System.out.println("test method : " + message.get());

}

@Test
public void testUpdateForAddPolicies() throws InterruptedException {
CountDownLatch countDownLatch = new CountDownLatch(1);
watcher.setUpdateCallback((msg)-> {
countDownLatch.countDown();
System.out.println("test method :" + msg);
AtomicReference<String> message = new AtomicReference<>(null);
watcher.setUpdateCallback((msg) -> {
message.set(msg);
});
List<List<String>> rules = Arrays.asList(
Arrays.asList("jack", "data4", "read"),
Expand All @@ -104,15 +112,15 @@ public void testUpdateForAddPolicies() throws InterruptedException {
Arrays.asList("ham", "data4", "write")
);
watcher.updateForAddPolicies("alice", "data1", rules);
Assert.assertTrue(countDownLatch.await(5, TimeUnit.SECONDS));
Awaitility.await().atMost(TEN_SECONDS).until(() -> message.get() != null);
System.out.println("test method : " + message.get());
}

@Test
public void testUpdateForRemovePolicies() throws InterruptedException {
CountDownLatch countDownLatch = new CountDownLatch(1);
watcher.setUpdateCallback((msg)-> {
countDownLatch.countDown();
System.out.println("test method :" + msg);
AtomicReference<String> message = new AtomicReference<>(null);
watcher.setUpdateCallback((msg) -> {
message.set(msg);
});
List<List<String>> rules = Arrays.asList(
Arrays.asList("jack", "data4", "read"),
Expand All @@ -121,6 +129,8 @@ public void testUpdateForRemovePolicies() throws InterruptedException {
Arrays.asList("ham", "data4", "write")
);
watcher.updateForRemovePolicies("alice", "data1", rules);
Assert.assertTrue(countDownLatch.await(5, TimeUnit.SECONDS));

Awaitility.await().atMost(TEN_SECONDS).until(() -> message.get() != null);
System.out.println("test method : " + message.get());
}
}
Loading