Missing PubNub callback

I am doing functional tests for my project in java.

PubNub does not execute callbacks when I try to subscribe or publish in a test method, even though the connection is ok.

If I try to post or subscribe to a feed from a separate java client the callback works well.

public class FunctionalTest {

private Logger logger = LoggerFactory.getLogger(FunctionalTest.class);

@Test
public void verifyCreateSuccess() throws JAXBException,IOException {

    Pubnub pubnub = new Pubnub("publisher-key", "subscriber-key");

    Callback callback = new Callback() {
       public void successCallback(String channel, Object response) {
           logger.info("PUBLISHER::" + response.toString());
       }
       public void errorCallback(String channel, PubnubError error) {
           logger.info("PUBLISHER::" + error.toString());
       }
   };

   try {
       pubnub.publish("foo-test",new JSONObject().put("test", 1),callback);
   } catch (JSONException e) {
       e.printStackTrace();
   }

}

      

}

+3


source to share


1 answer


Using PubNub inside Java Unit Test

The JUnit test completes before the call to Pubnub completes. Put 60 seconds to sleep in one of the test cases and see if you get posting (not that it takes 60 seconds, just want you to pause enough time).



When you call a publish, the method returns immediately, but the work is queued, so we use a callback, test exits, the pubnub object falls out of scope and the callback never fires.

This is really not a PubNub issue, but an attempt to write a functional test with a unit test framework using an asynchronous framework like PubNub.

+1


source







All Articles