downloader_test.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package downloader
  2. import (
  3. "fmt"
  4. "git.bvbej.com/bvbej/base-golang/pkg/downloader/base"
  5. "git.bvbej.com/bvbej/base-golang/pkg/downloader/controller"
  6. "git.bvbej.com/bvbej/base-golang/tool"
  7. "golang.org/x/net/proxy"
  8. "net/http"
  9. "net/url"
  10. "runtime"
  11. "testing"
  12. "time"
  13. )
  14. func TestNewDownloader(t *testing.T) {
  15. dialer, err := proxy.SOCKS5("tcp", "127.0.0.1:1080", nil, proxy.Direct)
  16. parse, _ := url.Parse(`socks5://127.0.0.1:1080`)
  17. if err != nil {
  18. t.Fatal(err, dialer)
  19. }
  20. err = <-New(
  21. controller.WithDialer(dialer), // todo: use dialer proxy
  22. controller.WithCookie(nil),
  23. controller.WithProxy(http.ProxyURL(parse)), // todo: use http client proxy
  24. controller.WithTimeout(time.Second*3),
  25. ).URL("http://10.0.1.34/com.tencent.tmgp.jxqy.apk").
  26. Listener(func(event *Event) {
  27. if event.Key == EventKeyFinally {
  28. fmt.Println("下载完成!")
  29. }
  30. if event.Key == EventKeyProgress {
  31. fmt.Printf("下载速度:%s/s 已下载:%s 已用时:%s \n",
  32. tool.ByteFmt(event.Task.Progress.Speed),
  33. tool.ByteFmt(event.Task.Progress.Downloaded),
  34. time.Duration(event.Task.Progress.Used),
  35. )
  36. }
  37. }).
  38. Create(&base.Options{
  39. Connections: runtime.NumCPU(),
  40. })
  41. t.Log(err)
  42. }