Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f2c8d3c31204271433fe696cc4bc288cd36cf7c2 (plain) (blame)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
//
//  ========================================================================
//  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
//  ------------------------------------------------------------------------
//  All rights reserved. This program and the accompanying materials
//  are made available under the terms of the Eclipse Public License v1.0
//  and Apache License v2.0 which accompanies this distribution.
//
//      The Eclipse Public License is available at
//      http://www.eclipse.org/legal/epl-v10.html
//
//      The Apache License v2.0 is available at
//      http://www.opensource.org/licenses/apache2.0.php
//
//  You may elect to redistribute this code under either of these licenses.
//  ========================================================================
//

package org.eclipse.jetty.io;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.channels.SelectionKey;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

import org.eclipse.jetty.toolchain.test.annotation.Slow;
import org.eclipse.jetty.util.Callback;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.eclipse.jetty.util.thread.TimerScheduler;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class SelectorManagerTest
{
    private QueuedThreadPool executor = new QueuedThreadPool();
    private TimerScheduler scheduler = new TimerScheduler();

    @Before
    public void prepare() throws Exception
    {
        executor.start();
        scheduler.start();
    }

    @After
    public void dispose() throws Exception
    {
        scheduler.stop();
        executor.stop();
    }

    @Slow
    @Test
    public void testConnectTimeoutBeforeSuccessfulConnect() throws Exception
    {
        ServerSocketChannel server = ServerSocketChannel.open();
        server.bind(new InetSocketAddress("localhost", 0));
        SocketAddress address = server.getLocalAddress();

        final AtomicLong timeoutConnection = new AtomicLong();
        final long connectTimeout = 1000;
        SelectorManager selectorManager = new SelectorManager(executor, scheduler)
        {
            @Override
            protected EndPoint newEndPoint(SocketChannel channel, ManagedSelector selector, SelectionKey selectionKey) throws IOException
            {
                return new SelectChannelEndPoint(channel, selector, selectionKey, getScheduler(), connectTimeout / 2);
            }

            @Override
            protected boolean finishConnect(SocketChannel channel) throws IOException
            {
                try
                {
                    long timeout = timeoutConnection.get();
                    if (timeout > 0)
                        TimeUnit.MILLISECONDS.sleep(timeout);
                    return super.finishConnect(channel);
                }
                catch (InterruptedException e)
                {
                    return false;
                }
            }

            @Override
            public Connection newConnection(SocketChannel channel, EndPoint endpoint, Object attachment) throws IOException
            {
                ((Callback)attachment).succeeded();
                return new AbstractConnection(endpoint, executor)
                {
                    @Override
                    public void onFillable()
                    {
                    }
                };
            }

            @Override
            protected void connectionFailed(SocketChannel channel, Throwable ex, Object attachment)
            {
                ((Callback)attachment).failed(ex);
            }
        };
        selectorManager.setConnectTimeout(connectTimeout);
        selectorManager.start();

        try
        {
            SocketChannel client1 = SocketChannel.open();
            client1.configureBlocking(false);
            client1.connect(address);
            long timeout = connectTimeout * 2;
            timeoutConnection.set(timeout);
            final CountDownLatch latch1 = new CountDownLatch(1);
            selectorManager.connect(client1, new Callback()
            {
                @Override
                public void failed(Throwable x)
                {
                    latch1.countDown();
                }
            });
            Assert.assertTrue(latch1.await(connectTimeout * 3, TimeUnit.MILLISECONDS));
            Assert.assertFalse(client1.isOpen());

            // Wait for the first connect to finish, as the selector thread is waiting in finishConnect().
            Thread.sleep(timeout);

            // Verify that after the failure we can connect successfully.
            try (SocketChannel client2 = SocketChannel.open())
            {
                client2.configureBlocking(false);
                client2.connect(address);
                timeoutConnection.set(0);
                final CountDownLatch latch2 = new CountDownLatch(1);
                selectorManager.connect(client2, new Callback()
                {
                    @Override
                    public void succeeded()
                    {
                        latch2.countDown();
                    }
                });
                Assert.assertTrue(latch2.await(connectTimeout * 5, TimeUnit.MILLISECONDS));
                Assert.assertTrue(client2.isOpen());
            }
        }
        finally
        {
            selectorManager.stop();
        }
    }
}

Back to the top