Also test the effect of completeExceptionally()

This commit is contained in:
Benoit Marty 2021-05-05 11:12:06 +02:00
parent 62207f02f0
commit 590d8fc849
3 changed files with 27 additions and 8 deletions

View File

@ -210,7 +210,8 @@ internal class UploadContentWorker(val context: Context, params: WorkerParameter
.also { filesToDelete.add(it) } .also { filesToDelete.add(it) }
} }
VideoCompressionResult.CompressionNotNeeded, VideoCompressionResult.CompressionNotNeeded,
VideoCompressionResult.CompressionCancelled -> { VideoCompressionResult.CompressionCancelled,
is VideoCompressionResult.CompressionFailed -> {
workingFile workingFile
} }
} }
@ -380,7 +381,7 @@ internal class UploadContentWorker(val context: Context, params: WorkerParameter
val updatedContent = when (messageContent) { val updatedContent = when (messageContent) {
is MessageImageContent -> messageContent.update(url, encryptedFileInfo, newAttachmentAttributes) is MessageImageContent -> messageContent.update(url, encryptedFileInfo, newAttachmentAttributes)
is MessageVideoContent -> messageContent.update(url, encryptedFileInfo, thumbnailUrl, thumbnailEncryptedFileInfo, newAttachmentAttributes) is MessageVideoContent -> messageContent.update(url, encryptedFileInfo, thumbnailUrl, thumbnailEncryptedFileInfo, newAttachmentAttributes)
is MessageFileContent -> messageContent.update(url, encryptedFileInfo, newAttachmentAttributes.newFileSize) is MessageFileContent -> messageContent.update(url, encryptedFileInfo, newAttachmentAttributes.newFileSize)
is MessageAudioContent -> messageContent.update(url, encryptedFileInfo, newAttachmentAttributes.newFileSize) is MessageAudioContent -> messageContent.update(url, encryptedFileInfo, newAttachmentAttributes.newFileSize)
else -> messageContent else -> messageContent
} }

View File

@ -22,4 +22,5 @@ internal sealed class VideoCompressionResult {
data class Success(val compressedFile: File) : VideoCompressionResult() data class Success(val compressedFile: File) : VideoCompressionResult()
object CompressionNotNeeded : VideoCompressionResult() object CompressionNotNeeded : VideoCompressionResult()
object CompressionCancelled : VideoCompressionResult() object CompressionCancelled : VideoCompressionResult()
data class CompressionFailed(val failure: Throwable) : VideoCompressionResult()
} }

View File

@ -42,6 +42,7 @@ internal class VideoCompressor @Inject constructor(private val context: Context)
progressListener?.onProgress(0, 100) progressListener?.onProgress(0, 100)
var result: Int = -1 var result: Int = -1
var failure: Throwable? = null
Transcoder.into(destinationFile.path) Transcoder.into(destinationFile.path)
.addDataSource(videoFile.path) .addDataSource(videoFile.path)
.setListener(object : TranscoderListener { .setListener(object : TranscoderListener {
@ -63,6 +64,7 @@ internal class VideoCompressor @Inject constructor(private val context: Context)
override fun onTranscodeFailed(exception: Throwable) { override fun onTranscodeFailed(exception: Throwable) {
Timber.w(exception, "Compressing: failure") Timber.w(exception, "Compressing: failure")
failure = exception
job.completeExceptionally(exception) job.completeExceptionally(exception)
} }
}) })
@ -70,13 +72,23 @@ internal class VideoCompressor @Inject constructor(private val context: Context)
job.join() job.join()
// Note: job is also cancelled if completeExceptionally() was called
if (job.isCancelled) { if (job.isCancelled) {
Timber.w("Compressing: Job cancelled")
// Delete now the temporary file // Delete now the temporary file
deleteFile(destinationFile) deleteFile(destinationFile)
// We do not throw a CancellationException, because it's not critical, we will try to send the original file return when (val finalFailure = failure) {
// Anyway this should never occurs, since we never cancel the return value of transcode() null -> {
return VideoCompressionResult.CompressionCancelled // We do not throw a CancellationException, because it's not critical, we will try to send the original file
// Anyway this should never occurs, since we never cancel the return value of transcode()
Timber.w("Compressing: A failure occurred")
VideoCompressionResult.CompressionCancelled
}
else -> {
// Compression failure can also be considered as not critical, but let the caller decide
Timber.w("Compressing: Job cancelled")
VideoCompressionResult.CompressionFailed(finalFailure)
}
}
} }
progressListener?.onProgress(100, 100) progressListener?.onProgress(100, 100)
@ -90,8 +102,13 @@ internal class VideoCompressor @Inject constructor(private val context: Context)
deleteFile(destinationFile) deleteFile(destinationFile)
VideoCompressionResult.CompressionNotNeeded VideoCompressionResult.CompressionNotNeeded
} }
else -> else -> {
throw IllegalStateException("Unknown result: $result") // Should not happen...
// Delete now the temporary file
deleteFile(destinationFile)
Timber.w("Unknown result: $result")
VideoCompressionResult.CompressionFailed(IllegalStateException("Unknown result: $result"))
}
} }
} }