Skip to content

Commit

Permalink
fix test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
USAMAWIZARD committed Jan 18, 2025
1 parent 46a5d64 commit d8d0298
Showing 1 changed file with 52 additions and 14 deletions.
66 changes: 52 additions & 14 deletions src/test/java/io/antmedia/test/MuxerUnitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,7 @@
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.*;
import java.lang.ref.WeakReference;
import java.nio.ByteBuffer;
import java.util.ArrayList;
Expand All @@ -49,6 +45,7 @@
import java.util.concurrent.atomic.AtomicBoolean;

import io.antmedia.*;
import io.grpc.Context;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.lang3.RandomUtils;
import org.apache.mina.core.buffer.IoBuffer;
Expand Down Expand Up @@ -81,6 +78,7 @@
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;
import org.mockito.ArgumentCaptor;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.red5.codec.AbstractVideo;
import org.red5.codec.IAudioStreamCodec;
Expand Down Expand Up @@ -5963,34 +5961,71 @@ public void testGetSubfolder() throws Exception {

}

static class RecordMuxerMock extends RecordMuxer {
public static class RecordMuxerMock extends RecordMuxer {
protected RecordMuxerMock(StorageClient storageClient, Vertx vertx, String s3FolderPath) {
super(storageClient, vertx, s3FolderPath);
}
@Override
protected void finalizeRecordFile(File file) throws IOException {
}

}
public static class StorageClientMock extends StorageClient{
static Boolean saveCalledWithCorrectParams = false;

@Override
public void delete(String key) {

}

@Override
public void save(String key, InputStream inputStream, boolean waitForCompletion) {

}

@Override
public void save(String key, File file, boolean deleteLocalFile) {
logger.info(key);
String result = RecordMuxer.replaceDoubleSlashesWithSingleSlash(key);
if(key.equals(result)) {
saveCalledWithCorrectParams = true;
}
}

@Override
public boolean fileExist(String key) {
return false;
}

@Override
public void reset() {

}
}

@Test
public void testWriteTrailer() throws IOException, InterruptedException {

Vertx vertx = Vertx.vertx();
RecordMuxerMock recordMuxerMock = Mockito.spy(new RecordMuxerMock(Mockito.mock(StorageClient.class), vertx, ""));
File file = Mockito.spy(new File("test"));
StorageClientMock storageClient = Mockito.spy(new StorageClientMock());
Mockito.when(storageClient.isEnabled()).thenReturn(true);
RecordMuxerMock recordMuxerMock = Mockito.spy(new RecordMuxerMock(storageClient, vertx, "streams"));
appScope = (WebScope) applicationContext.getBean("web.scope");
recordMuxerMock.init(appScope, "test", 0, "", 0);
recordMuxerMock.init(appScope, "", 0, "", 0);
recordMuxerMock.setIsRunning(new AtomicBoolean(true));
doReturn(true).when(recordMuxerMock).isUploadingToS3();

File file = mock(File.class);
recordMuxerMock.setFileTmp(file);
Mockito.when(file.exists()).thenReturn(true);
StorageClient storageClient = Mockito.mock(StorageClient.class);
Mockito.when(storageClient.isEnabled()).thenReturn(true);
doReturn(new File("test")).when(recordMuxerMock).getFinalFileName(anyBoolean());


doReturn(new File("test.mp4")).when(recordMuxerMock).getFinalFileName(anyBoolean());
doNothing().when(recordMuxerMock).finalizeRecordFile(any());

AppSettings settings = Mockito.mock(AppSettings.class);
doReturn(true).when(settings).isS3RecordingEnabled();
doReturn(7).when(settings).getUploadExtensionsToS3();
doReturn(settings).when(recordMuxerMock).getAppSettings();

AntMediaApplicationAdapter adapter = Mockito.mock(AntMediaApplicationAdapter.class);
Expand All @@ -6001,17 +6036,20 @@ public void testWriteTrailer() throws IOException, InterruptedException {
doNothing().when(adapter).muxingFinished(any(),anyString(),any(),anyLong(),anyLong(),anyInt(),anyString(),anyString());

recordMuxerMock.writeTrailer();
Thread.sleep(5000);

Thread.sleep(500);

verify(recordMuxerMock, times(1)).finalizeRecordFile(any());
verify(recordMuxerMock, times(1)).getFinalFileName(anyBoolean());
assert (StorageClientMock.saveCalledWithCorrectParams);

recordMuxerMock.writeTrailer();


//trailer already written should not invoke again
verify(recordMuxerMock, times(1)).finalizeRecordFile(any());
verify(recordMuxerMock, times(1)).getFinalFileName(anyBoolean());

verify(recordMuxerMock, times(1)).getFinalFileName(anyBoolean());

}
}

0 comments on commit d8d0298

Please sign in to comment.