-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[improve][websocket][branch-2.11] Add ping support (#19255)
Signed-off-by: Zixuan Liu <nodeces@gmail.com>
- Loading branch information
Showing
7 changed files
with
313 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
pulsar-broker/src/test/java/org/apache/pulsar/websocket/proxy/ProxyIdleTimeoutTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/** | ||
* 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.pulsar.websocket.proxy; | ||
|
||
import static java.util.concurrent.TimeUnit.SECONDS; | ||
import static org.apache.pulsar.broker.BrokerTestUtil.spyWithClassAndConstructorArgs; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.ArgumentMatchers.anyInt; | ||
import static org.mockito.ArgumentMatchers.anyString; | ||
import static org.mockito.Mockito.doReturn; | ||
import java.net.URI; | ||
import java.util.Optional; | ||
import java.util.concurrent.Future; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.pulsar.client.api.ProducerConsumerBase; | ||
import org.apache.pulsar.metadata.impl.ZKMetadataStore; | ||
import org.apache.pulsar.websocket.WebSocketService; | ||
import org.apache.pulsar.websocket.service.ProxyServer; | ||
import org.apache.pulsar.websocket.service.WebSocketProxyConfiguration; | ||
import org.apache.pulsar.websocket.service.WebSocketServiceStarter; | ||
import org.awaitility.Awaitility; | ||
import org.eclipse.jetty.websocket.api.Session; | ||
import org.eclipse.jetty.websocket.client.ClientUpgradeRequest; | ||
import org.eclipse.jetty.websocket.client.WebSocketClient; | ||
import org.testng.annotations.AfterMethod; | ||
import org.testng.annotations.BeforeMethod; | ||
import org.testng.annotations.Test; | ||
|
||
@Test(groups = "websocket") | ||
@Slf4j | ||
public class ProxyIdleTimeoutTest extends ProducerConsumerBase { | ||
protected String methodName; | ||
private ProxyServer proxyServer; | ||
private WebSocketService service; | ||
|
||
private static final int TIME_TO_CHECK_BACKLOG_QUOTA = 5; | ||
|
||
@BeforeMethod | ||
public void setup() throws Exception { | ||
conf.setBacklogQuotaCheckIntervalInSeconds(TIME_TO_CHECK_BACKLOG_QUOTA); | ||
|
||
super.internalSetup(); | ||
super.producerBaseSetup(); | ||
|
||
WebSocketProxyConfiguration config = new WebSocketProxyConfiguration(); | ||
config.setWebServicePort(Optional.of(0)); | ||
config.setClusterName("test"); | ||
config.setConfigurationStoreServers(GLOBAL_DUMMY_VALUE); | ||
config.setWebSocketSessionIdleTimeoutMillis(3 * 1000); | ||
service = spyWithClassAndConstructorArgs(WebSocketService.class, config); | ||
doReturn(new ZKMetadataStore(mockZooKeeperGlobal)).when(service).createMetadataStore(anyString(), anyInt()); | ||
proxyServer = new ProxyServer(config); | ||
WebSocketServiceStarter.start(proxyServer, service); | ||
log.info("Proxy Server Started"); | ||
} | ||
|
||
@AfterMethod(alwaysRun = true) | ||
protected void cleanup() throws Exception { | ||
super.internalCleanup(); | ||
if (service != null) { | ||
service.close(); | ||
} | ||
if (proxyServer != null) { | ||
proxyServer.stop(); | ||
} | ||
log.info("Finished Cleaning Up Test setup"); | ||
} | ||
|
||
@Test | ||
public void testIdleTimeout() throws Exception { | ||
String producerUri = "ws://localhost:" + proxyServer.getListenPortHTTP().get() + | ||
"/ws/v2/producer/persistent/my-property/my-ns/my-topic1/"; | ||
|
||
URI produceUri = URI.create(producerUri); | ||
WebSocketClient produceClient = new WebSocketClient(); | ||
SimpleProducerSocket produceSocket = new SimpleProducerSocket(); | ||
|
||
try { | ||
produceClient.start(); | ||
ClientUpgradeRequest produceRequest = new ClientUpgradeRequest(); | ||
Future<Session> producerFuture = produceClient.connect(produceSocket, produceUri, produceRequest); | ||
assertThat(producerFuture).succeedsWithin(2, SECONDS); | ||
Session session = producerFuture.get(); | ||
Awaitility.await().during(5, SECONDS).untilAsserted(() -> assertThat(session.isOpen()).isFalse()); | ||
} finally { | ||
produceClient.stop(); | ||
} | ||
} | ||
} |
107 changes: 107 additions & 0 deletions
107
pulsar-broker/src/test/java/org/apache/pulsar/websocket/proxy/ProxyPingTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/** | ||
* 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.pulsar.websocket.proxy; | ||
|
||
import static java.util.concurrent.TimeUnit.SECONDS; | ||
import static org.apache.pulsar.broker.BrokerTestUtil.spyWithClassAndConstructorArgs; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.ArgumentMatchers.anyInt; | ||
import static org.mockito.ArgumentMatchers.anyString; | ||
import static org.mockito.Mockito.doReturn; | ||
import java.net.URI; | ||
import java.util.Optional; | ||
import java.util.concurrent.Future; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.pulsar.client.api.ProducerConsumerBase; | ||
import org.apache.pulsar.metadata.impl.ZKMetadataStore; | ||
import org.apache.pulsar.websocket.WebSocketService; | ||
import org.apache.pulsar.websocket.service.ProxyServer; | ||
import org.apache.pulsar.websocket.service.WebSocketProxyConfiguration; | ||
import org.apache.pulsar.websocket.service.WebSocketServiceStarter; | ||
import org.awaitility.Awaitility; | ||
import org.eclipse.jetty.websocket.api.Session; | ||
import org.eclipse.jetty.websocket.client.ClientUpgradeRequest; | ||
import org.eclipse.jetty.websocket.client.WebSocketClient; | ||
import org.testng.annotations.AfterMethod; | ||
import org.testng.annotations.BeforeMethod; | ||
import org.testng.annotations.Test; | ||
|
||
@Test(groups = "websocket") | ||
@Slf4j | ||
public class ProxyPingTest extends ProducerConsumerBase { | ||
protected String methodName; | ||
|
||
private ProxyServer proxyServer; | ||
private WebSocketService service; | ||
|
||
private static final int TIME_TO_CHECK_BACKLOG_QUOTA = 5; | ||
|
||
@BeforeMethod | ||
public void setup() throws Exception { | ||
conf.setBacklogQuotaCheckIntervalInSeconds(TIME_TO_CHECK_BACKLOG_QUOTA); | ||
|
||
super.internalSetup(); | ||
super.producerBaseSetup(); | ||
|
||
WebSocketProxyConfiguration config = new WebSocketProxyConfiguration(); | ||
config.setWebServicePort(Optional.of(0)); | ||
config.setClusterName("test"); | ||
config.setConfigurationStoreServers(GLOBAL_DUMMY_VALUE); | ||
config.setWebSocketSessionIdleTimeoutMillis(3 * 1000); | ||
config.setWebSocketPingDurationSeconds(2); | ||
service = spyWithClassAndConstructorArgs(WebSocketService.class, config); | ||
doReturn(new ZKMetadataStore(mockZooKeeperGlobal)).when(service).createMetadataStore(anyString(), anyInt()); | ||
proxyServer = new ProxyServer(config); | ||
WebSocketServiceStarter.start(proxyServer, service); | ||
log.info("Proxy Server Started"); | ||
} | ||
|
||
@AfterMethod(alwaysRun = true) | ||
protected void cleanup() throws Exception { | ||
super.internalCleanup(); | ||
if (service != null) { | ||
service.close(); | ||
} | ||
if (proxyServer != null) { | ||
proxyServer.stop(); | ||
} | ||
log.info("Finished Cleaning Up Test setup"); | ||
} | ||
|
||
@Test | ||
public void testPing() throws Exception { | ||
String producerUri = "ws://localhost:" + proxyServer.getListenPortHTTP().get() + | ||
"/ws/v2/producer/persistent/my-property/my-ns/my-topic1/"; | ||
|
||
URI produceUri = URI.create(producerUri); | ||
WebSocketClient produceClient = new WebSocketClient(); | ||
SimpleProducerSocket produceSocket = new SimpleProducerSocket(); | ||
|
||
try { | ||
produceClient.start(); | ||
ClientUpgradeRequest produceRequest = new ClientUpgradeRequest(); | ||
Future<Session> producerFuture = produceClient.connect(produceSocket, produceUri, produceRequest); | ||
assertThat(producerFuture).succeedsWithin(2, SECONDS); | ||
Session session = producerFuture.get(); | ||
Awaitility.await().during(5, SECONDS).untilAsserted(() -> assertThat(session.isOpen()).isTrue()); | ||
} finally { | ||
produceClient.stop(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.