-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathqueue_test.go
200 lines (169 loc) · 4.88 KB
/
queue_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package main
import (
"crypto/md5"
"errors"
"fmt"
"github.com/stretchr/testify/assert"
"io"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"
)
func FileServerHandler(directory string) http.Handler {
// Use http.FileServer to serve the directory
fs := http.FileServer(http.Dir(directory))
return http.StripPrefix("/files/", fs)
}
func TestDownloadQueue_QueueDownload_Formats(t *testing.T) {
// Create a test server with the file server handler
ts := httptest.NewServer(FileServerHandler("./test/testdata"))
q := NewDownloadQueue()
t.Run("ISO6Download", func(t *testing.T) {
toPath := t.TempDir() + "/audio_iso6.mp4"
task := DownloadTask{
SrcURL: ts.URL + "/files/audio_iso6.mp4",
ToPath: toPath,
VideoUrl: "http://example.com/video.mp4",
}
err := q.QueueDownload(task)
assert.NoError(t, err)
// Check every 50 ms for maximum of 5 seconds if the download has been queued, begun and finished, if not fail the test
for i := 0; i < 100; i++ {
time.Sleep(50 * time.Millisecond)
if _, err := os.Stat(toPath); errors.Is(err, os.ErrNotExist) {
continue
}
break
}
assert.FileExists(t, toPath)
assertIdenticalFiles(t, "./test/testdata/audio_iso6.mp4", toPath)
})
t.Run("MP42Download", func(t *testing.T) {
toPath := t.TempDir() + "/audio_mp42.mp4"
task := DownloadTask{
SrcURL: ts.URL + "/files/audio_mp42.mp4",
ToPath: toPath,
VideoUrl: "http://example.com/video.mp4",
}
err := q.QueueDownload(task)
assert.NoError(t, err)
for i := 0; i < 100; i++ {
time.Sleep(50 * time.Millisecond)
if _, err := os.Stat(toPath); errors.Is(err, os.ErrNotExist) {
continue
}
break
}
assert.FileExists(t, toPath)
assertIdenticalFiles(t, "./test/testdata/audio_mp42.mp4", toPath)
})
t.Run("InvalidFile", func(t *testing.T) {
toPath := t.TempDir() + "/vid.mp4"
task := DownloadTask{
SrcURL: ts.URL + "/files/vid.mp4",
ToPath: toPath,
VideoUrl: "http://example.com/video.mp4",
}
err := q.QueueDownload(task)
assert.NoError(t, err)
for i := 0; i < 5; i++ {
time.Sleep(1 * time.Second)
assert.NoFileExists(t, toPath)
}
})
t.Cleanup(func() {
q.Release()
ts.Close()
})
}
func TestDownloadQueue_Statuses(t *testing.T) {
// Create a test server with the file server handler
testServer := httptest.NewServer(FileServerHandler("./test/testdata"))
defer testServer.Close()
q := NewDownloadQueue()
defer q.Release()
hasQueued, hasBegun, hasFinished := false, false, false
_, _, _ = hasQueued, hasBegun, hasFinished
// Listen for download status updates
q.OnStatusReceived(func(status DownloadStatus) {
switch status.(type) {
case Queuing:
hasQueued = true
case Begin:
hasBegun = true
case Finished:
hasFinished = true
}
})
task := DownloadTask{
SrcURL: testServer.URL + "/files/audio_iso6.mp4",
ToPath: t.TempDir() + "/audio_iso6.mp4",
VideoUrl: "http://example.com/video.mp4",
}
err := q.QueueDownload(task)
assert.NoError(t, err)
// Check every 50 ms for maximum of 5 seconds if the download has been queued, begun and finished, if not fail the test
for i := 0; i < 100; i++ {
if hasQueued && hasBegun && hasFinished {
break
}
time.Sleep(50 * time.Millisecond)
}
assert.True(t, hasQueued)
assert.True(t, hasBegun)
assert.True(t, hasFinished)
}
// assertIdenticalFiles checks if two files are identical.
func assertIdenticalFiles(t *testing.T, filePath1, filePath2 string) bool {
// Open the first file
file1, err := os.Open(filePath1)
if err != nil {
return assert.Fail(t, "failed to open file1: %v", err)
}
defer file1.Close()
// Open the second file
file2, err := os.Open(filePath2)
if err != nil {
return assert.Fail(t, "failed to open file1: %v", err)
}
defer file2.Close()
// Get the file info for both files
fileInfo1, err := file1.Stat()
if err != nil {
return assert.Fail(t, "failed to stat file1: %v", err)
}
fileInfo2, err := file2.Stat()
if err != nil {
return assert.Fail(t, "failed to stat file2: %v", err)
}
// Compare sizes
if fileInfo1.Size() != fileInfo2.Size() {
return assert.Fail(t, "file sizes are different: %d != %d", fileInfo1.Size(), fileInfo2.Size())
}
// Compare content using MD5 hash
hash1, err := calculateMD5(file1)
if err != nil {
return assert.Fail(t, "failed to calculate hash for file1: %v", err)
}
hash2, err := calculateMD5(file2)
if err != nil {
return assert.Fail(t, "failed to calculate hash for file2: %v", err)
}
// Compare the hashes
return hash1 == hash2
}
// calculateMD5 calculates the MD5 hash of a file
func calculateMD5(file *os.File) (string, error) {
// Reset the file pointer to the beginning
_, err := file.Seek(0, io.SeekStart)
if err != nil {
return "", fmt.Errorf("failed to reset file pointer: %v", err)
}
hash := md5.New()
if _, err := io.Copy(hash, file); err != nil {
return "", fmt.Errorf("failed to hash file: %v", err)
}
return fmt.Sprintf("%x", hash.Sum(nil)), nil
}