@Override public void registerDestructionCallback(String name, Runnable callback) { SimpAttributesContextHolder.currentAttributes().registerDestructionCallback(name, callback); }
@Test public void sessionCompleted() { Runnable callback1 = Mockito.mock(Runnable.class); Runnable callback2 = Mockito.mock(Runnable.class); this.simpAttributes.registerDestructionCallback("name1", callback1); this.simpAttributes.registerDestructionCallback("name2", callback2); this.simpAttributes.sessionCompleted(); verify(callback1, times(1)).run(); verify(callback2, times(1)).run(); }
@Test public void removeDestructionCallback() { Runnable callback1 = Mockito.mock(Runnable.class); Runnable callback2 = Mockito.mock(Runnable.class); this.simpAttributes.registerDestructionCallback("name1", callback1); this.simpAttributes.registerDestructionCallback("name2", callback2); assertThat(this.simpAttributes.getAttributeNames().length, is(2)); }
@Test public void sessionCompletedIsIdempotent() { Runnable callback1 = Mockito.mock(Runnable.class); this.simpAttributes.registerDestructionCallback("name1", callback1); this.simpAttributes.sessionCompleted(); this.simpAttributes.sessionCompleted(); this.simpAttributes.sessionCompleted(); verify(callback1, times(1)).run(); }
@Test public void registerDestructionCallbackAfterSessionCompleted() { this.simpAttributes.sessionCompleted(); this.thrown.expect(IllegalStateException.class); this.thrown.expectMessage(containsString("already completed")); this.simpAttributes.registerDestructionCallback("name1", Mockito.mock(Runnable.class)); }
@Test public void registerDestructionCallback() { Runnable callback = Mockito.mock(Runnable.class); this.simpAttributes.registerDestructionCallback("name1", callback); assertThat(this.simpAttributes.getAttribute( SimpAttributes.DESTRUCTION_CALLBACK_NAME_PREFIX + "name1"), sameInstance(callback)); }
@Test public void webSocketScope() { Runnable runnable = Mockito.mock(Runnable.class); SimpAttributes simpAttributes = new SimpAttributes(this.session.getId(), this.session.getAttributes()); simpAttributes.setAttribute("name", "value"); simpAttributes.registerDestructionCallback("name", runnable); MessageChannel testChannel = new MessageChannel() { @Override public boolean send(Message<?> message) { SimpAttributes simpAttributes = SimpAttributesContextHolder.currentAttributes(); assertThat(simpAttributes.getAttribute("name"), is("value")); return true; } @Override public boolean send(Message<?> message, long timeout) { return false; } }; this.protocolHandler.afterSessionStarted(this.session, this.channel); StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.CONNECT); Message<byte[]> message = MessageBuilder.createMessage(EMPTY_PAYLOAD, headers.getMessageHeaders()); TextMessage textMessage = new TextMessage(new StompEncoder().encode(message)); this.protocolHandler.handleMessageFromClient(this.session, textMessage, testChannel); assertEquals(Collections.<WebSocketMessage<?>>emptyList(), session.getSentMessages()); this.protocolHandler.afterSessionEnded(this.session, CloseStatus.BAD_DATA, testChannel); assertEquals(Collections.<WebSocketMessage<?>>emptyList(), this.session.getSentMessages()); verify(runnable, times(1)).run(); }
@Override public void registerDestructionCallback(String name, Runnable callback) { SimpAttributesContextHolder.currentAttributes().registerDestructionCallback(name, callback); }
@Override public void registerDestructionCallback(String name, Runnable callback) { SimpAttributesContextHolder.currentAttributes().registerDestructionCallback(name, callback); }
@Override public void registerDestructionCallback(String name, Runnable callback) { SimpAttributesContextHolder.currentAttributes().registerDestructionCallback(name, callback); }