Techcable / pircbotx

Automatically exported from code.google.com/p/pircbotx
0 stars 0 forks source link

WaitForQueue timeout does not work #204

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?

currentEvent = (MessageEvent) queue.waitFor(Arrays.asList(MessageEvent.class), 
10000, TimeUnit.MILLISECONDS);

What is the expected output? What do you see instead?

The above code will not honor the timeout setting of 10 seconds, instead it 
keeps waiting.

What version of the product are you using? On what operating system?

Version 2.0.1 on OS X.

Please provide any additional information below.

The LinkedBlockingQueue returns null if the specified waiting time elapses 
before an element is available. As a result, the foreach loop is empty and 
therefore the function does not return and simply polls again with the same 
timeout setting.

It can be fixed as following:

        public Event waitFor(@NonNull List<Class<? extends Event>> eventClasses, long timeout, @NonNull TimeUnit unit) throws InterruptedException {
                while (true) {
                        Event curEvent = eventQueue.poll(timeout, unit);
                        if (curEvent == null)
                                return curEvent;
                        for (Class<? extends Event> curEventClass : eventClasses)
                                if (curEventClass.isInstance(curEvent))
                                        return curEvent;
                }
        }

Original issue reported on code.google.com by Arnold.O...@gmail.com on 29 Sep 2014 at 6:38

GoogleCodeExporter commented 9 years ago
Sorry about that. Fixed in Revision 26567f3437b6

Original comment by Lord.Qua...@gmail.com on 30 Oct 2014 at 11:25