ndt_runner_test.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package speedtest
  2. import (
  3. "context"
  4. "testing"
  5. "github.com/m-lab/ndt7-client-go/spec"
  6. )
  7. func TestNdtServerHost(t *testing.T) {
  8. cases := map[string]string{
  9. "": "",
  10. " ": "",
  11. "ndt-mlab1-mad04.mlab-oti.org": "ndt-mlab1-mad04.mlab-oti.org",
  12. "wss://ndt.example.com/ndt/v7/download": "ndt.example.com",
  13. "ndt.example.com:4443": "ndt.example.com:4443",
  14. }
  15. for in, want := range cases {
  16. if got := ndtServerHost(in); got != want {
  17. t.Errorf("ndtServerHost(%q) = %q, want %q", in, got, want)
  18. }
  19. }
  20. }
  21. // fakePhase alimenta un phase con medidas AppInfo/TCPInfo prefijadas.
  22. func fakePhase(t *testing.T, ms []spec.Measurement) func(context.Context) (<-chan spec.Measurement, error) {
  23. return func(context.Context) (<-chan spec.Measurement, error) {
  24. ch := make(chan spec.Measurement, len(ms))
  25. for _, m := range ms {
  26. ch <- m
  27. }
  28. close(ch)
  29. return ch, nil
  30. }
  31. }
  32. func TestNdtRunPhaseRateAndRTT(t *testing.T) {
  33. // 100 MB en 10 s = 80 Mbps; MinRTT 12.5 ms (12500 us).
  34. // El campo MinRTT es promovido del LinuxTCPInfo embebido: en literal no
  35. // compila con go1.26, se asigna aparte.
  36. ti := &spec.TCPInfo{}
  37. ti.MinRTT = 12500
  38. ms := []spec.Measurement{
  39. {AppInfo: &spec.AppInfo{NumBytes: 50_000_000, ElapsedTime: 5e6}, TCPInfo: ti},
  40. {AppInfo: &spec.AppInfo{NumBytes: 100_000_000, ElapsedTime: 10e6}, TCPInfo: ti},
  41. }
  42. mbps, rtt, err := ndtRunPhase(context.Background(), fakePhase(t, ms))
  43. if err != nil {
  44. t.Fatal(err)
  45. }
  46. if mbps < 79.9 || mbps > 80.1 {
  47. t.Errorf("mbps = %v, want ~80", mbps)
  48. }
  49. if rtt != 12500 {
  50. t.Errorf("rtt = %v, want 12500", rtt)
  51. }
  52. }
  53. func TestNdtRunPhaseSinMediciones(t *testing.T) {
  54. _, _, err := ndtRunPhase(context.Background(), fakePhase(t, nil))
  55. if err == nil {
  56. t.Fatal("esperaba error con phase vacío")
  57. }
  58. }