diff --git a/graphenej/src/main/java/de/bitsharesmunich/graphenej/RPC.java b/graphenej/src/main/java/de/bitsharesmunich/graphenej/RPC.java index bf97c50..2375925 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/RPC.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/RPC.java @@ -11,6 +11,7 @@ public class RPC { public static final String CALL_DATABASE = "database"; public static final String CALL_ASSET = "asset"; public static final String CALL_SET_SUBSCRIBE_CALLBACK = "set_subscribe_callback"; + public static final String CALL_CANCEL_ALL_SUBSCRIPTIONS = "cancel_all_subscriptions"; public static final String CALL_GET_ACCOUNT_BY_NAME = "get_account_by_name"; public static final String CALL_GET_ACCOUNTS = "get_accounts"; public static final String CALL_GET_DYNAMIC_GLOBAL_PROPERTIES = "get_dynamic_global_properties"; diff --git a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/SubscriptionMessagesHub.java b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/SubscriptionMessagesHub.java index 1935fbf..1250002 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/SubscriptionMessagesHub.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/SubscriptionMessagesHub.java @@ -198,6 +198,11 @@ public class SubscriptionMessagesHub extends BaseGrapheneHandler implements Subs subscriptionCounter = 0; } + public void cancelSubscriptions(){ + ApiCall unsubscribe = new ApiCall(databaseApiId, RPC.CALL_CANCEL_ALL_SUBSCRIPTIONS, new ArrayList(), RPC.VERSION, SUBCRIPTION_REQUEST); + mWebsocket.sendText(unsubscribe.toJsonString()); + } + public void addRequestHandler(BaseGrapheneHandler handler) throws RepeatedRequestIdException { if(mHandlerMap.get(handler.getRequestId()) != null){ throw new RepeatedRequestIdException("Already registered handler with id: "+handler.getRequestId()); diff --git a/graphenej/src/test/java/de/bitsharesmunich/graphenej/api/SubscriptionMessagesHubTest.java b/graphenej/src/test/java/de/bitsharesmunich/graphenej/api/SubscriptionMessagesHubTest.java index d76be97..ff543c3 100644 --- a/graphenej/src/test/java/de/bitsharesmunich/graphenej/api/SubscriptionMessagesHubTest.java +++ b/graphenej/src/test/java/de/bitsharesmunich/graphenej/api/SubscriptionMessagesHubTest.java @@ -6,6 +6,8 @@ import org.junit.Test; import java.io.Serializable; import java.util.List; +import java.util.Timer; +import java.util.TimerTask; import de.bitsharesmunich.graphenej.ObjectType; import de.bitsharesmunich.graphenej.Transaction; @@ -35,6 +37,59 @@ public class SubscriptionMessagesHubTest extends BaseApiTest { } }; + /** + * Testing the subscription and unsubscription features. + * + * The test is deemed successful if no exception is thown and the messages indeed + * are cancelled. + */ + @Test + public void testSubscribeUnsubscribe(){ + /** + * Task that will send a 'cancel_all_subscriptions' API message. + */ + TimerTask unsubscribeTask = new TimerTask() { + @Override + public void run() { + System.out.println("Cancelling all subscriptions"); + mMessagesHub.cancelSubscriptions(); + } + }; + + /** + * Task that will just finish the test. + */ + TimerTask shutdownTask = new TimerTask() { + + @Override + public void run() { + System.out.println("Finish test"); + synchronized (SubscriptionMessagesHubTest.this){ + SubscriptionMessagesHubTest.this.notifyAll(); + } + } + }; + + try{ + mMessagesHub = new SubscriptionMessagesHub("", "", true, mErrorListener); + mWebSocket.addListener(mMessagesHub); + mWebSocket.connect(); + + Timer timer = new Timer(); + timer.schedule(unsubscribeTask, 5000); + timer.schedule(shutdownTask, 15000); + + // Holding this thread while we get update notifications + synchronized (this){ + wait(); + } + } catch (InterruptedException e) { + System.out.println("InterruptedException. Msg: "+e.getMessage()); + } catch (WebSocketException e) { + System.out.println("WebSocketException. Msg: " + e.getMessage()); + } + } + @Test public void testGlobalPropertiesDeserializer(){ try{