service_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package zeroconf
  2. import (
  3. "context"
  4. "log"
  5. "testing"
  6. "time"
  7. "github.com/pkg/errors"
  8. )
  9. var (
  10. mdnsName = "test--xxxxxxxxxxxx"
  11. mdnsService = "test--xxxx.tcp"
  12. mdnsDomain = "local."
  13. mdnsPort = 8888
  14. )
  15. func startMDNS(ctx context.Context, port int, name, service, domain string) {
  16. // 5353 is default mdns port
  17. server, err := Register(name, service, domain, port, []string{"txtv=0", "lo=1", "la=2"}, nil)
  18. if err != nil {
  19. panic(errors.Wrap(err, "while registering mdns service"))
  20. }
  21. defer server.Shutdown()
  22. log.Printf("Published service: %s, type: %s, domain: %s", name, service, domain)
  23. <-ctx.Done()
  24. log.Printf("Shutting down.")
  25. }
  26. func TestBasic(t *testing.T) {
  27. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  28. defer cancel()
  29. go startMDNS(ctx, mdnsPort, mdnsName, mdnsService, mdnsDomain)
  30. time.Sleep(time.Second)
  31. resolver, err := NewResolver(nil)
  32. if err != nil {
  33. t.Fatalf("Expected create resolver success, but got %v", err)
  34. }
  35. entries := make(chan *ServiceEntry)
  36. expectedResult := []*ServiceEntry{}
  37. go func(results <-chan *ServiceEntry) {
  38. s := <-results
  39. expectedResult = append(expectedResult, s)
  40. }(entries)
  41. if err := resolver.Browse(ctx, mdnsService, mdnsDomain, entries); err != nil {
  42. t.Fatalf("Expected browse success, but got %v", err)
  43. }
  44. <-ctx.Done()
  45. if len(expectedResult) != 1 {
  46. t.Fatalf("Expected number of service entries is 1, but got %d", len(expectedResult))
  47. }
  48. if expectedResult[0].Domain != mdnsDomain {
  49. t.Fatalf("Expected domain is %s, but got %s", mdnsDomain, expectedResult[0].Domain)
  50. }
  51. if expectedResult[0].Service != mdnsService {
  52. t.Fatalf("Expected service is %s, but got %s", mdnsService, expectedResult[0].Service)
  53. }
  54. if expectedResult[0].Instance != mdnsName {
  55. t.Fatalf("Expected instance is %s, but got %s", mdnsName, expectedResult[0].Instance)
  56. }
  57. if expectedResult[0].Port != mdnsPort {
  58. t.Fatalf("Expected port is %d, but got %d", mdnsPort, expectedResult[0].Port)
  59. }
  60. }
  61. func TestNoRegister(t *testing.T) {
  62. resolver, err := NewResolver(nil)
  63. if err != nil {
  64. t.Fatalf("Expected create resolver success, but got %v", err)
  65. }
  66. // before register, mdns resolve shuold not have any entry
  67. entries := make(chan *ServiceEntry)
  68. go func(results <-chan *ServiceEntry) {
  69. s := <-results
  70. if s != nil {
  71. t.Fatalf("Expected empty service entries but got %v", *s)
  72. }
  73. }(entries)
  74. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  75. if err := resolver.Browse(ctx, mdnsService, mdnsDomain, entries); err != nil {
  76. t.Fatalf("Expected browse success, but got %v", err)
  77. }
  78. <-ctx.Done()
  79. cancel()
  80. }