0

I have a implemented a class that acts as a Cache for objects. It has a DelayQueue for removing objects from cache.

class mySingletonClass {
  ConcurrentHashMap map<String,MyObject>();
  DelayQueue queue <MyObject>(); 

  boolean alreadySeen(MyObject myObject) {
    MyObject obj = map.putIfAbsent(myObject.getID(),myObject);
    if(obj != null) return true;
    queue.add(myObject);
    return false;
}

It works fine for single threaded. I am trying to test it with multiple threads concurrently. I have tried to reason about if this method is thread safe but I am unsure. I have tried the static analyzer FindBugs plugin. I am unsure how to unit test this. I have never used java.util.concurrent nor worked with threads so I am trying to learn it all relatively quickly.

My thoughts for unit testing were create an AtomicInteger and run multiple threads trying to add the same object to the map. If the method returns true increment the integer. This integer should equal one if successful. As my second test I was thinking similarly with AtomicInteger try to have maybe 10? threads with each thread adding a list of 5 unique objects and ensure the total after all threads are done is 5.

I have been playing with Executors and creating Runnable methods but I am getting NullPointerExceptions and I am not sure exactly what I am doing.

I'm looking for:

  1. Is this method thread safe? If not is there a way to make it thread safe (other than just making the method synchronized)?

  2. How to I implement the two unit tests with multiple threads in junit4?

dm03514
  • 54,664
  • 18
  • 108
  • 145
  • Can you post the stack trace? – Miguel Garnacho Vélez Jan 28 '19 at 14:45
  • @MiguelGarnachoVélez From the debugger the NullPointerException is variable scope issue I think. The variables I'm accessing are being set in by BeforeEach setup() method. When debugging it shows a null value for all of these. I tried to move them inside my test method and it made no difference. That's why I'm leaning towards I'm just implementing the Executor and Runnable method wrong. I can't find a good example that shows multiple threads running the same method of an object with variable inputs. – TheSingularity Jan 28 '19 at 14:54
  • Stolen from @Kibbee https://stackoverflow.com/questions/425756/multithread-a-unit-test The problem with this kind of approach is that the results are not reproducible. So, if there was a bug in your code, calling the same test multiple times would result in (for instance) a deadlock in some cases, while in other cases it would not. – John Camerin Jan 28 '19 at 20:36
  • Thanks @JohnCamerin. While this is for C# the assumptions still hold true. The consensus seems to be that this type of unit test even if implemented correctly (which I still don't know how to do) may or may not find bugs that are deterministic. I've searched around stackoverflow more and it seems all roads lead to this post https://stackoverflow.com/questions/12159/how-should-i-unit-test-threaded-code?rq=1 for those looking for similar answers. For now I just slapped on the synchronized keyword to the method and i will start back at the basics in an attempt to reason about the correctness. – TheSingularity Jan 29 '19 at 02:39
  • is `myObject.getID()` threadsafe? – dm03514 Jan 29 '19 at 13:03

0 Answers0