Implementing a call to the 'cancel_all_subscriptions' method and its test

This commit is contained in:
Nelson R. Perez 2017-06-28 18:19:41 -05:00
parent 1f705f1a4e
commit 9ae1762fe2
3 changed files with 61 additions and 0 deletions

View file

@ -11,6 +11,7 @@ public class RPC {
public static final String CALL_DATABASE = "database"; public static final String CALL_DATABASE = "database";
public static final String CALL_ASSET = "asset"; public static final String CALL_ASSET = "asset";
public static final String CALL_SET_SUBSCRIBE_CALLBACK = "set_subscribe_callback"; 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_ACCOUNT_BY_NAME = "get_account_by_name";
public static final String CALL_GET_ACCOUNTS = "get_accounts"; public static final String CALL_GET_ACCOUNTS = "get_accounts";
public static final String CALL_GET_DYNAMIC_GLOBAL_PROPERTIES = "get_dynamic_global_properties"; public static final String CALL_GET_DYNAMIC_GLOBAL_PROPERTIES = "get_dynamic_global_properties";

View file

@ -198,6 +198,11 @@ public class SubscriptionMessagesHub extends BaseGrapheneHandler implements Subs
subscriptionCounter = 0; subscriptionCounter = 0;
} }
public void cancelSubscriptions(){
ApiCall unsubscribe = new ApiCall(databaseApiId, RPC.CALL_CANCEL_ALL_SUBSCRIPTIONS, new ArrayList<Serializable>(), RPC.VERSION, SUBCRIPTION_REQUEST);
mWebsocket.sendText(unsubscribe.toJsonString());
}
public void addRequestHandler(BaseGrapheneHandler handler) throws RepeatedRequestIdException { public void addRequestHandler(BaseGrapheneHandler handler) throws RepeatedRequestIdException {
if(mHandlerMap.get(handler.getRequestId()) != null){ if(mHandlerMap.get(handler.getRequestId()) != null){
throw new RepeatedRequestIdException("Already registered handler with id: "+handler.getRequestId()); throw new RepeatedRequestIdException("Already registered handler with id: "+handler.getRequestId());

View file

@ -6,6 +6,8 @@ import org.junit.Test;
import java.io.Serializable; import java.io.Serializable;
import java.util.List; import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import de.bitsharesmunich.graphenej.ObjectType; import de.bitsharesmunich.graphenej.ObjectType;
import de.bitsharesmunich.graphenej.Transaction; 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 @Test
public void testGlobalPropertiesDeserializer(){ public void testGlobalPropertiesDeserializer(){
try{ try{