Skip to content

Commit 284c03f

Browse files
committed
Add test case for resuming chunked uploads
Signed-off-by: ZetaTom <70907959+ZetaTom@users.noreply.github.com>
1 parent 7db3f50 commit 284c03f

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

library/src/androidTest/java/com/owncloud/android/lib/resources/files/webdav/ChunkedFileUploadRemoteOperationIT.kt

+72
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,17 @@
2222
package com.owncloud.android.lib.resources.files.webdav
2323

2424
import com.owncloud.android.AbstractIT
25+
import com.owncloud.android.lib.common.network.WebdavEntry
26+
import com.owncloud.android.lib.common.network.WebdavUtils
2527
import com.owncloud.android.lib.common.operations.RemoteOperationResult
2628
import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode
2729
import com.owncloud.android.lib.resources.files.ChunkedFileUploadRemoteOperation
2830
import junit.framework.TestCase
2931
import junit.framework.TestCase.assertNotNull
3032
import junit.framework.TestCase.assertTrue
33+
import org.apache.jackrabbit.webdav.client.methods.PropFindMethod
3134
import org.junit.Test
35+
import java.io.File
3236

3337
class ChunkedFileUploadRemoteOperationIT : AbstractIT() {
3438
@Test
@@ -70,6 +74,65 @@ class ChunkedFileUploadRemoteOperationIT : AbstractIT() {
7074
TestCase.assertSame(ResultCode.CANCELLED, uploadResult?.code)
7175
}
7276

77+
@Test
78+
fun resume() {
79+
val filePath = createFile("chunkedFile.txt", BIG_FILE_ITERATION * 2)
80+
val timestamp = System.currentTimeMillis() / MILLI_TO_SECOND
81+
val remotePath = "/bigFile.md"
82+
83+
// set up first upload
84+
var sut = ChunkedFileUploadRemoteOperation(
85+
filePath,
86+
remotePath,
87+
"text/markdown",
88+
"",
89+
RANDOM_MTIME,
90+
timestamp,
91+
false,
92+
true
93+
)
94+
95+
// start first upload
96+
var uploadResult: RemoteOperationResult<String>? = null
97+
Thread {
98+
uploadResult = sut.execute(client)
99+
}.start()
100+
101+
// delay and cancel upload
102+
shortSleep()
103+
shortSleep()
104+
sut.cancel(ResultCode.CANCELLED)
105+
106+
for (i in 1..MAX_TRIES) {
107+
shortSleep()
108+
109+
if (uploadResult != null) {
110+
break
111+
}
112+
}
113+
114+
// start second upload of same file
115+
sut = ChunkedFileUploadRemoteOperation(
116+
filePath,
117+
remotePath,
118+
"text/markdown",
119+
"",
120+
RANDOM_MTIME,
121+
timestamp,
122+
false,
123+
true
124+
)
125+
126+
// reset result; start second upload
127+
uploadResult = null
128+
uploadResult = sut.execute(client)
129+
130+
// second upload should succeed
131+
assert(uploadResult?.isSuccess == true)
132+
133+
assert(File(filePath).length() == getRemoteSize(remotePath))
134+
}
135+
73136
private fun genLargeUpload(onWifiConnection: Boolean): ChunkedFileUploadRemoteOperation {
74137
// create file
75138
val filePath = createFile("chunkedFile.txt", BIG_FILE_ITERATION)
@@ -87,6 +150,15 @@ class ChunkedFileUploadRemoteOperationIT : AbstractIT() {
87150
)
88151
}
89152

153+
private fun getRemoteSize(remotePath: String): Long {
154+
val davPath = client.filesDavUri.toString() + "/" + WebdavUtils.encodePath(remotePath)
155+
val propFindMethod = PropFindMethod(davPath, WebdavUtils.getFilePropSet(), 0)
156+
client.executeMethod(propFindMethod)
157+
assert(propFindMethod.succeeded())
158+
159+
return WebdavEntry(propFindMethod.responseBodyAsMultiStatus.responses[0], remotePath).contentLength
160+
}
161+
90162
companion object {
91163
const val BIG_FILE_ITERATION = 1000000
92164
const val MAX_TRIES = 30

0 commit comments

Comments
 (0)