microwww / redis-mock

redis server for java, java redis mock
https://github.com/microwww/jedis-mock
Apache License 2.0
47 stars 18 forks source link

fix NPE on close #6

Closed shuttie closed 2 years ago

shuttie commented 2 years ago

While playing with redis-mock example code from readme, I've found that RedisServer throws NPE on close() call when schema is not set.

To reproduce:

    RedisServer server = new RedisServer();
    server.listener("127.0.0.1", 6379); // Redis runs in the background
    server.close();

which makes a loud blast:

java.lang.NullPointerException was thrown.
java.lang.NullPointerException
    at com.github.microwww.redis.RedisServer.close(RedisServer.java:132)

Although this code is not triggering the NPE:

    RedisServer server = new RedisServer();
    server.listener("127.0.0.1", 6379); // Redis runs in the background
    server.getSchema(); // MAGIC!
    server.close();

Looks like that schema is null by default when nobody set something custom with setSchema. In this PR we init the schema variable with the default value, as magical getSchema does. There is also a reproducer test added in the test suite.

lichangshu commented 2 years ago

thank you for your action sometimes we need to configure it by public void configScheme(int size, AbstractOperation... operation) {, So we can't initialize it in the constructor. i add

if (schema != null) schema.close();

And add an assertion

Assert.isTrue(this.schema == null, "Server is running, you can not modify it, please invoke it before `getSchema`");