|
|
@ -1,6 +1,7 @@ |
|
|
|
package tech.pegasys.pantheon.ethereum.jsonrpc.websocket.subscription; |
|
|
|
package tech.pegasys.pantheon.ethereum.jsonrpc.websocket.subscription; |
|
|
|
|
|
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat; |
|
|
|
import static org.assertj.core.api.Assertions.assertThat; |
|
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.catchThrowable; |
|
|
|
import static org.hamcrest.CoreMatchers.both; |
|
|
|
import static org.hamcrest.CoreMatchers.both; |
|
|
|
import static org.hamcrest.CoreMatchers.equalTo; |
|
|
|
import static org.hamcrest.CoreMatchers.equalTo; |
|
|
|
import static org.hamcrest.CoreMatchers.instanceOf; |
|
|
|
import static org.hamcrest.CoreMatchers.instanceOf; |
|
|
@ -13,6 +14,7 @@ import tech.pegasys.pantheon.ethereum.jsonrpc.websocket.subscription.request.Uns |
|
|
|
import tech.pegasys.pantheon.ethereum.jsonrpc.websocket.subscription.syncing.SyncingSubscription; |
|
|
|
import tech.pegasys.pantheon.ethereum.jsonrpc.websocket.subscription.syncing.SyncingSubscription; |
|
|
|
|
|
|
|
|
|
|
|
import java.util.List; |
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
import java.util.UUID; |
|
|
|
|
|
|
|
|
|
|
|
import org.junit.Before; |
|
|
|
import org.junit.Before; |
|
|
|
import org.junit.Rule; |
|
|
|
import org.junit.Rule; |
|
|
@ -33,8 +35,7 @@ public class SubscriptionManagerTest { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void subscribeShouldCreateSubscription() { |
|
|
|
public void subscribeShouldCreateSubscription() { |
|
|
|
final SubscribeRequest subscribeRequest = |
|
|
|
final SubscribeRequest subscribeRequest = subscribeRequest(CONNECTION_ID); |
|
|
|
new SubscribeRequest(SubscriptionType.SYNCING, null, null, CONNECTION_ID); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
final Long subscriptionId = subscriptionManager.subscribe(subscribeRequest); |
|
|
|
final Long subscriptionId = subscriptionManager.subscribe(subscribeRequest); |
|
|
|
|
|
|
|
|
|
|
@ -49,8 +50,7 @@ public class SubscriptionManagerTest { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void unsubscribeExistingSubscriptionShouldDestroySubscription() { |
|
|
|
public void unsubscribeExistingSubscriptionShouldDestroySubscription() { |
|
|
|
final SubscribeRequest subscribeRequest = |
|
|
|
final SubscribeRequest subscribeRequest = subscribeRequest(CONNECTION_ID); |
|
|
|
new SubscribeRequest(SubscriptionType.SYNCING, null, null, CONNECTION_ID); |
|
|
|
|
|
|
|
final Long subscriptionId = subscriptionManager.subscribe(subscribeRequest); |
|
|
|
final Long subscriptionId = subscriptionManager.subscribe(subscribeRequest); |
|
|
|
|
|
|
|
|
|
|
|
assertThat(subscriptionManager.subscriptions().get(subscriptionId)).isNotNull(); |
|
|
|
assertThat(subscriptionManager.subscriptions().get(subscriptionId)).isNotNull(); |
|
|
@ -76,8 +76,7 @@ public class SubscriptionManagerTest { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void shouldAddSubscriptionToNewConnection() { |
|
|
|
public void shouldAddSubscriptionToNewConnection() { |
|
|
|
final SubscribeRequest subscribeRequest = |
|
|
|
final SubscribeRequest subscribeRequest = subscribeRequest(CONNECTION_ID); |
|
|
|
new SubscribeRequest(SubscriptionType.SYNCING, null, null, CONNECTION_ID); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
subscriptionManager.subscribe(subscribeRequest); |
|
|
|
subscriptionManager.subscribe(subscribeRequest); |
|
|
|
|
|
|
|
|
|
|
@ -90,8 +89,7 @@ public class SubscriptionManagerTest { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void shouldAddSubscriptionToExistingConnection() { |
|
|
|
public void shouldAddSubscriptionToExistingConnection() { |
|
|
|
final SubscribeRequest subscribeRequest = |
|
|
|
final SubscribeRequest subscribeRequest = subscribeRequest(CONNECTION_ID); |
|
|
|
new SubscribeRequest(SubscriptionType.SYNCING, null, null, CONNECTION_ID); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
subscriptionManager.subscribe(subscribeRequest); |
|
|
|
subscriptionManager.subscribe(subscribeRequest); |
|
|
|
|
|
|
|
|
|
|
@ -110,8 +108,7 @@ public class SubscriptionManagerTest { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void shouldRemoveSubscriptionFromExistingConnection() { |
|
|
|
public void shouldRemoveSubscriptionFromExistingConnection() { |
|
|
|
final SubscribeRequest subscribeRequest = |
|
|
|
final SubscribeRequest subscribeRequest = subscribeRequest(CONNECTION_ID); |
|
|
|
new SubscribeRequest(SubscriptionType.SYNCING, null, null, CONNECTION_ID); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
final Long subscriptionId1 = subscriptionManager.subscribe(subscribeRequest); |
|
|
|
final Long subscriptionId1 = subscriptionManager.subscribe(subscribeRequest); |
|
|
|
|
|
|
|
|
|
|
@ -140,8 +137,7 @@ public class SubscriptionManagerTest { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void shouldRemoveConnectionWithSingleSubscriptions() { |
|
|
|
public void shouldRemoveConnectionWithSingleSubscriptions() { |
|
|
|
final SubscribeRequest subscribeRequest = |
|
|
|
final SubscribeRequest subscribeRequest = subscribeRequest(CONNECTION_ID); |
|
|
|
new SubscribeRequest(SubscriptionType.SYNCING, null, null, CONNECTION_ID); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
final Long subscriptionId1 = subscriptionManager.subscribe(subscribeRequest); |
|
|
|
final Long subscriptionId1 = subscriptionManager.subscribe(subscribeRequest); |
|
|
|
|
|
|
|
|
|
|
@ -188,17 +184,37 @@ public class SubscriptionManagerTest { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void unsubscribeWithUnknownConnectionId() { |
|
|
|
public void unsubscribeOthersSubscriptionsNotHavingOwnSubscriptionShouldReturnNotFound() { |
|
|
|
final SubscribeRequest subscribeRequestOne = |
|
|
|
SubscribeRequest subscribeRequest = subscribeRequest(CONNECTION_ID); |
|
|
|
new SubscribeRequest(SubscriptionType.NEW_BLOCK_HEADERS, null, true, CONNECTION_ID); |
|
|
|
Long subscriptionId = subscriptionManager.subscribe(subscribeRequest); |
|
|
|
final long subscriptionId = subscriptionManager.subscribe(subscribeRequestOne); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
final boolean success = |
|
|
|
UnsubscribeRequest unsubscribeRequest = |
|
|
|
subscriptionManager.unsubscribe( |
|
|
|
new UnsubscribeRequest(subscriptionId, UUID.randomUUID().toString()); |
|
|
|
new UnsubscribeRequest(subscriptionId, "unknown-connection-id")); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assertThat(success).isTrue(); |
|
|
|
final Throwable thrown = |
|
|
|
|
|
|
|
catchThrowable(() -> subscriptionManager.unsubscribe(unsubscribeRequest)); |
|
|
|
|
|
|
|
assertThat(thrown).isInstanceOf(SubscriptionNotFoundException.class); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// TODO vertx event bus testing for response
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void unsubscribeOthersSubscriptionsHavingOwnSubscriptionShouldReturnNotFound() { |
|
|
|
|
|
|
|
String ownConnectionId = UUID.randomUUID().toString(); |
|
|
|
|
|
|
|
SubscribeRequest ownSubscribeRequest = subscribeRequest(ownConnectionId); |
|
|
|
|
|
|
|
subscriptionManager.subscribe(ownSubscribeRequest); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String otherConnectionId = UUID.randomUUID().toString(); |
|
|
|
|
|
|
|
SubscribeRequest otherSubscribeRequest = subscribeRequest(otherConnectionId); |
|
|
|
|
|
|
|
Long otherSubscriptionId = subscriptionManager.subscribe(otherSubscribeRequest); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
UnsubscribeRequest unsubscribeRequest = |
|
|
|
|
|
|
|
new UnsubscribeRequest(otherSubscriptionId, ownConnectionId); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
final Throwable thrown = |
|
|
|
|
|
|
|
catchThrowable(() -> subscriptionManager.unsubscribe(unsubscribeRequest)); |
|
|
|
|
|
|
|
assertThat(thrown).isInstanceOf(SubscriptionNotFoundException.class); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private SubscribeRequest subscribeRequest(final String connectionId) { |
|
|
|
|
|
|
|
return new SubscribeRequest(SubscriptionType.SYNCING, null, null, connectionId); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|