downloader_test.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. "runtime"
  9. "testing"
  10. "time"
  11. )
  12. func TestNewDownloader(t *testing.T) {
  13. dialer, err := proxy.SOCKS5("tcp", "127.0.0.1:1080", nil, proxy.Direct)
  14. if err != nil {
  15. t.Fatal(err, dialer)
  16. }
  17. err = <-New(
  18. controller.WithDialer(dialer),
  19. controller.WithCookie(nil),
  20. controller.WithTimeout(time.Second*3),
  21. ).URL("http://10.0.1.34/com.tencent.tmgp.jxqy.apk").
  22. Listener(func(event *Event) {
  23. if event.Key == EventKeyFinally {
  24. fmt.Println("下载完成!")
  25. }
  26. if event.Key == EventKeyProgress {
  27. fmt.Printf("下载速度:%s/s 已下载:%s 已用时:%s \n",
  28. tool.ByteFmt(event.Task.Progress.Speed),
  29. tool.ByteFmt(event.Task.Progress.Downloaded),
  30. time.Duration(event.Task.Progress.Used),
  31. )
  32. }
  33. }).
  34. Create(&base.Options{
  35. Connections: runtime.NumCPU(),
  36. })
  37. t.Log(err)
  38. }