| 1 | + | /* |
| 2 | + | * ClusterConnectionSelectorTest.java |
| 3 | + | * |
| 4 | + | * Tigase Jabber/XMPP Server |
| 5 | + | * Copyright (C) 2004-2015 "Tigase, Inc." <office@tigase.com> |
| 6 | + | * |
| 7 | + | * This program is free software: you can redistribute it and/or modify |
| 8 | + | * it under the terms of the GNU Affero General Public License as published by |
| 9 | + | * the Free Software Foundation, either version 3 of the License, |
| 10 | + | * or (at your option) any later version. |
| 11 | + | * |
| 12 | + | * This program is distributed in the hope that it will be useful, |
| 13 | + | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + | * GNU Affero General Public License for more details. |
| 16 | + | * |
| 17 | + | * You should have received a copy of the GNU Affero General Public License |
| 18 | + | * along with this program. Look for COPYING file in the top folder. |
| 19 | + | * If not, see http://www.gnu.org/licenses/. |
| 20 | + | * |
| 21 | + | */ |
| 22 | + | package tigase.cluster; |
| 23 | + | |
| 24 | + | import java.util.Arrays; |
| 25 | + | import java.util.HashMap; |
| 26 | + | import java.util.HashSet; |
| 27 | + | import java.util.Map; |
| 28 | + | import java.util.Set; |
| 29 | + | import junit.framework.TestCase; |
| 30 | + | import org.junit.Test; |
| 31 | + | import static tigase.cluster.ClusterConnectionSelector.CLUSTER_SYS_CONNECTIONS_PER_NODE_PROP_KEY; |
| 32 | + | import tigase.cluster.api.ClusterConnectionHandler; |
| 33 | + | import tigase.server.Packet; |
| 34 | + | import tigase.server.Priority; |
| 35 | + | import tigase.xml.Element; |
| 36 | + | import tigase.xmpp.XMPPIOService; |
| 37 | + | |
| 38 | + | /** |
| 39 | + | * |
| 40 | + | * @author andrzej |
| 41 | + | */ |
| 42 | + | public class ClusterConnectionSelectorTest extends TestCase { |
| 43 | + | |
| 44 | + | @Test |
| 45 | + | public void testSelectConnection() throws Exception { |
| 46 | + | ClusterConnection conn = new ClusterConnection("test"); |
| 47 | + | ClusterConnectionSelector selector = new ClusterConnectionSelector(); |
| 48 | + | selector.setClusterConnectionHandler(new ClusterConnectionHandler() { |
| 49 | + | |
| 50 | + | @Override |
| 51 | + | public int hashCodeForPacket(Packet packet) { |
| 52 | + | return packet.getStanzaFrom().hashCode(); |
| 53 | + | } |
| 54 | + | }); |
| 55 | + | |
| 56 | + | Element el = new Element("iq", new String[] { "from" }, new String[] { "test1" }); |
| 57 | + | Packet p = Packet.packetInstance(el); |
| 58 | + | assertNull(selector.selectConnection(p, conn)); |
| 59 | + | |
| 60 | + | XMPPIOService<Object> serv1 = new XMPPIOService<Object>(); |
| 61 | + | conn.addConn(serv1); |
| 62 | + | assertEquals(serv1, selector.selectConnection(p, conn)); |
| 63 | + | |
| 64 | + | p.setPriority(Priority.SYSTEM); |
| 65 | + | assertEquals(serv1, selector.selectConnection(p, conn)); |
| 66 | + | |
| 67 | + | p.setPriority(null); |
| 68 | + | XMPPIOService<Object> serv2 = new XMPPIOService<Object>(); |
| 69 | + | conn.addConn(serv2); |
| 70 | + | assertEquals(2, conn.size()); |
| 71 | + | assertEquals(serv2, selector.selectConnection(p, conn)); |
| 72 | + | |
| 73 | + | p.setPriority(Priority.SYSTEM); |
| 74 | + | assertEquals(serv1, selector.selectConnection(p, conn)); |
| 75 | + | |
| 76 | + | p.setPriority(null); |
| 77 | + | XMPPIOService<Object> serv3 = new XMPPIOService<Object>(); |
| 78 | + | conn.addConn(serv3); |
| 79 | + | assertEquals(3, conn.size()); |
| 80 | + | assertNotSame(serv1, selector.selectConnection(p, conn)); |
| 81 | + | |
| 82 | + | p.setPriority(Priority.SYSTEM); |
| 83 | + | assertEquals(serv1, selector.selectConnection(p, conn)); |
| 84 | + | |
| 85 | + | el = new Element("iq", new String[] { "from" }, new String[] { "test2" }); |
| 86 | + | p = Packet.packetInstance(el); |
| 87 | + | assertEquals(3, conn.size()); |
| 88 | + | assertNotSame(serv1, selector.selectConnection(p, conn)); |
| 89 | + | |
| 90 | + | el = new Element("iq", new String[] { "from" }, new String[] { "test3" }); |
| 91 | + | p = Packet.packetInstance(el); |
| 92 | + | assertEquals(3, conn.size()); |
| 93 | + | assertNotSame(serv1, selector.selectConnection(p, conn)); |
| 94 | + | |
| 95 | + | el = new Element("iq", new String[] { "from" }, new String[] { "test4" }); |
| 96 | + | p = Packet.packetInstance(el); |
| 97 | + | assertEquals(3, conn.size()); |
| 98 | + | assertNotSame(serv1, selector.selectConnection(p, conn)); |
| 99 | + | } |
| 100 | + | |
| 101 | + | @Test |
| 102 | + | public void testSelectConnectionFor2() throws Exception { |
| 103 | + | ClusterConnection conn = new ClusterConnection("test"); |
| 104 | + | ClusterConnectionSelector selector = new ClusterConnectionSelector(); |
| 105 | + | selector.setClusterConnectionHandler(new ClusterConnectionHandler() { |
| 106 | + | |
| 107 | + | @Override |
| 108 | + | public int hashCodeForPacket(Packet packet) { |
| 109 | + | return packet.getStanzaFrom().hashCode(); |
| 110 | + | } |
| 111 | + | }); |
| 112 | + | Map<String,Object> props = new HashMap<>(); |
| 113 | + | props.put(CLUSTER_SYS_CONNECTIONS_PER_NODE_PROP_KEY, 2); |
| 114 | + | selector.setProperties(props); |
| 115 | + | |
| 116 | + | Element el = new Element("iq", new String[] { "from" }, new String[] { "test1" }); |
| 117 | + | Packet p = Packet.packetInstance(el); |
| 118 | + | assertNull(selector.selectConnection(p, conn)); |
| 119 | + | |
| 120 | + | XMPPIOService<Object> serv1 = new XMPPIOService<Object>(); |
| 121 | + | conn.addConn(serv1); |
| 122 | + | assertEquals(serv1, selector.selectConnection(p, conn)); |
| 123 | + | |
| 124 | + | p.setPriority(Priority.SYSTEM); |
| 125 | + | assertEquals(serv1, selector.selectConnection(p, conn)); |
| 126 | + | |
| 127 | + | p.setPriority(null); |
| 128 | + | XMPPIOService<Object> serv2 = new XMPPIOService<Object>(); |
| 129 | + | conn.addConn(serv2); |
| 130 | + | Set<XMPPIOService<Object>> sysServs = new HashSet<>(Arrays.asList(serv1, serv2)); |
| 131 | + | assertEquals(2, conn.size()); |
| 132 | + | assertTrue(sysServs.contains(selector.selectConnection(p, conn))); |
| 133 | + | |
| 134 | + | p.setPriority(Priority.SYSTEM); |
| 135 | + | assertTrue(sysServs.contains(selector.selectConnection(p, conn))); |
| 136 | + | |
| 137 | + | p.setPriority(null); |
| 138 | + | XMPPIOService<Object> serv3 = new XMPPIOService<Object>(); |
| 139 | + | conn.addConn(serv3); |
| 140 | + | assertEquals(3, conn.size()); |
| 141 | + | assertSame(serv3, selector.selectConnection(p, conn)); |
| 142 | + | |
| 143 | + | p.setPriority(Priority.SYSTEM); |
| 144 | + | assertTrue(sysServs.contains(selector.selectConnection(p, conn))); |
| 145 | + | |
| 146 | + | el = new Element("iq", new String[] { "from" }, new String[] { "test2" }); |
| 147 | + | p = Packet.packetInstance(el); |
| 148 | + | assertEquals(3, conn.size()); |
| 149 | + | assertSame(serv3, selector.selectConnection(p, conn)); |
| 150 | + | |
| 151 | + | el = new Element("iq", new String[] { "from" }, new String[] { "test3" }); |
| 152 | + | p = Packet.packetInstance(el); |
| 153 | + | assertEquals(3, conn.size()); |
| 154 | + | assertSame(serv3, selector.selectConnection(p, conn)); |
| 155 | + | |
| 156 | + | el = new Element("iq", new String[] { "from" }, new String[] { "test4" }); |
| 157 | + | p = Packet.packetInstance(el); |
| 158 | + | assertEquals(3, conn.size()); |
| 159 | + | assertSame(serv3, selector.selectConnection(p, conn)); |
| 160 | + | } |
| 161 | + | |
| 162 | + | } |
| 163 | + | |