-
Notifications
You must be signed in to change notification settings - Fork 59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix TorrentDownloadController #1208
Open
StageGuard
wants to merge
8
commits into
main
Choose a base branch
from
sg/torrent-downloader-controller
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+430
−21
Open
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d1ce487
Fix TorrentDownloadController
StageGuard 3f96b7b
Fix footer
StageGuard 08c50f6
Fix onSeek will fill piece which has downloaded.
StageGuard 41b4d12
expand lastIndex to the last piece index in piece list, contains foot…
StageGuard 92eac52
Add unit test
StageGuard 38066dc
Always add footer piece which hasn't downloaded when seek
StageGuard 5d41f62
add more test
StageGuard fd99103
fix import
StageGuard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
torrent/api/src/commonTest/kotlin/api/pieces/TorrentDownloadControllerTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* | ||
* Copyright (C) 2024 OpenAni and contributors. | ||
* | ||
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. | ||
* Use of this source code is governed by the GNU AGPLv3 license, which can be found at the following link. | ||
* | ||
* https://github.com/open-ani/ani/blob/main/LICENSE | ||
*/ | ||
|
||
package me.him188.ani.app.torrent.api.pieces | ||
|
||
import me.him188.ani.datasources.api.topic.FileSize.Companion.kiloBytes | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
internal class TorrentDownloadControllerTest { | ||
internal val Int.kb: Long get() = kiloBytes.inBytes | ||
|
||
@Test | ||
fun `test sequence download and seek`() { | ||
var currentDownloadingPieces: List<Int> = emptyList() | ||
var currentPossibleFooterRange: IntRange = IntRange.EMPTY | ||
|
||
val priorities = object : PiecePriorities { | ||
override fun downloadOnly(pieceIndexes: List<Int>, possibleFooterRange: IntRange) { | ||
currentDownloadingPieces = pieceIndexes | ||
currentPossibleFooterRange = possibleFooterRange | ||
} | ||
} | ||
|
||
val pieceList = PieceList.create( | ||
totalSize = 1000.kb, | ||
pieceSize = 1.kb, | ||
StageGuard marked this conversation as resolved.
Show resolved
Hide resolved
|
||
initialDataOffset = 0, | ||
initialPieceIndex = 0, | ||
StageGuard marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
|
||
val controller = TorrentDownloadController( | ||
pieces = pieceList, | ||
priorities = priorities, | ||
windowSize = 10, | ||
headerSize = 5.kb, | ||
footerSize = 3.kb, | ||
possibleFooterSize = 12.kb, | ||
) | ||
|
||
fun finishPiece(index: Int) { | ||
with(pieceList) { getByPieceIndex(index).state = PieceState.FINISHED } | ||
controller.onPieceDownloaded(index) | ||
} | ||
|
||
StageGuard marked this conversation as resolved.
Show resolved
Hide resolved
|
||
controller.onTorrentResumed() | ||
// resume 后立刻请求 windowSize 大小的 header 和 footer | ||
assertEquals(13, currentDownloadingPieces.size) | ||
assertEquals(listOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 997, 998, 999), currentDownloadingPieces) | ||
assertEquals(988..999, currentPossibleFooterRange) | ||
|
||
// resume window 内的 piece 会使 window 向后滑动 | ||
StageGuard marked this conversation as resolved.
Show resolved
Hide resolved
|
||
finishPiece(0) | ||
assertEquals(listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 997, 998, 999), currentDownloadingPieces.sorted()) | ||
StageGuard marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
finishPiece(1) | ||
assertEquals(listOf(2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 997, 998, 999), currentDownloadingPieces.sorted()) | ||
|
||
finishPiece(5) | ||
assertEquals(listOf(2, 3, 4, 6, 7, 8, 9, 10, 11, 12, 997, 998, 999), currentDownloadingPieces.sorted()) | ||
|
||
finishPiece(12) | ||
assertEquals(listOf(2, 3, 4, 6, 7, 8, 9, 10, 11, 13, 997, 998, 999), currentDownloadingPieces.sorted()) | ||
|
||
// resume window 外的 piece 不会使 window 向后滑动 | ||
StageGuard marked this conversation as resolved.
Show resolved
Hide resolved
|
||
finishPiece(100) | ||
assertEquals(listOf(2, 3, 4, 6, 7, 8, 9, 10, 11, 13, 997, 998, 999), currentDownloadingPieces.sorted()) | ||
|
||
// 不会重复请求已经完成的 piece | ||
StageGuard marked this conversation as resolved.
Show resolved
Hide resolved
|
||
(10..99).forEach { finishPiece(it) } | ||
assertEquals(listOf(2, 3, 4, 6, 7, 8, 9, 101, 102, 103, 997, 998, 999), currentDownloadingPieces.sorted()) | ||
|
||
// seek 到 200, seek 后面的地方不会请求 footer | ||
StageGuard marked this conversation as resolved.
Show resolved
Hide resolved
StageGuard marked this conversation as resolved.
Show resolved
Hide resolved
|
||
controller.onSeek(200) | ||
assertEquals(listOf(200, 201, 202, 203, 204, 205, 206, 207, 208, 209), currentDownloadingPieces.sorted()) | ||
|
||
(200..220).forEach { finishPiece(it) } | ||
assertEquals(listOf(221, 222, 223, 224, 225, 226, 227, 228, 229, 230), currentDownloadingPieces.sorted()) | ||
|
||
// seek 到前面已经完成的部分, window 应该填充 50 后的前十个未完成的 piece | ||
controller.onSeek(50) | ||
assertEquals(listOf(101, 102, 103, 104, 105, 106, 107, 108, 109, 110), currentDownloadingPieces.sorted()) | ||
StageGuard marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// 200 - 220 已经完成了 | ||
controller.onSeek(200) | ||
assertEquals(listOf(221, 222, 223, 224, 225, 226, 227, 228, 229, 230), currentDownloadingPieces.sorted()) | ||
|
||
(101..109 step 2).forEach { finishPiece(it) } | ||
controller.onSeek(50) | ||
assertEquals(listOf(102, 104, 106, 108, 110, 111, 112, 113, 114, 115), currentDownloadingPieces.sorted()) | ||
|
||
// 完成很多 piece | ||
(0..994).forEach { finishPiece(it) } | ||
controller.onSeek(100) | ||
assertEquals(listOf(995, 996, 997, 998, 999), currentDownloadingPieces.sorted()) | ||
|
||
// 测试边界 piece | ||
(998..999).forEach { finishPiece(it) } | ||
controller.onSeek(100) | ||
assertEquals(listOf(995, 996, 997), currentDownloadingPieces.sorted()) | ||
|
||
finishPiece(996) | ||
controller.onSeek(100) | ||
assertEquals(listOf(995, 997), currentDownloadingPieces.sorted()) | ||
} | ||
StageGuard marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
如果 pieces 为空, 这就会抛出异常