github.com/iikira/iikira-go-utils@v0.0.0-20230610031953-f2cb11cde33a/requester/downloader/download_test.go (about) 1 package downloader 2 3 import ( 4 "fmt" 5 "github.com/iikira/iikira-go-utils/pcsverbose" 6 "github.com/iikira/iikira-go-utils/requester" 7 "os" 8 "testing" 9 "time" 10 ) 11 12 var ( 13 url1 = "https://dldir1.qq.com/qqfile/qq/TIM2.1.8/23475/TIM2.1.8.exe" 14 url2 = "https://git.oschina.net/lufenping/pixabay_img/raw/master/tiny-20170712/lizard-2427248_1920.jpg" 15 ) 16 17 func TestRandomNumber(t *testing.T) { 18 for i := 0; i < 10; i++ { 19 fmt.Println(RandomNumber(0, 5)) 20 } 21 } 22 23 func TestExample(t *testing.T) { 24 DoDownload(url2, "lizard-2427248_1920.jpg", nil) 25 } 26 27 func TestDownloadTIM(t *testing.T) { 28 pcsverbose.IsVerbose = true 29 30 file, _ := os.OpenFile("tim.exe", os.O_CREATE|os.O_WRONLY, 0777) 31 d := NewDownloader(url1, file, &Config{ 32 MaxParallel: 10, 33 CacheSize: 8192, 34 InstanceStatePath: "tmp.txt", 35 }) 36 37 client := requester.NewHTTPClient() 38 client.SetTimeout(10 * time.Second) 39 d.SetClient(client) 40 41 go func() { 42 for { 43 if d.monitor != nil { 44 fmt.Println(d.monitor) 45 } 46 time.Sleep(1e9) 47 } 48 }() 49 go func() { 50 time.Sleep(3e9) 51 d.Pause() 52 time.Sleep(5e9) 53 d.Resume() 54 time.Sleep(9e9) 55 d.Pause() 56 time.Sleep(5e9) 57 d.Resume() 58 time.Sleep(3e9) 59 d.Cancel() 60 fmt.Println("canceled") 61 time.Sleep(3e9) 62 }() 63 err := d.Execute() 64 if err != nil { 65 fmt.Println(err) 66 } 67 } 68 69 func newSlice() [][]byte { 70 s := make([][]byte, 20) 71 s[0] = []byte("kjashdfiuqwheirhwuq") 72 s[9] = []byte("kjashdfiuqwheirhwuq") 73 return s 74 } 75 76 func rangeSlice(f func(key int, by []byte) bool) { 77 s := newSlice() 78 for k := range s { 79 if s[k] == nil { 80 continue 81 } 82 if !f(k, s[k]) { 83 break 84 } 85 } 86 } 87 88 func BenchmarkRange1(b *testing.B) { 89 for i := 0; i < b.N; i++ { 90 var a = 0 91 rangeSlice(func(key int, s []byte) bool { 92 a++ 93 return true 94 }) 95 } 96 } 97 98 func BenchmarkRange2(b *testing.B) { 99 for i := 0; i < b.N; i++ { 100 s := newSlice() 101 a := 0 102 for k := range s { 103 if s[k] == nil { 104 continue 105 } 106 a++ 107 } 108 } 109 }