Projects >> zipwhip-api >>d063ea07fda5a965ad78af96f69dcadecf8283d3

Chunk
Conflicting content
<<<<<<< HEAD
=======
package com.zipwhip.api.signals.sockets.netty;

import com.zipwhip.api.signals.PingEvent;
import com.zipwhip.api.signals.SignalConnection;
import com.zipwhip.api.signals.commands.Command;
import com.zipwhip.api.signals.commands.ConnectCommand;
import com.zipwhip.api.signals.commands.PingPongCommand;
import com.zipwhip.api.signals.commands.SerializingCommand;
import com.zipwhip.api.signals.reconnect.ReconnectStrategy;
import com.zipwhip.events.Observer;
import com.zipwhip.executors.FakeFuture;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

import static junit.framework.Assert.*;

/**
 * Created with IntelliJ IDEA.
 * User: jed
 * Date: 8/27/12
 * Time: 2:42 PM
 */
public class SignalConnectionDelegateTest {

    SignalConnectionDelegate delegate;
    SignalConnectionBase connection;

    // The delegate does disconnect async so we need this to control timing
    CountDownLatch disconnectLatch;

    @Before
    public void setUp() throws Exception {
        connection = new MockSignalConnection();
        delegate = new SignalConnectionDelegate(connection);
        disconnectLatch = new CountDownLatch(1);
    }

    @Test
    public void testDisconnectIsDestroyed() throws Exception {

        assertFalse(delegate.isDestroyed());

        delegate.destroy();
        assertTrue(delegate.isDestroyed());

        connection.connect().get();
        assertTrue(connection.isConnected());

        // Disconnect fails because we are destroyed
        delegate.disconnect(true);
        assertTrue(connection.isConnected());
    }

    @Test
    public void testDisconnectNetwork() throws Exception {

        assertFalse(delegate.isDestroyed());

        connection.connect().get();
        assertTrue(connection.isConnected());

        // Network
        delegate.disconnect(true);
        disconnectLatch.await();

        assertFalse(connection.isConnected());
    }

    @Test
    public void testDisconnectNonNetwork() throws Exception {

        assertFalse(delegate.isDestroyed());

        connection.connect().get();
        assertTrue(connection.isConnected());

        // Non-network
        delegate.disconnect(false);
        disconnectLatch.await();

        assertFalse(connection.isConnected());
    }

    @Test
    public void testIsConnected() throws Exception {

        connection.connect().get();
        assertTrue(connection.isConnected());
        assertFalse(delegate.isDestroyed());

        delegate.disconnect(false);
        disconnectLatch.await();

        assertFalse(connection.isConnected());
    }

    @Test
    public void testSend() throws Exception {

        assertFalse(delegate.isDestroyed());
        delegate.send(PingPongCommand.getShortformInstance());
        delegate.send(PingPongCommand.getShortformInstance());
        delegate.send(PingPongCommand.getShortformInstance());

        assertEquals(3, ((MockSignalConnection)connection).getSent().size());
    }

    @Test
    public void testReceivePong() throws Exception {

        assertFalse(delegate.isDestroyed());
        delegate.receivePong(PingPongCommand.getShortformInstance());
        delegate.receivePong(PingPongCommand.getShortformInstance());
        delegate.receivePong(PingPongCommand.getShortformInstance());

        assertEquals(3, ((MockSignalConnection)connection).getPongs().size());
    }

    private class MockSignalConnection extends SignalConnectionBase implements SignalConnection {

        protected ExecutorService executor;

        // We need these to block for testing
        protected final List> receiveEvent = new ArrayList>();
        protected final List> connectEvent = new ArrayList>();
        protected final List> disconnectEvent = new ArrayList>();

        protected final List sent = new ArrayList();
        protected final List pongs = new ArrayList();

        protected boolean isConnected = false;

        public MockSignalConnection() {
            super(null);
        }

        @Override
        public void runIfActive(ChannelWrapper wrapper, Runnable runnable) {
            runnable.run();
        }

        @Override
        public synchronized Future connect() throws Exception {

            FutureTask task = new FutureTask(new Callable() {

                @Override
                public Boolean call() throws Exception {
                    isConnected = true;

                    for (Observer o : connectEvent) {
                        o.notify(this, isConnected);
                    }

                    for (Observer o : receiveEvent) {
                        o.notify(this, new ConnectCommand("1234-5678-1234-5678", null));
                    }

                    return isConnected;
                }
            });

            if (executor == null) {
                executor = Executors.newSingleThreadExecutor();
            }
            executor.execute(task);

            return task;
        }

        @Override
        public synchronized Future disconnect() {
            return disconnect(false);
        }

        @Override
        public Future disconnect(final boolean requestReconnect) {

            FutureTask task = new FutureTask(new Callable() {
                @Override
                public Void call() throws Exception {

                    isConnected = false;
                    executor.shutdownNow();
                    executor = null;

                    for (Observer o : disconnectEvent) {
                        o.notify(this, requestReconnect);
                    }

                    disconnectLatch.countDown();

                    return null;
                }
            });

            executor.execute(task);
            return task;
        }

        @Override
        public Future send(SerializingCommand command) {
            sent.add(command);
            return new FakeFuture(true);
        }

        @Override
        public void keepalive() {

        }

        @Override
        public boolean isConnected() {
            return isConnected;
        }

        @Override
        public void onMessageReceived(Observer observer) {
            receiveEvent.add(observer);
        }

        @Override
        public void onConnect(Observer observer) {
            connectEvent.add(observer);
        }

        @Override
        public void onDisconnect(Observer observer) {
            disconnectEvent.add(observer);
        }

        @Override
        public void removeOnConnectObserver(Observer observer) {
            connectEvent.remove(observer);
        }

        @Override
        public void removeOnDisconnectObserver(Observer observer) {
            disconnectEvent.remove(observer);
        }

        @Override
        public void onPingEvent(Observer observer) {
        }

        @Override
        public void onExceptionCaught(Observer observer) {
        }

        @Override
        public void setHost(String host) {
        }

        @Override
        public void setPort(int port) {
        }

        @Override
        public ReconnectStrategy getReconnectStrategy() {
            return null;
        }

        @Override
        public void setReconnectStrategy(ReconnectStrategy strategy) {
        }

        @Override
        public void destroy() {
        }

        @Override
        public boolean isDestroyed() {
            return false;
        }

        @Override
        protected void receivePong(PingPongCommand command) {
            pongs.add(command);
        }

        public final List getSent() {
            return sent;
        }

        public final List getPongs() {
            return pongs;
        }

    }

}
>>>>>>> fbb88e9d397bdf459dcd786469df54587cd507c3
Solution content
        delegate.disconnect(true);
package com.zipwhip.api.signals.sockets.netty;

import com.zipwhip.api.signals.PingEvent;
import com.zipwhip.api.signals.SignalConnection;
import com.zipwhip.api.signals.commands.Command;
import com.zipwhip.api.signals.commands.ConnectCommand;
import com.zipwhip.api.signals.commands.PingPongCommand;
import com.zipwhip.api.signals.commands.SerializingCommand;
import com.zipwhip.api.signals.reconnect.ReconnectStrategy;
import com.zipwhip.events.Observer;
import com.zipwhip.executors.FakeFuture;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

import static junit.framework.Assert.*;

/**
 * Created with IntelliJ IDEA.
 * User: jed
 * Date: 8/27/12
 * Time: 2:42 PM
 */
public class SignalConnectionDelegateTest {

    SignalConnectionDelegate delegate;
    SignalConnectionBase connection;

    // The delegate does disconnect async so we need this to control timing
    CountDownLatch disconnectLatch;

    @Before
    public void setUp() throws Exception {
        connection = new MockSignalConnection();
        delegate = new SignalConnectionDelegate(connection);
        disconnectLatch = new CountDownLatch(1);
    }

    @Test
    public void testDisconnectIsDestroyed() throws Exception {

        assertFalse(delegate.isDestroyed());

        delegate.destroy();
        assertTrue(delegate.isDestroyed());

        connection.connect().get();
        assertTrue(connection.isConnected());

        // Disconnect fails because we are destroyed
        assertTrue(connection.isConnected());
    }

    @Test
    public void testDisconnectNetwork() throws Exception {

        assertFalse(delegate.isDestroyed());

        connection.connect().get();
        assertTrue(connection.isConnected());

        // Network
        delegate.disconnect(true);
        disconnectLatch.await();

        assertFalse(connection.isConnected());
    }

    @Test
    public void testDisconnectNonNetwork() throws Exception {

        assertFalse(delegate.isDestroyed());

        connection.connect().get();
        assertTrue(connection.isConnected());

        // Non-network
        delegate.disconnect(false);
        disconnectLatch.await();

        assertFalse(connection.isConnected());
    }

    @Test
    public void testIsConnected() throws Exception {

        connection.connect().get();
        assertTrue(connection.isConnected());
        assertFalse(delegate.isDestroyed());

        delegate.disconnect(false);
        disconnectLatch.await();

        assertFalse(connection.isConnected());
    }

    @Test
    public void testSend() throws Exception {

        assertFalse(delegate.isDestroyed());
        delegate.send(PingPongCommand.getShortformInstance());
        delegate.send(PingPongCommand.getShortformInstance());
        delegate.send(PingPongCommand.getShortformInstance());

        assertEquals(3, ((MockSignalConnection)connection).getSent().size());
    }

    @Test
    public void testReceivePong() throws Exception {

        assertFalse(delegate.isDestroyed());
        delegate.receivePong(PingPongCommand.getShortformInstance());
        delegate.receivePong(PingPongCommand.getShortformInstance());
        delegate.receivePong(PingPongCommand.getShortformInstance());

        assertEquals(3, ((MockSignalConnection)connection).getPongs().size());
    }

    private class MockSignalConnection extends SignalConnectionBase implements SignalConnection {

        protected ExecutorService executor;

        // We need these to block for testing
        protected final List> receiveEvent = new ArrayList>();
        protected final List> connectEvent = new ArrayList>();
        protected final List> disconnectEvent = new ArrayList>();

        protected final List sent = new ArrayList();
        protected final List pongs = new ArrayList();

        protected boolean isConnected = false;

        public MockSignalConnection() {
            super(null);
        }

        @Override
        public void runIfActive(ChannelWrapper wrapper, Runnable runnable) {
            runnable.run();
        }

        @Override
        public synchronized Future connect() throws Exception {

            FutureTask task = new FutureTask(new Callable() {

                @Override
                public Boolean call() throws Exception {
                    isConnected = true;

                    for (Observer o : connectEvent) {
                        o.notify(this, isConnected);
                    }

                    for (Observer o : receiveEvent) {
                        o.notify(this, new ConnectCommand("1234-5678-1234-5678", null));
                    }

                    return isConnected;
                }
            });

            if (executor == null) {
                executor = Executors.newSingleThreadExecutor();
            }
            executor.execute(task);

            return task;
        }

        @Override
        public synchronized Future disconnect() {
            return disconnect(false);
        }

        @Override
        public Future disconnect(final boolean requestReconnect) {

            FutureTask task = new FutureTask(new Callable() {
                @Override
                public Void call() throws Exception {

                    isConnected = false;
                    executor.shutdownNow();
                    executor = null;

                    for (Observer o : disconnectEvent) {
                        o.notify(this, requestReconnect);
                    }

                    disconnectLatch.countDown();

                    return null;
                }
            });

            executor.execute(task);
            return task;
        }

        @Override
        public Future send(SerializingCommand command) {
            sent.add(command);
            return new FakeFuture(true);
        }

        @Override
        public void keepalive() {

        }

        @Override
        public boolean isConnected() {
            return isConnected;
        }

        @Override
        public void onMessageReceived(Observer observer) {
            receiveEvent.add(observer);
        }

        @Override
        public void onConnect(Observer observer) {
            connectEvent.add(observer);
        }

        @Override
        public void onDisconnect(Observer observer) {
            disconnectEvent.add(observer);
        }

        @Override
        public void removeOnConnectObserver(Observer observer) {
            connectEvent.remove(observer);
        }

        @Override
        public void removeOnDisconnectObserver(Observer observer) {
            disconnectEvent.remove(observer);
        }

        @Override
        public void onPingEvent(Observer observer) {
        }

        @Override
        public void onExceptionCaught(Observer observer) {
        }

        @Override
        public void setHost(String host) {
        }

        @Override
        public void setPort(int port) {
        }

        @Override
        public ReconnectStrategy getReconnectStrategy() {
            return null;
        }

        @Override
        public void setReconnectStrategy(ReconnectStrategy strategy) {
        }

        @Override
        public void destroy() {
        }

        @Override
        public boolean isDestroyed() {
            return false;
        }

        @Override
        protected void receivePong(PingPongCommand command) {
            pongs.add(command);
        }

        public final List getSent() {
            return sent;
        }

        public final List getPongs() {
            return pongs;
        }

    }

}
File
SignalConnectionDelegateTest.java
Developer's decision
Version 2
Kind of conflict
Class declaration
Comment
Import
Package declaration