Skip to content

Commit

Permalink
try fix ResourceManager test (#252)
Browse files Browse the repository at this point in the history
Co-authored-by: Ohad Bitton <[email protected]>
  • Loading branch information
ohadbitt and ohbitton authored Jun 20, 2022
1 parent 7e62bf7 commit 0c0848b
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ private void refreshIngestionResources() throws IngestionClientException, Ingest
addIngestionResource(resourceTypeName, storageUrl);
}
}
log.info("Refreshing Ingestion Resources Finised");
} catch (DataServiceException e) {
throw new IngestionServiceException(e.getIngestionSource(), "Error refreshing IngestionResources", e);
} catch (DataClientException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@
import com.microsoft.azure.kusto.ingest.exceptions.IngestionClientException;
import com.microsoft.azure.kusto.ingest.exceptions.IngestionServiceException;
import org.json.JSONException;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.mockito.stubbing.Answer;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
Expand Down Expand Up @@ -50,6 +51,11 @@ static void setUp() throws DataClientException, DataServiceException, JSONExcept
resourceManager = new ResourceManager(clientMock);
}

@AfterAll
static void afterAll() {
resourceManager.close();
}

@Test
void GetIdentityToken_ReturnsCorrectToken() throws IngestionServiceException, IngestionClientException {
assertEquals(AUTH_TOKEN, resourceManager.getIdentityToken());
Expand Down Expand Up @@ -113,32 +119,7 @@ void GetIngestionResource_SuccessfulIngestionQueue_ReturnCorrectQueue()
resourceManager.getIngestionResource(ResourceManager.ResourceType.SUCCESSFUL_INGESTIONS_QUEUE));
}

@Test
void TimerTest() throws DataClientException, DataServiceException, InterruptedException, KustoServiceQueryError, IOException {
Client mockedClient = mock(Client.class);
final List<Date> refreshTimestamps = new ArrayList<>();
when(mockedClient.execute(Commands.IDENTITY_GET_COMMAND))
.thenReturn(generateIngestionAuthTokenResult());
when(mockedClient.execute(Commands.INGESTION_RESOURCES_SHOW_COMMAND)).then((Answer) invocationOnMock -> {
refreshTimestamps.add((new Date()));
if (refreshTimestamps.size() != 1) {
throw new Exception();
}

return generateIngestionResourcesResult();
});

ResourceManager resourceManager = new ResourceManager(mockedClient, 1000L, 500L);
Thread.sleep(100);
assertEquals(1, refreshTimestamps.size());
Thread.sleep(1100);
assertEquals(2, refreshTimestamps.size());
Thread.sleep(600);
assertEquals(3, refreshTimestamps.size());
resourceManager.close();
}

private static KustoOperationResult generateIngestionResourcesResult() throws JSONException, KustoServiceQueryError, IOException {
static KustoOperationResult generateIngestionResourcesResult() throws JSONException, KustoServiceQueryError, IOException {
List<List<String>> valuesList = new ArrayList<>();
valuesList.add(new ArrayList<>((Arrays.asList("SecuredReadyForAggregationQueue", QUEUE_1))));
valuesList.add(new ArrayList<>((Arrays.asList("SecuredReadyForAggregationQueue", QUEUE_2))));
Expand All @@ -156,7 +137,7 @@ private static KustoOperationResult generateIngestionResourcesResult() throws JS
return new KustoOperationResult(response, "v1");
}

private static KustoOperationResult generateIngestionAuthTokenResult() throws JSONException, KustoServiceQueryError, IOException {
static KustoOperationResult generateIngestionAuthTokenResult() throws JSONException, KustoServiceQueryError, IOException {
List<List<String>> valuesList = new ArrayList<>();
valuesList.add(new ArrayList<>((Collections.singletonList(AUTH_TOKEN))));
String listAsJson = new ObjectMapper().writeValueAsString(valuesList);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.microsoft.azure.kusto.ingest;

import com.microsoft.azure.kusto.data.Client;
import com.microsoft.azure.kusto.data.exceptions.DataClientException;
import com.microsoft.azure.kusto.data.exceptions.DataServiceException;
import com.microsoft.azure.kusto.data.exceptions.KustoServiceQueryError;
import com.microsoft.azure.kusto.ingest.Commands;
import com.microsoft.azure.kusto.ingest.ResourceManager;
import org.junit.jupiter.api.Test;
import org.mockito.stubbing.Answer;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.microsoft.azure.kusto.ingest.ResourceManagerTest.generateIngestionAuthTokenResult;
import static com.microsoft.azure.kusto.ingest.ResourceManagerTest.generateIngestionResourcesResult;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class ResourceManagerTimerTest {

@Test
void TimerTest() throws DataClientException, DataServiceException, InterruptedException, KustoServiceQueryError, IOException {
Client mockedClient = mock(Client.class);
final List<Date> refreshTimestamps = new ArrayList<>();
when(mockedClient.execute(Commands.IDENTITY_GET_COMMAND))
.thenReturn(generateIngestionAuthTokenResult());
when(mockedClient.execute(Commands.INGESTION_RESOURCES_SHOW_COMMAND)).then((Answer) invocationOnMock -> {
refreshTimestamps.add((new Date()));
if (refreshTimestamps.size() != 1) {
throw new Exception();
}

return generateIngestionResourcesResult();
});

ResourceManager resourceManager = new ResourceManager(mockedClient, 1000L, 500L);
Thread.sleep(100);
assertEquals(1, refreshTimestamps.size());
Thread.sleep(1100);
assertEquals(2, refreshTimestamps.size());
Thread.sleep(600);
assertEquals(3, refreshTimestamps.size());
resourceManager.close();
}
}

0 comments on commit 0c0848b

Please sign in to comment.