@Test public void subscribe() throws Exception { final ChannelMetaData ch1 = new ChannelMetaData( 0, "C1", null, ChannelMetaData.FilterType.NONE, null, ChannelMetaData.CacheType.NONE, false, true ); final ChannelMetaData ch2 = new ChannelMetaData( 1, "C2", null, ChannelMetaData.FilterType.DYNAMIC, String.class, ChannelMetaData.CacheType.NONE, false, true ); final ChannelMetaData ch3 = new ChannelMetaData( 2, "C3", null, ChannelMetaData.FilterType.STATIC, String.class, ChannelMetaData.CacheType.NONE, false, true ); final ChannelMetaData[] channels = new ChannelMetaData[]{ ch1, ch2, ch3 }; final ChannelAddress address1 = new ChannelAddress( ch1.getChannelId(), null ); final ChannelAddress address2 = new ChannelAddress( ch2.getChannelId(), null ); final ChannelAddress address3 = new ChannelAddress( ch3.getChannelId(), null ); final TestFilter originalFilter = new TestFilter( 41 ); final TestFilter filter = new TestFilter( 42 ); final TestReplicantSessionManager sm = new TestReplicantSessionManager( channels ); final ReplicantSession session = createSession( sm ); final Session webSocketSession = session.getWebSocketSession(); when( webSocketSession.isOpen() ).thenReturn( true ); with( session, () -> assertNull( session.findSubscriptionEntry( address1 ) ) ); { EntityMessageCacheUtil.removeSessionChanges(); with( session, () -> sm.subscribe( session, address1, false, null, EntityMessageCacheUtil.getSessionChanges() ) ); final SubscriptionEntry entry1 = with( session, () -> session.findSubscriptionEntry( address1 ) ); assertNotNull( entry1 ); assertEntry( entry1, false, 0, 0, null ); assertChannelActionCount( 1 ); assertSessionChangesCount( 1 ); } { EntityMessageCacheUtil.removeSessionChanges(); with( session, () -> sm.subscribe( session, address1, false, null, EntityMessageCacheUtil.getSessionChanges() ) ); assertEntry( with( session, () -> session.getSubscriptionEntry( address1 ) ), false, 0, 0, null ); assertChannelActionCount( 0 ); assertSessionChangesCount( 0 ); } { EntityMessageCacheUtil.removeSessionChanges(); with( session, () -> sm.subscribe( session, address1, null ) ); assertEntry( with( session, () -> session.getSubscriptionEntry( address1 ) ), true, 0, 0, null ); assertChannelActionCount( 0 ); assertSessionChangesCount( 0 ); } { EntityMessageCacheUtil.removeSessionChanges(); with( session, () -> sm.subscribe( session, address2, originalFilter ) ); assertEntry( with( session, () -> session.getSubscriptionEntry( address2 ) ), true, 0, 0, originalFilter ); assertChannelActionCount( 1 ); assertSessionChangesCount( 1 ); EntityMessageCacheUtil.removeSessionChanges(); TransactionSynchronizationRegistryUtil.lookup() .putResource( ServerConstants.REQUEST_ID_KEY, ValueUtil.randomInt() ); with( session, () -> sm.subscribe( session, address2, originalFilter ) ); assertEntry( with( session, () -> session.getSubscriptionEntry( address2 ) ), true, 0, 0, originalFilter ); assertChannelActionCount( 0 ); assertSessionChangesCount( 0 ); EntityMessageCacheUtil.removeSessionChanges(); with( session, () -> sm.subscribe( session, address2, filter ) ); assertEntry( with( session, () -> session.getSubscriptionEntry( address2 ) ), true, 0, 0, filter ); assertChannelActionCount( 1 ); assertSessionChangesCount( 1 ); } { EntityMessageCacheUtil.removeSessionChanges(); with( session, () -> sm.subscribe( session, address3, originalFilter ) ); assertEntry( with( session, () -> session.getSubscriptionEntry( address3 ) ), true, 0, 0, originalFilter ); assertChannelActionCount( 1 ); assertSessionChangesCount( 1 ); EntityMessageCacheUtil.removeSessionChanges(); with( session, () -> sm.subscribe( session, address3, originalFilter ) ); assertEntry( with( session, () -> session.getSubscriptionEntry( address3 ) ), true, 0, 0, originalFilter ); assertChannelActionCount( 0 ); assertSessionChangesCount( 0 ); try { with( session, () -> sm.subscribe( session, address3, filter ) ); fail( "Successfully updated a static filter" ); } catch ( final AttemptedToUpdateStaticFilterException ignore ) { } } }
@Override public void subscribe( @Nonnull final ReplicantSession session, @Nonnull final ChannelAddress address, @Nullable final Object filter ) throws InterruptedException { final ReentrantLock lock = session.getLock(); lock.lockInterruptibly(); try { subscribe( session, address, true, filter, EntityMessageCacheUtil.getSessionChanges() ); } finally { lock.unlock(); } }
ReplicantSessionManagerImpl implements EntityMessageEndpoint, ReplicantSessionManager { @Override public void subscribe( @Nonnull final ReplicantSession session, @Nonnull final ChannelAddress address, @Nullable final Object filter ) throws InterruptedException { final ReentrantLock lock = session.getLock(); lock.lockInterruptibly(); try { subscribe( session, address, true, filter, EntityMessageCacheUtil.getSessionChanges() ); } finally { lock.unlock(); } } }
ReplicantSessionManagerImpl implements EntityMessageEndpoint, ReplicantSessionManager { @Override public void subscribe( @Nonnull final ReplicantSession session, @Nonnull final ChannelAddress address, @Nullable final Object filter ) throws InterruptedException { final ReentrantLock lock = session.getLock(); lock.lockInterruptibly(); try { subscribe( session, address, true, filter, EntityMessageCacheUtil.getSessionChanges() ); } finally { lock.unlock(); } }  }
ReplicantSessionManagerImpl implements EntityMessageEndpoint, ReplicantSessionManager { @Override public void subscribe( @Nonnull final ReplicantSession session, @Nonnull final ChannelAddress address, @Nullable final Object filter ) throws InterruptedException { final ReentrantLock lock = session.getLock(); lock.lockInterruptibly(); try { subscribe( session, address, true, filter, EntityMessageCacheUtil.getSessionChanges() ); } finally { lock.unlock(); } }  @Override boolean invalidateSession( @Nonnull final ReplicantSession session ); @Override @Nullable ReplicantSession getSession( @Nonnull final String sessionId ); @Nonnull @Override Set<String> getSessionIDs(); @Override @Nonnull ReplicantSession createSession( @Nonnull final Session webSocketSession ); @SuppressWarnings( { "WeakerAccess", "unused" } ) void pingSessions(); @SuppressWarnings( "WeakerAccess" ) void removeAllSessions(); @SuppressWarnings( "WeakerAccess" ) void removeClosedSessions(); @Override boolean saveEntityMessages( @Nullable final String sessionId,
                                     @Nullable final Integer requestId,
                                     @Nonnull final Collection<EntityMessage> messages,
                                     @Nullable final ChangeSet sessionChanges ); @Override void sendChangeMessage( @Nonnull final ReplicantSession session,
                                 @Nullable final Integer requestId,
                                 @Nullable final String etag,
                                 @Nonnull final Collection<EntityMessage> messages,
                                 @Nonnull final ChangeSet changeSet ); @Override void bulkSubscribe( @Nonnull final ReplicantSession session,
                             final int channelId,
                             @Nonnull final Collection<Integer> subChannelIds,
                             @Nullable final Object filter ); @Override void subscribe( @Nonnull final ReplicantSession session,
                         @Nonnull final ChannelAddress address,
                         @Nullable final Object filter ); @Override void unsubscribe( @Nonnull final ReplicantSession session, @Nonnull final ChannelAddress address ); @Override void bulkUnsubscribe( @Nonnull final ReplicantSession session,
                               final int channelId,
                               @Nonnull final Collection<Integer> subChannelIds ); }
ReplicantSessionManagerImpl implements EntityMessageEndpoint, ReplicantSessionManager { @Override public void subscribe( @Nonnull final ReplicantSession session, @Nonnull final ChannelAddress address, @Nullable final Object filter ) throws InterruptedException { final ReentrantLock lock = session.getLock(); lock.lockInterruptibly(); try { subscribe( session, address, true, filter, EntityMessageCacheUtil.getSessionChanges() ); } finally { lock.unlock(); } }  @Override boolean invalidateSession( @Nonnull final ReplicantSession session ); @Override @Nullable ReplicantSession getSession( @Nonnull final String sessionId ); @Nonnull @Override Set<String> getSessionIDs(); @Override @Nonnull ReplicantSession createSession( @Nonnull final Session webSocketSession ); @SuppressWarnings( { "WeakerAccess", "unused" } ) void pingSessions(); @SuppressWarnings( "WeakerAccess" ) void removeAllSessions(); @SuppressWarnings( "WeakerAccess" ) void removeClosedSessions(); @Override boolean saveEntityMessages( @Nullable final String sessionId,
                                     @Nullable final Integer requestId,
                                     @Nonnull final Collection<EntityMessage> messages,
                                     @Nullable final ChangeSet sessionChanges ); @Override void sendChangeMessage( @Nonnull final ReplicantSession session,
                                 @Nullable final Integer requestId,
                                 @Nullable final String etag,
                                 @Nonnull final Collection<EntityMessage> messages,
                                 @Nonnull final ChangeSet changeSet ); @Override void bulkSubscribe( @Nonnull final ReplicantSession session,
                             final int channelId,
                             @Nonnull final Collection<Integer> subChannelIds,
                             @Nullable final Object filter ); @Override void subscribe( @Nonnull final ReplicantSession session,
                         @Nonnull final ChannelAddress address,
                         @Nullable final Object filter ); @Override void unsubscribe( @Nonnull final ReplicantSession session, @Nonnull final ChannelAddress address ); @Override void bulkUnsubscribe( @Nonnull final ReplicantSession session,
                               final int channelId,
                               @Nonnull final Collection<Integer> subChannelIds );  }