summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTarun Gupta <tarun1995gupta@gmail.com>2015-07-27 15:01:45 (GMT)
committerAlex Clayton <alex.clayton@isode.com>2016-01-12 11:31:04 (GMT)
commitb4cf2bb8d7b69d95b4a10d610ad259998d2aee5b (patch)
treeb942953236a7c712eed6ce4a5f261019691c0dca /test/com/isode/stroke/component/ComponentConnectorTest.java
parentc168fcd0c2468ec939b8d164175e9c5776a63147 (diff)
downloadstroke-b4cf2bb8d7b69d95b4a10d610ad259998d2aee5b.zip
stroke-b4cf2bb8d7b69d95b4a10d610ad259998d2aee5b.tar.bz2
Make Networks equivalent with Swiften.
Adds ProxyProviders, DomainNameResolvers and DummyConnection. License: This patch is BSD-licensed, see Documentation/Licenses/BSD-simplified.txt for details. Test-Information: Tests added for ChainedConnector, Connector and HostAddress. Test also added for ComponentConnector, which needed bits of Network. Five assertions are commented in ConnectorTest, which fails and will be updated after review. Change-Id: I8a62841eb2f9c109bc3a94865b7a003b33493e11
Diffstat (limited to 'test/com/isode/stroke/component/ComponentConnectorTest.java')
-rw-r--r--test/com/isode/stroke/component/ComponentConnectorTest.java228
1 files changed, 228 insertions, 0 deletions
diff --git a/test/com/isode/stroke/component/ComponentConnectorTest.java b/test/com/isode/stroke/component/ComponentConnectorTest.java
new file mode 100644
index 0000000..413f325
--- /dev/null
+++ b/test/com/isode/stroke/component/ComponentConnectorTest.java
@@ -0,0 +1,228 @@
+/*
+ * Copyright (c) 2010 Isode Limited.
+ * All rights reserved.
+ * See the COPYING file for more information.
+ */
+/*
+ * Copyright (c) 2015 Tarun Gupta.
+ * Licensed under the simplified BSD license.
+ * See Documentation/Licenses/BSD-simplified.txt for more information.
+ */
+
+package com.isode.stroke.component;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import org.junit.Test;
+import org.junit.Before;
+import com.isode.stroke.base.SafeByteArray;
+import com.isode.stroke.base.ByteArray;
+import com.isode.stroke.eventloop.EventLoop;
+import com.isode.stroke.eventloop.Event;
+import com.isode.stroke.signals.Slot1;
+import com.isode.stroke.network.Connector;
+import com.isode.stroke.network.HostAddress;
+import com.isode.stroke.network.Connection;
+import com.isode.stroke.network.ConnectionFactory;
+import com.isode.stroke.network.HostAddressPort;
+import com.isode.stroke.network.StaticDomainNameResolver;
+import com.isode.stroke.network.DummyTimerFactory;
+import com.isode.stroke.eventloop.DummyEventLoop;
+import com.isode.stroke.network.DomainNameAddressQuery;
+import java.util.Vector;
+
+public class ComponentConnectorTest {
+
+ private HostAddress host1;
+ private HostAddress host2;
+ private DummyEventLoop eventLoop;
+ private StaticDomainNameResolver resolver;
+ private MockConnectionFactory connectionFactory;
+ private DummyTimerFactory timerFactory;
+ private Vector<MockConnection> connections = new Vector<MockConnection>();
+
+ private class MockConnection extends Connection {
+
+ public MockConnection(final Vector<HostAddressPort> failingPorts, boolean isResponsive, EventLoop eventLoop) {
+ this.eventLoop = eventLoop;
+ this.failingPorts = failingPorts;
+ this.isResponsive = isResponsive;
+ }
+
+ public void listen() { assert(false); }
+
+ public void connect(final HostAddressPort address) {
+ hostAddressPort = address;
+ if(isResponsive) {
+ final boolean fail = failingPorts.contains(address);
+ eventLoop.postEvent(new Event.Callback() {
+ @Override
+ public void run() {
+ onConnectFinished.emit(fail);
+ }
+ });
+ }
+ }
+
+ public HostAddressPort getLocalAddress() { return new HostAddressPort(); }
+
+ public void disconnect() { assert(false); }
+
+ public void write(final SafeByteArray data) { assert(false); }
+
+ public EventLoop eventLoop;
+ public HostAddressPort hostAddressPort;
+ public Vector<HostAddressPort> failingPorts = new Vector<HostAddressPort>();
+ public boolean isResponsive;
+ };
+
+ private class MockConnectionFactory implements ConnectionFactory {
+ public MockConnectionFactory(EventLoop eventLoop) {
+ this.eventLoop = eventLoop;
+ this.isResponsive = true;
+ }
+
+ public Connection createConnection() {
+ return new MockConnection(failingPorts, isResponsive, eventLoop);
+ }
+
+ public EventLoop eventLoop;
+ public boolean isResponsive;
+ public Vector<HostAddressPort> failingPorts = new Vector<HostAddressPort>();
+ };
+
+ private ComponentConnector createConnector(final String hostname, int port) {
+ ComponentConnector connector = ComponentConnector.create(hostname, port, resolver, connectionFactory, timerFactory);
+ connector.onConnectFinished.connect(new Slot1<Connection>() {
+ @Override
+ public void call(Connection c) {
+ handleConnectorFinished(c);
+ }
+ });
+ return connector;
+ }
+
+ private void handleConnectorFinished(Connection connection) {
+ MockConnection c = (MockConnection)(connection);
+ if (connection != null) {
+ assert(c != null);
+ }
+ connections.add(c);
+ }
+
+ @Before
+ public void setUp() {
+ host1 = new HostAddress("1.1.1.1");
+ host2 = new HostAddress("2.2.2.2");
+ eventLoop = new DummyEventLoop();
+ resolver = new StaticDomainNameResolver(eventLoop);
+ connectionFactory = new MockConnectionFactory(eventLoop);
+ timerFactory = new DummyTimerFactory();
+ }
+
+ @Test
+ public void testConnect() {
+ ComponentConnector testling = createConnector("foo.com", 1234);
+ resolver.addAddress("foo.com", host1);
+
+ testling.start();
+ eventLoop.processEvents();
+
+ assertEquals(1, (connections.size()));
+ assertNotNull(connections.get(0));
+ assertEquals(new HostAddressPort(host1, 1234), (connections.get(0).hostAddressPort));
+ }
+
+ @Test
+ public void testConnect_FirstAddressHostFails() {
+ ComponentConnector testling = createConnector("foo.com", 1234);
+ resolver.addAddress("foo.com", host1);
+ resolver.addAddress("foo.com", host2);
+ connectionFactory.failingPorts.add(new HostAddressPort(host1, 1234));
+
+ testling.start();
+ eventLoop.processEvents();
+
+ assertEquals(1, (connections.size()));
+ assertNotNull(connections.get(0));
+ assertEquals(new HostAddressPort(host2, 1234), (connections.get(0).hostAddressPort));
+ }
+
+ @Test
+ public void testConnect_NoHosts() {
+ ComponentConnector testling = createConnector("foo.com", 1234);
+
+ testling.start();
+ eventLoop.processEvents();
+
+ assertEquals(1, (connections.size()));
+ assertNull(connections.get(0));
+ }
+
+ @Test
+ public void testConnect_TimeoutDuringResolve() {
+ ComponentConnector testling = createConnector("foo.com", 1234);
+
+ testling.setTimeoutMilliseconds(10);
+ resolver.setIsResponsive(false);
+
+ testling.start();
+ eventLoop.processEvents();
+ timerFactory.setTime(10);
+ eventLoop.processEvents();
+
+ assertEquals(1, (connections.size()));
+ assertNull(connections.get(0));
+ }
+
+ @Test
+ public void testConnect_TimeoutDuringConnect() {
+ ComponentConnector testling = createConnector("foo.com", 1234);
+ testling.setTimeoutMilliseconds(10);
+ resolver.addAddress("foo.com", host1);
+ connectionFactory.isResponsive = false;
+
+ testling.start();
+ eventLoop.processEvents();
+ timerFactory.setTime(10);
+ eventLoop.processEvents();
+
+ assertEquals(1, (connections.size()));
+ assertNull(connections.get(0));
+ }
+
+ @Test
+ public void testConnect_NoTimeout() {
+ ComponentConnector testling = createConnector("foo.com", 1234);
+ testling.setTimeoutMilliseconds(10);
+ resolver.addAddress("foo.com", host1);
+
+ testling.start();
+ eventLoop.processEvents();
+ timerFactory.setTime(10);
+ eventLoop.processEvents();
+
+ assertEquals(1, (connections.size()));
+ assertNotNull(connections.get(0));
+ }
+
+ @Test
+ public void testStop_Timeout() {
+ ComponentConnector testling = createConnector("foo.com", 1234);
+ testling.setTimeoutMilliseconds(10);
+ resolver.addAddress("foo.com", host1);
+
+ testling.start();
+ testling.stop();
+
+ eventLoop.processEvents();
+ timerFactory.setTime(10);
+ eventLoop.processEvents();
+
+ assertEquals(1, (connections.size()));
+ assertNull(connections.get(0));
+ }
+} \ No newline at end of file