github.com/volts-dev/volts@v0.0.0-20240120094013-5e9c65924106/internal/mdns/server_test.go (about)

     1  package mdns
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  )
     7  
     8  func TestServer_StartStop(t *testing.T) {
     9  	s := makeService(t)
    10  	serv, err := NewServer(&Config{Zone: s, LocalhostChecking: true})
    11  	if err != nil {
    12  		t.Fatalf("err: %v", err)
    13  	}
    14  	defer serv.Shutdown()
    15  }
    16  
    17  func TestServer_Lookup(t *testing.T) {
    18  	serv, err := NewServer(&Config{Zone: makeServiceWithServiceName(t, "_foobar._tcp"), LocalhostChecking: true})
    19  	if err != nil {
    20  		t.Fatalf("err: %v", err)
    21  	}
    22  	defer serv.Shutdown()
    23  
    24  	entries := make(chan *ServiceEntry, 1)
    25  	found := false
    26  	doneCh := make(chan struct{})
    27  	go func() {
    28  		select {
    29  		case e := <-entries:
    30  			if e.Name != "hostname._foobar._tcp.local." {
    31  				t.Logf("bad: %v", e)
    32  				return
    33  			}
    34  			if e.Port != 80 {
    35  				t.Logf("bad: %v", e)
    36  				return
    37  			}
    38  			if e.Info != "Local web server" {
    39  				t.Logf("bad: %v", e)
    40  				return
    41  			}
    42  			found = true
    43  
    44  		case <-time.After(80 * time.Millisecond):
    45  			t.Logf("timeout")
    46  			return
    47  		}
    48  		close(doneCh)
    49  	}()
    50  
    51  	params := &QueryParam{
    52  		Service: "_foobar._tcp",
    53  		Domain:  "local",
    54  		Timeout: 50 * time.Millisecond,
    55  		Entries: entries,
    56  	}
    57  	err = Query(params)
    58  	if err != nil {
    59  		t.Fatalf("err: %v", err)
    60  	}
    61  	<-doneCh
    62  	if !found {
    63  		t.Fatalf("record not found")
    64  	}
    65  }