speedtest_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. package speedtest
  4. import (
  5. "flag"
  6. "net"
  7. "testing"
  8. "time"
  9. "tailscale.com/cmd/testwrapper/flakytest"
  10. )
  11. var manualTest = flag.Bool("do-speedtest", false, "if true, run the speedtest TestDownload test. Otherwise skip it because it's slow and flaky; see https://github.com/tailscale/tailscale/issues/17338")
  12. func TestDownload(t *testing.T) {
  13. if !*manualTest {
  14. t.Skip("skipping slow test without --do-speedtest")
  15. }
  16. flakytest.Mark(t, "https://github.com/tailscale/tailscale/issues/17338")
  17. // start a listener and find the port where the server will be listening.
  18. ln, err := net.Listen("tcp", ":0")
  19. if err != nil {
  20. t.Fatal(err)
  21. }
  22. t.Cleanup(func() { ln.Close() })
  23. serverIP := ln.Addr().String()
  24. t.Log("server IP found:", serverIP)
  25. type state struct {
  26. err error
  27. }
  28. displayResult := func(t *testing.T, r Result, start time.Time) {
  29. t.Helper()
  30. t.Logf("{ Megabytes: %.2f, Start: %.1f, End: %.1f, Total: %t }", r.MegaBytes(), r.IntervalStart.Sub(start).Seconds(), r.IntervalEnd.Sub(start).Seconds(), r.Total)
  31. }
  32. stateChan := make(chan state, 1)
  33. go func() {
  34. err := Serve(ln)
  35. stateChan <- state{err: err}
  36. }()
  37. // ensure that the test returns an appropriate number of Result structs
  38. expectedLen := int(DefaultDuration.Seconds()) + 1
  39. t.Run("download test", func(t *testing.T) {
  40. // conduct a download test
  41. results, err := RunClient(Download, DefaultDuration, serverIP)
  42. if err != nil {
  43. t.Fatal("download test failed:", err)
  44. }
  45. if len(results) < expectedLen {
  46. t.Fatalf("download results: expected length: %d, actual length: %d", expectedLen, len(results))
  47. }
  48. start := results[0].IntervalStart
  49. for _, result := range results {
  50. displayResult(t, result, start)
  51. }
  52. })
  53. t.Run("upload test", func(t *testing.T) {
  54. // conduct an upload test
  55. results, err := RunClient(Upload, DefaultDuration, serverIP)
  56. if err != nil {
  57. t.Fatal("upload test failed:", err)
  58. }
  59. if len(results) < expectedLen {
  60. t.Fatalf("upload results: expected length: %d, actual length: %d", expectedLen, len(results))
  61. }
  62. start := results[0].IntervalStart
  63. for _, result := range results {
  64. displayResult(t, result, start)
  65. }
  66. })
  67. // causes the server goroutine to finish
  68. ln.Close()
  69. testState := <-stateChan
  70. if testState.err != nil {
  71. t.Error("server error:", err)
  72. }
  73. }