Skip to content

Commit

Permalink
v0.10.2
Browse files Browse the repository at this point in the history
- 修复waiting导致状态卡住
- 优化gpython的re模块
- 修复单测
  • Loading branch information
wetor committed Oct 11, 2023
1 parent ef89343 commit 9dc72ff
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 92 deletions.
4 changes: 2 additions & 2 deletions internal/animego/anisource/anisource_mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ func (a *BangumiMock) GetCache(id int, filters any) (any, error) {

type ThemoviedbMock struct{}

func (a *ThemoviedbMock) Search(name string) (int, error) {
func (a *ThemoviedbMock) Search(name string, filters any) (int, error) {
return 0, errors.New("ErrNotImplemented")
}

func (a *ThemoviedbMock) SearchCache(name string) (int, error) {
func (a *ThemoviedbMock) SearchCache(name string, filters any) (int, error) {
switch name {
case "err_themoviedb_search":
return 0, &exceptions.ErrRequest{Name: "err_themoviedb_search"}
Expand Down
9 changes: 6 additions & 3 deletions internal/animego/database/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,9 +301,12 @@ func TestOnDownloadExistAnime(t *testing.T) {
test.LogBatchCompare(out, test.MatchContainsRegexp,
"OnDownloadStart",
map[string]any{`\[Plugin\] Rename插件.*? src_\d.mp4`: 3},
map[string]any{`发现部分已下载,跳过此部分重命名: .*?src_\d.mp4`: 3, "OnDownloadSeeding": 1},
"OnDownloadComplete",
map[string]any{`重命名任务不存在,可能已经完成`: 3},
map[string]any{
`发现部分已下载,跳过此部分重命名: .*?src_\d.mp4`: 3,
"OnDownloadSeeding": 1,
"OnDownloadComplete": 1,
`重命名任务不存在,可能已经完成`: 3,
},
)
out.Reset()
// 结束
Expand Down
2 changes: 1 addition & 1 deletion internal/animego/downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (m *Manager) transition(oldTorrentState, newTorrentState models.TorrentStat
}

state := newTorrentState
result := NotifyOnInit
result := NotifyOnStart
switch oldTorrentState {
case StateInit:
// init -> start
Expand Down
120 changes: 96 additions & 24 deletions internal/animego/downloader/downloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,63 @@ var (
out *bytes.Buffer
)

var defaultUpdateList = func(m *qbittorrent.ClientMock) {
for name, item := range m.Name2item {
switch item.State {
case qbittorrent.QbtDownloading, qbittorrent.QbtQueuedUP:
item.State = qbittorrent.QbtDownloading
item.Progress += 0.2
if item.Progress >= 1.005 {
item.Progress = 0
item.State = qbittorrent.QbtUploading
} else {
log.Debugf("%s: 下载: %v (%s)", name, int((item.Progress+0.005)*100), item.State)
}
case qbittorrent.QbtUploading:
item.Progress += 0.2
if item.Progress >= 1.005 {
item.State = qbittorrent.QbtCheckingUP
} else {
log.Debugf("%s: 做种: %v (%s)", name, int((item.Progress+0.005)*100), item.State)
}
default:
log.Debugf("%s: %s", name, item.State)
}
}
}

var waitUpdateList = func(m *qbittorrent.ClientMock) {
for name, item := range m.Name2item {
switch item.State {
case qbittorrent.QbtQueuedUP:
item.Progress += 0.4
if item.Progress >= 1.005 {
item.Progress = 0
item.State = qbittorrent.QbtDownloading
} else {
log.Debugf("%s: 等待: %v (%s)", name, int((item.Progress+0.005)*100), item.State)
}
case qbittorrent.QbtDownloading:
item.Progress += 0.2
if item.Progress >= 1.005 {
item.Progress = 0
item.State = qbittorrent.QbtUploading
} else {
log.Debugf("%s: 下载: %v (%s)", name, int((item.Progress+0.005)*100), item.State)
}
case qbittorrent.QbtUploading:
item.Progress += 0.2
if item.Progress >= 1.005 {
item.State = qbittorrent.QbtCheckingUP
} else {
log.Debugf("%s: 做种: %v (%s)", name, int((item.Progress+0.005)*100), item.State)
}
default:
log.Debugf("%s: %s", name, item.State)
}
}
}

func TestMain(m *testing.M) {
fmt.Println("begin")
_ = os.RemoveAll("data")
Expand Down Expand Up @@ -92,29 +149,7 @@ func TestMain(m *testing.M) {
qbt = &qbittorrent.ClientMock{}
qbt.MockInit(qbittorrent.ClientMockOptions{
DownloadPath: DownloadPath,
UpdateList: func(m *qbittorrent.ClientMock) {
for name, item := range m.Name2item {
switch item.State {
case qbittorrent.QbtDownloading:
item.Progress += 0.2
if item.Progress >= 1.005 {
item.Progress = 0
item.State = qbittorrent.QbtUploading
} else {
log.Debugf("%s: 下载: %v (%s)", name, int((item.Progress+0.005)*100), item.State)
}
case qbittorrent.QbtUploading:
item.Progress += 0.2
if item.Progress >= 1.005 {
item.State = qbittorrent.QbtCheckingUP
} else {
log.Debugf("%s: 做种: %v (%s)", name, int((item.Progress+0.005)*100), item.State)
}
default:
log.Debugf("%s: %s", name, item.State)
}
}
},
UpdateList: defaultUpdateList,
})

downloader.Init(&downloader.Options{
Expand Down Expand Up @@ -248,7 +283,6 @@ func TestManager_ReStartOnDownload(t *testing.T) {
cancel()
}()
wg.Wait()
time.Sleep(1 * time.Second)
exist := dbs.IsExist(&models.AnimeEntity{
NameCN: "test",
Season: 2,
Expand All @@ -259,6 +293,7 @@ func TestManager_ReStartOnDownload(t *testing.T) {
},
})
assert.Equal(t, exist, true)
time.Sleep(1 * time.Second)
for _, f := range file1 {
assert.FileExists(t, f)
}
Expand Down Expand Up @@ -298,4 +333,41 @@ func TestManager_ReStartOnSeed(t *testing.T) {
}
}

func TestManager_StartWait(t *testing.T) {
out.Reset()
qbt.MockInit(qbittorrent.ClientMockOptions{
DownloadPath: DownloadPath,
UpdateList: waitUpdateList,
})
wg, cancel := initTest(true)

go func() {
time.Sleep(14 * time.Second)
download("test", 2, []int{1, 2, 3})
time.Sleep(2 * time.Second)
cancel()
}()
file1, _, _ := download("test", 2, []int{1, 2, 3})
file2, _, _ := download("test", 2, []int{1, 2, 4})
wg.Wait()
time.Sleep(1 * time.Second)
exist := dbs.IsExist(&models.AnimeEntity{
NameCN: "test",
Season: 2,
Ep: []*models.AnimeEpEntity{
{Type: models.AnimeEpNormal, Ep: 1},
{Type: models.AnimeEpNormal, Ep: 2},
{Type: models.AnimeEpNormal, Ep: 3},
{Type: models.AnimeEpNormal, Ep: 4},
},
})
assert.Equal(t, exist, true)
for _, f := range file1 {
assert.FileExists(t, f)
}
for _, f := range file2 {
assert.FileExists(t, f)
}
}

// TODO: 补充单测: 各种状态下重启
6 changes: 4 additions & 2 deletions pkg/client/qbittorrent/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
)

const (
QbtQueuedUP = "queuedUP"
QbtDownloading = "downloading"
QbtUploading = "uploading"
QbtCheckingUP = "checkingUP"
Expand All @@ -24,7 +25,8 @@ const (

var defaultUpdateList = func(m *ClientMock) {
for _, item := range m.Name2item {
if item.State == QbtDownloading {
if item.State == QbtDownloading || item.State == QbtQueuedUP {
item.State = QbtDownloading
item.Progress += 0.5
if item.Progress >= 1.005 {
item.Progress = 0
Expand Down Expand Up @@ -172,7 +174,7 @@ func (m *ClientMock) Add(opt *client.AddOptions) error {
Hash: m.Name2hash[opt.Name],
Name: opt.Name,
Progress: 0.0,
State: QbtDownloading,
State: QbtQueuedUP,
}
return nil
}
Expand Down
15 changes: 3 additions & 12 deletions third_party/gpython/stdlib/re/match.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,11 @@ const match_start_doc = `start([group=0]) -> int.
Return index of the start of the substring matched by group.`

func (m *Match) start(args py.Tuple) (py.Object, error) {
var index_ py.Object
var index_ py.Object = py.Int(0)
err := py.UnpackTuple(args, nil, "start", 0, 1, &index_)
if err != nil {
return nil, err
}
if index_ == nil {
index_ = py.Int(0)
}
index := int(index_.(py.Int))
return py.Int(m.pos + m.index_[index*2]), nil
}
Expand All @@ -168,14 +165,11 @@ const match_end_doc = `end([group=0]) -> int.
Return index of the end of the substring matched by group.`

func (m *Match) end(args py.Tuple) (py.Object, error) {
var index_ py.Object
var index_ py.Object = py.Int(0)
err := py.UnpackTuple(args, nil, "end", 0, 1, &index_)
if err != nil {
return nil, err
}
if index_ == nil {
index_ = py.Int(0)
}
index := int(index_.(py.Int))
return py.Int(m.pos + m.index_[index*2+1]), nil
}
Expand All @@ -184,14 +178,11 @@ const match_span_doc = `span([group]) -> tuple.
For MatchObject m, return the 2-tuple (m.start(group), m.end(group)).`

func (m *Match) span(args py.Tuple) (py.Object, error) {
var index_ py.Object
var index_ py.Object = py.Int(0)
err := py.UnpackTuple(args, nil, "span", 0, 1, &index_)
if err != nil {
return nil, err
}
if index_ == nil {
index_ = py.Int(0)
}
index := int(index_.(py.Int))
return py.Tuple{py.Int(m.pos + m.index_[index*2]), py.Int(m.pos + m.index_[index*2+1])}, nil
}
Expand Down
Loading

0 comments on commit 9dc72ff

Please sign in to comment.