|
2 | 2 |
|
3 | 3 | import junit.framework.TestCase; |
4 | 4 |
|
| 5 | +import net.servicestack.client.sse.GetEventSubscribers; |
5 | 6 | import net.servicestack.client.sse.ServerEventConnect; |
6 | 7 | import net.servicestack.client.sse.ServerEventJoin; |
7 | 8 | import net.servicestack.client.sse.ServerEventLeave; |
|
12 | 13 | import net.servicestack.func.Func; |
13 | 14 |
|
14 | 15 | import java.util.ArrayList; |
| 16 | +import java.util.HashMap; |
15 | 17 | import java.util.List; |
16 | 18 | import java.util.concurrent.CountDownLatch; |
17 | 19 | import java.util.concurrent.TimeUnit; |
|
20 | 22 | import static chat.chatdtos.ChatMessage; |
21 | 23 | import static chat.chatdtos.PostChatToChannel; |
22 | 24 | import static chat.chatdtos.ResetServerEvents; |
| 25 | +import static org.junit.Assert.assertArrayEquals; |
23 | 26 |
|
24 | 27 | /** |
25 | 28 | * Created by mythz on 2/10/2017. |
@@ -493,6 +496,10 @@ public void test_Does_set_properties_on_global_receiver() throws Exception { |
493 | 496 | .setId(1) |
494 | 497 | .setName("Foo")); |
495 | 498 |
|
| 499 | + while (msgs1.size() < 1){ |
| 500 | + Thread.sleep(100); |
| 501 | + } |
| 502 | + |
496 | 503 | SetterType foo = TestGlobalReceiver.AnyNamedSetterReceived; |
497 | 504 | assertNotNull(foo); |
498 | 505 | assertEquals(1, foo.getId().intValue()); |
@@ -653,4 +660,248 @@ public void test_Does_receive_messages_on_to_clients_subscribed_on_multiple_chan |
653 | 660 | assertEquals(8, msgsABCD.size()); |
654 | 661 | } |
655 | 662 | } |
| 663 | + |
| 664 | + public void test_Does_receive_all_join_and_leave_messages() throws Exception { |
| 665 | + List<ServerEventJoin> joinA = new ArrayList<>(); |
| 666 | + List<ServerEventJoin> joinB = new ArrayList<>(); |
| 667 | + List<ServerEventJoin> joinAB = new ArrayList<>(); |
| 668 | + |
| 669 | + List<ServerEventLeave> leaveA = new ArrayList<>(); |
| 670 | + List<ServerEventLeave> leaveB = new ArrayList<>(); |
| 671 | + List<ServerEventLeave> leaveAB = new ArrayList<>(); |
| 672 | + |
| 673 | + try(ServerEventsClient clientA = new ServerEventsClient("http://chat.servicestack.net", "A") |
| 674 | + .setOnCommand(e -> { |
| 675 | + if (e instanceof ServerEventJoin){ |
| 676 | + joinA.add((ServerEventJoin)e); |
| 677 | + } else if (e instanceof ServerEventLeave){ |
| 678 | + leaveA.add((ServerEventLeave)e); |
| 679 | + } |
| 680 | + }); |
| 681 | + ServerEventsClient clientB = new ServerEventsClient("http://chat.servicestack.net", "B") |
| 682 | + .setOnCommand(e -> { |
| 683 | + if (e instanceof ServerEventJoin){ |
| 684 | + joinB.add((ServerEventJoin)e); |
| 685 | + } else if (e instanceof ServerEventLeave){ |
| 686 | + leaveB.add((ServerEventLeave)e); |
| 687 | + } |
| 688 | + }); |
| 689 | + ServerEventsClient clientAB = new ServerEventsClient("http://chat.servicestack.net", "A", "B") |
| 690 | + .setOnCommand(e -> { |
| 691 | + if (e instanceof ServerEventJoin){ |
| 692 | + joinAB.add((ServerEventJoin)e); |
| 693 | + } else if (e instanceof ServerEventLeave){ |
| 694 | + leaveAB.add((ServerEventLeave)e); |
| 695 | + } |
| 696 | + }); |
| 697 | + ){ |
| 698 | + |
| 699 | + clientA.start().waitTillConnected(); |
| 700 | + clientB.start().waitTillConnected(); |
| 701 | + clientAB.start().waitTillConnected(); |
| 702 | + |
| 703 | + while (joinA.size() < 2 || joinB.size() < 2 || joinAB.size() < 2){ |
| 704 | + Thread.sleep(100); |
| 705 | + } |
| 706 | + |
| 707 | + assertEquals(2, joinA.size()); //A + [(A) B] |
| 708 | + assertEquals(2, joinB.size()); //B + [A (B)] |
| 709 | + assertEquals(2, joinAB.size()); //[(A) B] + [A (B)] |
| 710 | + |
| 711 | + List<ServerEventUser> channelAsubscribers = clientA.getChannelSubscribers(); |
| 712 | + assertEquals(2, channelAsubscribers.size()); |
| 713 | + |
| 714 | + List<ServerEventUser> channelBsubscribers = clientB.getChannelSubscribers(); |
| 715 | + assertEquals(2, channelBsubscribers.size()); |
| 716 | + |
| 717 | + List<ServerEventUser> channelABsubscribers = clientAB.getChannelSubscribers(); |
| 718 | + assertEquals(3, channelABsubscribers.size()); |
| 719 | + |
| 720 | + ArrayList<HashMap<String,String>> usersA = clientA.getServiceClient().get(new GetEventSubscribers() |
| 721 | + .setChannels(Func.toList("A"))); |
| 722 | + ArrayList<HashMap<String,String>> usersB = clientA.getServiceClient().get(new GetEventSubscribers() |
| 723 | + .setChannels(Func.toList("B"))); |
| 724 | + ArrayList<HashMap<String,String>> usersAB = clientA.getServiceClient().get(new GetEventSubscribers() |
| 725 | + .setChannels(Func.toList("A", "B"))); |
| 726 | + |
| 727 | + assertEquals(2, usersA.size()); |
| 728 | + assertEquals(2, usersB.size()); |
| 729 | + assertEquals(3, usersAB.size()); |
| 730 | + |
| 731 | + clientAB.stop(); |
| 732 | + |
| 733 | + Thread.sleep(100); |
| 734 | + |
| 735 | + clientB.stop(); |
| 736 | + clientA.stop(); |
| 737 | + |
| 738 | + Thread.sleep(100); |
| 739 | + |
| 740 | + assertEquals(1, leaveA.size()); |
| 741 | + assertEquals(1, leaveB.size()); |
| 742 | + assertEquals(0, leaveAB.size()); |
| 743 | + } |
| 744 | + } |
| 745 | + |
| 746 | + public void test_MultiChannel_Does_receive_all_join_and_leave_messages() throws Exception { |
| 747 | + List<ServerEventJoin> joinA = new ArrayList<>(); |
| 748 | + List<ServerEventJoin> joinB = new ArrayList<>(); |
| 749 | + List<ServerEventJoin> joinAB = new ArrayList<>(); |
| 750 | + |
| 751 | + List<ServerEventLeave> leaveA = new ArrayList<>(); |
| 752 | + List<ServerEventLeave> leaveB = new ArrayList<>(); |
| 753 | + List<ServerEventLeave> leaveAB = new ArrayList<>(); |
| 754 | + |
| 755 | + try( |
| 756 | + ServerEventsClient clientAB = new ServerEventsClient("http://chat.servicestack.net", "A", "B") |
| 757 | + .setOnCommand(e -> { |
| 758 | + if (e instanceof ServerEventJoin){ |
| 759 | + joinAB.add((ServerEventJoin)e); |
| 760 | + } else if (e instanceof ServerEventLeave){ |
| 761 | + leaveAB.add((ServerEventLeave)e); |
| 762 | + } |
| 763 | + }); |
| 764 | + ServerEventsClient clientA = new ServerEventsClient("http://chat.servicestack.net", "A") |
| 765 | + .setOnCommand(e -> { |
| 766 | + if (e instanceof ServerEventJoin){ |
| 767 | + joinA.add((ServerEventJoin)e); |
| 768 | + } else if (e instanceof ServerEventLeave){ |
| 769 | + leaveA.add((ServerEventLeave)e); |
| 770 | + } |
| 771 | + }); |
| 772 | + ServerEventsClient clientB = new ServerEventsClient("http://chat.servicestack.net", "B") |
| 773 | + .setOnCommand(e -> { |
| 774 | + if (e instanceof ServerEventJoin){ |
| 775 | + joinB.add((ServerEventJoin)e); |
| 776 | + } else if (e instanceof ServerEventLeave){ |
| 777 | + leaveB.add((ServerEventLeave)e); |
| 778 | + } |
| 779 | + }); |
| 780 | + ) { |
| 781 | + |
| 782 | + clientAB.start().waitTillConnected(); |
| 783 | + clientA.start().waitTillConnected(); |
| 784 | + clientB.start().waitTillConnected(); |
| 785 | + |
| 786 | + while (joinAB.size() < 4 //[(A) B] + [A (B)] + A + B |
| 787 | + || joinA.size() < 1 || joinB.size() < 1) { |
| 788 | + Thread.sleep(100); |
| 789 | + } |
| 790 | + |
| 791 | + clientA.stop(); |
| 792 | + clientB.stop(); |
| 793 | + |
| 794 | + Thread.sleep(100); |
| 795 | + |
| 796 | + clientAB.stop(); |
| 797 | + |
| 798 | + assertEquals(2, leaveAB.size()); |
| 799 | + assertEquals(0, leaveA.size()); |
| 800 | + assertEquals(0, leaveB.size()); |
| 801 | + } |
| 802 | + } |
| 803 | + |
| 804 | + public void test_Can_subscribe_to_channels_whilst_connected() throws Exception { |
| 805 | + List<ServerEventMessage> msgs1 = new ArrayList<>(); |
| 806 | + List<ServerEventMessage> msgs2 = new ArrayList<>(); |
| 807 | + |
| 808 | + try(ServerEventsClient client1 = new ServerEventsClient("http://chat.servicestack.net", "A") |
| 809 | + .setOnMessage(msgs1::add) |
| 810 | + .start(); |
| 811 | + ServerEventsClient client2 = new ServerEventsClient("http://chat.servicestack.net", "B") |
| 812 | + .setOnMessage(msgs2::add) |
| 813 | + .start()) { |
| 814 | + |
| 815 | + client1.waitTillConnected(); |
| 816 | + client2.waitTillConnected(); |
| 817 | + |
| 818 | + assertArrayEquals(new String[]{"A"}, client1.getChannels()); |
| 819 | + |
| 820 | + postChat(client2, "#1 hello to B", "B"); |
| 821 | + |
| 822 | + Thread.sleep(500); |
| 823 | + |
| 824 | + assertEquals(0, msgs1.size()); |
| 825 | + assertEquals(1, msgs2.size()); |
| 826 | + |
| 827 | + client1.subscribeToChannels("B"); |
| 828 | + |
| 829 | + Thread.sleep(500); |
| 830 | + |
| 831 | + postChat(client2, "#2 hello to B", "B"); |
| 832 | + postChat(client2, "#3 hello to C", "C"); |
| 833 | + Thread.sleep(500); |
| 834 | + |
| 835 | + assertArrayEquals(new String[]{"A", "B"}, client1.getChannels()); |
| 836 | + assertArrayEquals(new String[]{"B"}, client2.getChannels()); |
| 837 | + |
| 838 | + assertTrue(client1.getEventStreamUri().endsWith("?channels=A,B")); |
| 839 | + assertTrue(client2.getEventStreamUri().endsWith("?channels=B")); |
| 840 | + |
| 841 | + client1.subscribeToChannels("C"); |
| 842 | + client2.subscribeToChannels("C"); |
| 843 | + Thread.sleep(500); |
| 844 | + |
| 845 | + postChat(client2, "#4 hello to C", "C"); |
| 846 | + Thread.sleep(500); |
| 847 | + |
| 848 | + assertArrayEquals(new String[]{"A", "B", "C"}, client1.getChannels()); |
| 849 | + assertArrayEquals(new String[]{"B", "C"}, client2.getChannels()); |
| 850 | + |
| 851 | + assertTrue(client1.getEventStreamUri().endsWith("?channels=A,B,C")); |
| 852 | + assertTrue(client2.getEventStreamUri().endsWith("?channels=B,C")); |
| 853 | + } |
| 854 | + } |
| 855 | + |
| 856 | + public void test_Can_unsubscribe_from_channels_whilst_connected() throws Exception { |
| 857 | + List<ServerEventMessage> msgs1 = new ArrayList<>(); |
| 858 | + List<ServerEventMessage> msgs2 = new ArrayList<>(); |
| 859 | + |
| 860 | + try(ServerEventsClient client1 = new ServerEventsClient("http://chat.servicestack.net", "A","B","C") |
| 861 | + .setOnMessage(msgs1::add) |
| 862 | + .start() |
| 863 | + .waitTillConnected(); |
| 864 | + ServerEventsClient client2 = new ServerEventsClient("http://chat.servicestack.net", "B","C") |
| 865 | + .setOnMessage(msgs2::add) |
| 866 | + .start() |
| 867 | + .waitTillConnected()) { |
| 868 | + |
| 869 | + assertArrayEquals(new String[]{"A","B","C"}, client1.getChannels()); |
| 870 | + |
| 871 | + postChat(client2, "#1 hello to B", "B"); |
| 872 | + Thread.sleep(500); |
| 873 | + |
| 874 | + assertEquals(1, msgs1.size()); |
| 875 | + assertEquals(1, msgs2.size()); |
| 876 | + |
| 877 | + client1.unSubscribeFromChannels("B"); |
| 878 | + Thread.sleep(500); |
| 879 | + |
| 880 | + postChat(client2, "#2 hello to B", "B"); |
| 881 | + postChat(client2, "#3 hello to C", "C"); |
| 882 | + Thread.sleep(500); |
| 883 | + |
| 884 | + assertEquals(2, msgs1.size()); |
| 885 | + assertEquals(3, msgs2.size()); |
| 886 | + |
| 887 | + assertArrayEquals(new String[]{"A", "C"}, client1.getChannels()); |
| 888 | + assertArrayEquals(new String[]{"B", "C"}, client2.getChannels()); |
| 889 | + |
| 890 | + assertTrue(client1.getEventStreamUri().endsWith("?channels=A,C")); |
| 891 | + assertTrue(client2.getEventStreamUri().endsWith("?channels=B,C")); |
| 892 | + |
| 893 | + client1.unSubscribeFromChannels("C"); |
| 894 | + client2.unSubscribeFromChannels("C"); |
| 895 | + Thread.sleep(500); |
| 896 | + |
| 897 | + postChat(client2, "#4 hello to C", "C"); |
| 898 | + Thread.sleep(500); |
| 899 | + |
| 900 | + assertArrayEquals(new String[]{"A"}, client1.getChannels()); |
| 901 | + assertArrayEquals(new String[]{"B"}, client2.getChannels()); |
| 902 | + |
| 903 | + assertTrue(client1.getEventStreamUri().endsWith("?channels=A")); |
| 904 | + assertTrue(client2.getEventStreamUri().endsWith("?channels=B")); |
| 905 | + } |
| 906 | + } |
656 | 907 | } |
0 commit comments