Skip to content

Commit

Permalink
add cluster delete rest call
Browse files Browse the repository at this point in the history
  • Loading branch information
burak-58 committed Jan 19, 2025
1 parent f946cc4 commit 454f4cf
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/main/java/io/antmedia/AntMediaApplicationAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
Expand Down Expand Up @@ -1282,22 +1284,33 @@ private void putToMap(String keyName, Object keyValue, Map<String, Object> map)
}

public boolean sendClusterPost(String url, String clusterCommunicationToken)
{
HttpPost httpPost = new HttpPost(url);
return callClusterRestMethod(httpPost, clusterCommunicationToken);
}

public boolean sendClusterDelete(String url, String clusterCommunicationToken)
{
HttpDelete httpDelete = new HttpDelete(url);
return callClusterRestMethod(httpDelete, clusterCommunicationToken);
}

public boolean callClusterRestMethod(HttpRequestBase reuqest, String clusterCommunicationToken)
{

boolean result = false;
try (CloseableHttpClient httpClient = getHttpClient())
{
HttpPost httpPost = new HttpPost(url);
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(CLUSTER_POST_TIMEOUT_MS)
.setConnectionRequestTimeout(CLUSTER_POST_TIMEOUT_MS)
.setSocketTimeout(CLUSTER_POST_TIMEOUT_MS)
.build();
httpPost.setConfig(requestConfig);
reuqest.setConfig(requestConfig);

httpPost.setHeader(TokenFilterManager.TOKEN_HEADER_FOR_NODE_COMMUNICATION, clusterCommunicationToken);
reuqest.setHeader(TokenFilterManager.TOKEN_HEADER_FOR_NODE_COMMUNICATION, clusterCommunicationToken);

try (CloseableHttpResponse httpResponse = httpClient.execute(httpPost))
try (CloseableHttpResponse httpResponse = httpClient.execute(reuqest))
{
int statusCode = httpResponse.getStatusLine().getStatusCode();
logger.info("Cluster POST Response Status: {}", statusCode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
Expand Down Expand Up @@ -2203,6 +2204,85 @@ public void testSendClusterPost() throws Exception {
// Verify the retry logic was triggered
//verify(spyAdapter, times(2)).trySendClusterPostWithDelay(eq(testUrl), eq(testToken), eq(0));
}

@Test
public void testSendClusterDelete() throws Exception {
IScope scope = mock(IScope.class);
when(scope.getName()).thenReturn("junit");

AntMediaApplicationAdapter spyAdapter = Mockito.spy(adapter);
IContext context = mock(IContext.class);
when(context.getBean(IAntMediaStreamHandler.VERTX_BEAN_NAME)).thenReturn(vertx);

ApplicationContext appContext = Mockito.mock(ApplicationContext.class);
when(context.getApplicationContext()).thenReturn(appContext);
when(appContext.containsBean(IAntMediaStreamHandler.VERTX_BEAN_NAME)).thenReturn(true);
when(appContext.getBean(IAntMediaStreamHandler.VERTX_BEAN_NAME)).thenReturn(vertx);

AppSettings appSettings = new AppSettings();
appSettings.setWebhookRetryDelay(100); // Small delay for testing
spyAdapter.setAppSettings(appSettings);

CloseableHttpClient httpClient = mock(CloseableHttpClient.class);
Mockito.doReturn(httpClient).when(spyAdapter).getHttpClient();

String testUrl = "http://localhost:5080/test";
String testToken = "test-token";

// Mock responses
CloseableHttpResponse successResponse = mock(CloseableHttpResponse.class);
StatusLine successStatusLine = mock(StatusLine.class);
when(successStatusLine.getStatusCode()).thenReturn(HttpStatus.SC_OK);
when(successResponse.getStatusLine()).thenReturn(successStatusLine);

CloseableHttpResponse failResponse = mock(CloseableHttpResponse.class);
StatusLine failStatusLine = mock(StatusLine.class);
when(failStatusLine.getStatusCode()).thenReturn(HttpStatus.SC_INTERNAL_SERVER_ERROR);
when(failResponse.getStatusLine()).thenReturn(failStatusLine);

// First request fails, second succeeds
when(httpClient.execute(any(HttpDelete.class)))
.thenReturn(failResponse)
.thenReturn(successResponse);

// Test asynchronous behavior
long startTime = System.currentTimeMillis();
boolean result = spyAdapter.sendClusterDelete(testUrl, testToken);

long endTime = System.currentTimeMillis();
// Wait for the CompletableFuture to complete
assertTrue((endTime - startTime) < AntMediaApplicationAdapter.CLUSTER_POST_TIMEOUT_MS);

// Verify request configuration
ArgumentCaptor<HttpDelete> httpDeleteCaptor = ArgumentCaptor.forClass(HttpDelete.class);
verify(httpClient, times(1)).execute(httpDeleteCaptor.capture());

List<HttpDelete> capturedDeletes = httpDeleteCaptor.getAllValues();
assertEquals(testUrl, capturedDeletes.get(0).getURI().toString());
assertEquals(testToken, capturedDeletes.get(0)
.getFirstHeader(TokenFilterManager.TOKEN_HEADER_FOR_NODE_COMMUNICATION).getValue());

RequestConfig capturedConfig = capturedDeletes.get(0).getConfig();
assertEquals(AntMediaApplicationAdapter.CLUSTER_POST_TIMEOUT_MS, capturedConfig.getConnectTimeout());
assertEquals(AntMediaApplicationAdapter.CLUSTER_POST_TIMEOUT_MS, capturedConfig.getConnectionRequestTimeout());
assertEquals(AntMediaApplicationAdapter.CLUSTER_POST_TIMEOUT_MS, capturedConfig.getSocketTimeout());

// Test no retries left after failure
when(httpClient.execute(any(HttpDelete.class))).thenReturn(failResponse);
result = spyAdapter.sendClusterDelete(testUrl, testToken);
assertFalse(result);

// Test IOException handling with retries
when(httpClient.execute(any(HttpDelete.class)))
.thenThrow(new IOException("Test exception"))
.thenReturn(successResponse);

result = spyAdapter.sendClusterDelete(testUrl, testToken);
assertFalse(result);

// Verify the retry logic was triggered
//verify(spyAdapter, times(2)).trySendClusterPostWithDelay(eq(testUrl), eq(testToken), eq(0));
}

@Test
public void testStreamFetcherNotStartAutomatically()
Expand Down

0 comments on commit 454f4cf

Please sign in to comment.