File: TesterWsRemoteEndpointImplServer.java

package info (click to toggle)
tomcat10 10.1.52-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 47,900 kB
  • sloc: java: 375,756; xml: 59,410; jsp: 4,741; sh: 1,381; perl: 324; makefile: 25; ansic: 14
file content (161 lines) | stat: -rw-r--r-- 5,787 bytes parent folder | download | duplicates (9)
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
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.tomcat.websocket.server;

import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import jakarta.websocket.CloseReason;
import jakarta.websocket.ContainerProvider;
import jakarta.websocket.EncodeException;
import jakarta.websocket.Encoder;
import jakarta.websocket.EndpointConfig;
import jakarta.websocket.OnClose;
import jakarta.websocket.OnError;
import jakarta.websocket.OnMessage;
import jakarta.websocket.OnOpen;
import jakarta.websocket.Session;
import jakarta.websocket.WebSocketContainer;
import jakarta.websocket.server.ServerEndpointConfig;

import org.junit.Test;

import org.apache.catalina.Context;
import org.apache.catalina.servlets.DefaultServlet;
import org.apache.catalina.startup.Tomcat;
import org.apache.tomcat.websocket.WebSocketBaseTest;
import org.apache.tomcat.websocket.pojo.TesterUtil.SimpleClient;

/*
 * This test requires manual intervention to create breakpoints etc.
 */
public class TesterWsRemoteEndpointImplServer extends WebSocketBaseTest {

    /*
     * https://bz.apache.org/bugzilla/show_bug.cgi?id=58624
     *
     * This test requires three breakpoints to be set. Two in this file (marked A & B with comments) and one (C) at the
     * start of WsRemoteEndpointImplServer.doWrite().
     *
     * With the breakpoints in place, run this test. Once breakpoints A & B are reached, progress the thread at
     * breakpoint A one line to close the connection. Once breakpoint C is reached, allow the thread at breakpoint B to
     * continue. Then allow the thread at breakpoint C to continue.
     *
     * In the failure mode, the thread at breakpoint B will not progress past the call to sendObject(). If the issue is
     * fixed, the thread at breakpoint B will continue past sendObject() and terminate with a TimeoutException.
     */
    @Test
    public void testClientDropsConnection() throws Exception {
        Tomcat tomcat = getTomcatInstance();
        // No file system docBase required
        Context ctx = getProgrammaticRootContext();
        ctx.addApplicationListener(Bug58624Config.class.getName());
        Tomcat.addServlet(ctx, "default", new DefaultServlet());
        ctx.addServletMappingDecoded("/", "default");

        WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();

        tomcat.start();

        SimpleClient client = new SimpleClient();
        URI uri = new URI("ws://localhost:" + getPort() + Bug58624Config.PATH);

        Session session = wsContainer.connectToServer(client, uri);
        // Break point A required on following line
        session.close();
    }

    public static class Bug58624Config extends TesterEndpointConfig {

        public static final String PATH = "/bug58624";


        @Override
        protected ServerEndpointConfig getServerEndpointConfig() {
            List<Class<? extends Encoder>> encoders = new ArrayList<>();
            encoders.add(Bug58624Encoder.class);
            return ServerEndpointConfig.Builder.create(Bug58624Endpoint.class, PATH).encoders(encoders).build();
        }
    }

    public static class Bug58624Endpoint {

        private static final ExecutorService ex = Executors.newFixedThreadPool(1);

        @OnOpen
        public void onOpen(Session session) {
            // Disabling blocking timeouts for this test
            session.getUserProperties().put(org.apache.tomcat.websocket.Constants.BLOCKING_SEND_TIMEOUT_PROPERTY,
                    Long.valueOf(-1));
            ex.submit(new Bug58624SendMessage(session));
        }

        @OnMessage
        public void onMessage(String message) {
            System.out.println("OnMessage: " + message);
        }

        @OnError
        public void onError(Throwable t) {
            System.err.println("OnError:");
            t.printStackTrace();
        }

        @OnClose
        public void onClose(@SuppressWarnings("unused") Session session, CloseReason cr) {
            System.out.println("Closed " + cr);
        }
    }

    public static class Bug58624SendMessage implements Runnable {
        private Session session;

        public Bug58624SendMessage(Session session) {
            this.session = session;
        }

        @Override
        public void run() {
            try {
                // Breakpoint B required on following line
                session.getBasicRemote().sendObject("test");
            } catch (IOException | EncodeException e) {
                e.printStackTrace();
            }
        }
    }

    public static class Bug58624Encoder implements Encoder.Text<Object> {

        @Override
        public void destroy() {
        }

        @Override
        public void init(EndpointConfig endpointConfig) {
        }

        @Override
        public String encode(Object object) throws EncodeException {
            return (String) object;
        }
    }
}