55import org .junit .Assert ;
66import org .junit .Before ;
77import org .junit .Test ;
8+ import redis .clients .jedis .JedisPoolConfig ;
9+
810import java .io .ByteArrayOutputStream ;
911import java .io .PrintStream ;
1012
1113public class RedisWatcherTest {
12- private RedisWatcher redisWatcher ;
14+ private RedisWatcher redisWatcher , redisConfigWatcher ;
1315 private final String expect ="update msg" ;
16+ private final String expectConfig ="update msg for config" ;
1417
1518 /**
1619 * You should replace the initWatcher() method's content with your own Redis instance.
1720 */
1821 @ Before
1922 public void initWatcher (){
2023 String redisTopic = "jcasbin-topic" ;
24+ String redisConfig = "jcasbin-config" ;
25+ JedisPoolConfig config = new JedisPoolConfig ();
26+ config .setMaxTotal (2 );
27+ config .setMaxWaitMillis (100 * 1000 );
2128 redisWatcher = new RedisWatcher ("127.0.0.1" ,6379 , redisTopic , 2000 , "foobared" );
29+ redisConfigWatcher = new RedisWatcher (config ,"127.0.0.1" ,6376 , redisConfig , 2000 , "foobaredConfig" );
2230 Enforcer enforcer = new Enforcer ();
2331 enforcer .setWatcher (redisWatcher );
32+ Enforcer configEnforcer = new Enforcer ();
33+ configEnforcer .setWatcher (redisConfigWatcher );
34+ }
35+
36+ @ Test
37+ public void testConfigUpdate () throws InterruptedException {
38+ ByteArrayOutputStream baos = new ByteArrayOutputStream ();
39+ System .setOut (new PrintStream (baos ));
40+ redisConfigWatcher .setUpdateCallback (()-> System .out .print (expectConfig ) );
41+ redisConfigWatcher .update ();
42+ Thread .sleep (100 );
43+ Assert .assertEquals (expectConfig , expectConfig );
2444 }
2545
2646 @ Test
@@ -40,12 +60,22 @@ public void testConsumerCallback() throws InterruptedException {
4060 });
4161 redisWatcher .update ();
4262 Thread .sleep (100 );
63+
64+ redisConfigWatcher .setUpdateCallback ((s ) -> {
65+ System .out .print (s );
66+ });
67+ redisConfigWatcher .update ();
68+ Thread .sleep (100 );
4369 }
4470
4571 @ Test
4672 public void testConnectWatcherWithoutPassword () {
4773 String redisTopic = "jcasbin-topic" ;
4874 RedisWatcher redisWatcherWithoutPassword = new RedisWatcher ("127.0.0.1" , 6378 , redisTopic );
4975 Assert .assertNotNull (redisWatcherWithoutPassword );
76+
77+ String redisConfig = "jcasbin-config" ;
78+ RedisWatcher redisConfigWatcherWithoutPassword = new RedisWatcher ("127.0.0.1" , 6377 , redisConfig );
79+ Assert .assertNotNull (redisConfigWatcherWithoutPassword );
5080 }
5181}
0 commit comments