aboutsummaryrefslogtreecommitdiff
path: root/test/org/traccar/ChannelClosingTest.java
blob: 0b138235fcc70e1f4fcaf5ffa5a90d911212458a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package org.traccar;

import org.jboss.netty.bootstrap.ConnectionlessBootstrap;
import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.*;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.traccar.database.IdentityManager;
import org.traccar.helper.ChannelBufferTools;
import org.traccar.model.Device;
import org.traccar.protocol.GatorProtocol;
import org.traccar.protocol.GatorProtocolDecoder;

import java.net.*;
import java.util.concurrent.CyclicBarrier;

public class ChannelClosingTest {

    @BeforeClass
    public static void init() {
        Context.init(new IdentityManager() {

            private Device createDevice() {
                return null;
            }

            @Override
            public Device getDeviceById(long id) {
                return createDevice();
            }

            @Override
            public Device getDeviceByUniqueId(String imei) {
                return createDevice();
            }

        });
    }

    static class ExceptionWaiter implements ChannelUpstreamHandler {
        final CyclicBarrier barrier = new CyclicBarrier(2);
        Channel channel;

        @Override
        public void handleUpstream(ChannelHandlerContext ctx, ChannelEvent e) throws Exception {
            ctx.sendUpstream(e);
            if (e instanceof ExceptionEvent) {
                channel = e.getChannel();
                barrier.await();
            }
        }

        void waitFor() throws Exception {
            barrier.await();
        }

        Channel channel() {
            return channel;
        }
    }

    @Test
    public void testUDP() throws Exception {
        final ExceptionWaiter exception = new ExceptionWaiter();

        TrackerServer udpServer = new TrackerServer(new ConnectionlessBootstrap(), "gator") {
            @Override
            protected void addSpecificHandlers(ChannelPipeline pipeline) {
                pipeline.addLast("objectDecoder", new GatorProtocolDecoder(new GatorProtocol()));
                pipeline.addLast("exceptionWaiter", exception);
            }
        };
        final int PORT = 50522;
        udpServer.setPort(PORT);
        try {
            udpServer.start();
            Assert.assertFalse(udpServer.getChannelGroup().isEmpty());
            try (DatagramSocket socket = new DatagramSocket()) {
                socket.connect(InetAddress.getLocalHost(), PORT);
                byte[] data = ChannelBufferTools.convertHexString("242400");
                socket.send(new DatagramPacket(data, data.length));
            }
            exception.waitFor();
            Assert.assertFalse(udpServer.getChannelGroup().isEmpty());
            Assert.assertTrue(exception.channel().isBound());
            Assert.assertTrue(exception.channel().isOpen());
        } finally {
            udpServer.stop();
        }
    }

    @Test
    public void testTCP() throws Exception {
        final ExceptionWaiter exception = new ExceptionWaiter();
        TrackerServer tcpServer = new TrackerServer(new ServerBootstrap(), "gator") {
            @Override
            protected void addSpecificHandlers(ChannelPipeline pipeline) {
                pipeline.addLast("objectDecoder", new GatorProtocolDecoder(new GatorProtocol()));
                pipeline.addLast("exceptionWaiter", exception);
            }
        };
        final int PORT = 50522;
        tcpServer.setPort(PORT);
        try {
            tcpServer.start();
            try (Socket socket = new Socket()) {
                socket.connect(new InetSocketAddress(InetAddress.getLocalHost(), PORT));

                byte[] data = ChannelBufferTools.convertHexString("242400");
                socket.getOutputStream().write(data);

                exception.waitFor();
                Assert.assertFalse(exception.channel().isBound());
                Assert.assertFalse(exception.channel().isOpen());
                Assert.assertFalse(exception.channel().isConnected());
            }
        } finally {
            tcpServer.stop();
        }
    }
}