Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

package com.owncloud.android.lib.resources.files;

import androidx.annotation.VisibleForTesting;

import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.network.ChunkFromFileChannelRequestEntity;
import com.owncloud.android.lib.common.network.ProgressiveDataTransfer;
Expand Down Expand Up @@ -60,6 +62,10 @@ public class ChunkedFileUploadRemoteOperation extends UploadFileRemoteOperation
private static final String TAG = ChunkedFileUploadRemoteOperation.class.getSimpleName();
private final boolean onWifiConnection;

public final int ASSEMBLE_TIME_MIN = 30 * 1000; // 30s
public final int ASSEMBLE_TIME_MAX = 30 * 60 * 1000; // 30min
public final int ASSEMBLE_TIME_PER_GB = 3 * 60 * 1000; // 3 min

public ChunkedFileUploadRemoteOperation(String storagePath,
String remotePath,
String mimeType,
Expand Down Expand Up @@ -181,7 +187,9 @@ protected RemoteOperationResult run(OwnCloudClient client) {
if (token != null) {
moveMethod.addRequestHeader(E2E_TOKEN, token);
}
int moveResult = client.executeMethod(moveMethod);

final int DO_NOT_CHANGE_DEFAULT = -1;
int moveResult = client.executeMethod(moveMethod, calculateAssembleTimeout(file), DO_NOT_CHANGE_DEFAULT);

result = new RemoteOperationResult(isSuccess(moveResult), moveMethod);
} catch (Exception e) {
Expand Down Expand Up @@ -315,4 +323,11 @@ private PutMethod createPutMethod(String uriPrefix) {

return putMethod;
}

@VisibleForTesting
public int calculateAssembleTimeout(File file) {
final double fileSizeInGb = file.length() / 1e9;

return Math.max(ASSEMBLE_TIME_MIN, Math.min((int) (ASSEMBLE_TIME_PER_GB * fileSizeInGb), ASSEMBLE_TIME_MAX));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,24 @@

package com.owncloud.android.lib.resources.files;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;

import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class ChunkedFileUploadRemoteOperationTest {

private long chunkSize = 1024;
private final long chunkSize = 1024;

@Mock
File file;

@Test
public void testUploadWithoutExistingChunks() {
Expand Down Expand Up @@ -136,17 +143,66 @@ public void testUploadChunksMissingChunks2() {
assertTrue(test(existingChunks, expectedMissingChunks, chunkSize, length));
}

@Test
public void testAssembleTimeout() {
MockitoAnnotations.openMocks(this);

String modificationTimestamp = String.valueOf(System.currentTimeMillis() / 1000);
ChunkedFileUploadRemoteOperation sut = new ChunkedFileUploadRemoteOperation(null,
null,
null,
null,
modificationTimestamp,
false);

// 0b
when(file.length()).thenReturn(0L);
assertEquals(sut.ASSEMBLE_TIME_MIN, sut.calculateAssembleTimeout(file));

// 100b
when(file.length()).thenReturn(100L);
assertEquals(sut.ASSEMBLE_TIME_MIN, sut.calculateAssembleTimeout(file));

// 1Mb
when(file.length()).thenReturn(1000 * 1000L);
assertEquals(sut.ASSEMBLE_TIME_MIN, sut.calculateAssembleTimeout(file));

// 100Mb
when(file.length()).thenReturn(100 * 1000 * 1000L);
assertEquals(sut.ASSEMBLE_TIME_MIN, sut.calculateAssembleTimeout(file));

// 1Gb
when(file.length()).thenReturn(1000 * 1000 * 1000L);
assertEquals(sut.ASSEMBLE_TIME_PER_GB, sut.calculateAssembleTimeout(file));

// 2Gb
when(file.length()).thenReturn(2 * 1000 * 1000 * 1000L);
assertEquals(2 * sut.ASSEMBLE_TIME_PER_GB, sut.calculateAssembleTimeout(file));

// 5Gb
when(file.length()).thenReturn(5 * 1000 * 1000 * 1000L);
assertEquals(5 * sut.ASSEMBLE_TIME_PER_GB, sut.calculateAssembleTimeout(file));

// 50Gb
when(file.length()).thenReturn(50 * 1000 * 1000 * 1000L);
assertEquals(sut.ASSEMBLE_TIME_MAX, sut.calculateAssembleTimeout(file));

// 500Gb
when(file.length()).thenReturn(500 * 1000 * 1000 * 1000L);
assertEquals(sut.ASSEMBLE_TIME_MAX, sut.calculateAssembleTimeout(file));
}

private boolean test(List<Chunk> existingChunks,
List<Chunk> expectedMissingChunks,
long chunkSize,
long length) {
String modificationTimestamp = String.valueOf(System.currentTimeMillis() / 1000);
ChunkedFileUploadRemoteOperation sut = new ChunkedFileUploadRemoteOperation(null,
null,
null,
null,
modificationTimestamp,
false);
null,
null,
null,
modificationTimestamp,
false);

List<Chunk> missingChunks = sut.checkMissingChunks(existingChunks, length, chunkSize);

Expand Down