sh.handleRequestAtOwner(pubSubRequestPrototype, channel);
assertEquals(StatusCode.SUCCESS, ((PubSubResponse) channel.getMessagesWritten().get(0)).getStatusCode());
// make sure the channel was put in the maps
assertEquals(new TopicSubscriber(topic, subscriberId), sh.channel2sub.get(channel));
assertEquals(channel, sh.sub2Channel.get(new TopicSubscriber(topic, subscriberId)));
// make sure delivery was started
StartServingRequest startRequest = (StartServingRequest) dm.lastRequest.poll();
assertEquals(channel, ((ChannelEndPoint) startRequest.endPoint).getChannel());
assertEquals(false, startRequest.isHubSubscriber);
assertEquals(TrueFilter.class, startRequest.filter.getClass());
assertEquals(1, startRequest.seqIdToStartFrom.getLocalComponent());
assertEquals(subscriberId, startRequest.subscriberId);
assertEquals(topic, startRequest.topic);
// make sure subscription was registered
StubCallback<MessageSeqId> callback1 = new StubCallback<MessageSeqId>();
sm.serveSubscribeRequest(topic, SubscribeRequest.newBuilder(subRequestPrototype).setCreateOrAttach(
CreateOrAttach.CREATE).build(), MessageSeqId.newBuilder().setLocalComponent(10).build(), callback1,
null);
assertEquals(PubSubException.ClientAlreadySubscribedException.class, ConcurrencyUtils.take(callback1.queue)
.right().getClass());
// trying to subscribe again should throw an error
WriteRecordingChannel dupChannel = new WriteRecordingChannel();
sh.handleRequestAtOwner(pubSubRequestPrototype, dupChannel);
assertEquals(StatusCode.TOPIC_BUSY, ((PubSubResponse) dupChannel.getMessagesWritten().get(0)).getStatusCode());
// after disconnecting the channel, subscribe should work again
sh.channelDisconnected(channel);
dupChannel = new WriteRecordingChannel();
sh.handleRequestAtOwner(pubSubRequestPrototype, dupChannel);
assertEquals(StatusCode.SUCCESS, ((PubSubResponse) dupChannel.getMessagesWritten().get(0)).getStatusCode());
// test unsubscribe
channel = new WriteRecordingChannel();
ush.handleRequestAtOwner(pubSubRequestPrototype, channel);
assertEquals(StatusCode.MALFORMED_REQUEST, ((PubSubResponse) channel.getMessagesWritten().get(0))
.getStatusCode());
PubSubRequest unsubRequest = PubSubRequest.newBuilder(pubSubRequestPrototype).setUnsubscribeRequest(
UnsubscribeRequest.newBuilder().setSubscriberId(subscriberId)).build();
channel = new WriteRecordingChannel();
dm.lastRequest.clear();
ush.handleRequestAtOwner(unsubRequest, channel);
assertEquals(StatusCode.SUCCESS, ((PubSubResponse) channel.getMessagesWritten().get(0)).getStatusCode());
// make sure delivery has been stopped
assertEquals(new TopicSubscriber(topic, subscriberId), dm.lastRequest.poll());
// make sure the info is gone from the sm
StubCallback<MessageSeqId> callback2 = new StubCallback<MessageSeqId>();
sm.serveSubscribeRequest(topic, SubscribeRequest.newBuilder(subRequestPrototype).setCreateOrAttach(
CreateOrAttach.ATTACH).build(), MessageSeqId.newBuilder().setLocalComponent(10).build(), callback2,