2014-01-30 02:36:46 +04:00
|
|
|
package mdns
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2014-01-30 03:58:17 +04:00
|
|
|
"time"
|
2014-01-30 02:36:46 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestServer_StartStop(t *testing.T) {
|
|
|
|
s := makeService(t)
|
2014-02-26 02:16:06 +04:00
|
|
|
serv, err := NewServer(&Config{Zone: s})
|
2014-01-30 02:36:46 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
defer serv.Shutdown()
|
|
|
|
}
|
2014-01-30 03:58:17 +04:00
|
|
|
|
|
|
|
func TestServer_Lookup(t *testing.T) {
|
2014-10-16 21:38:12 +04:00
|
|
|
serv, err := NewServer(&Config{Zone: makeServiceWithServiceName(t, "_foobar._tcp")})
|
2014-01-30 03:58:17 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
defer serv.Shutdown()
|
|
|
|
|
|
|
|
entries := make(chan *ServiceEntry, 1)
|
|
|
|
found := false
|
2016-04-30 01:34:13 +03:00
|
|
|
doneCh := make(chan struct{})
|
2014-01-30 03:58:17 +04:00
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case e := <-entries:
|
|
|
|
if e.Name != "hostname._foobar._tcp.local." {
|
|
|
|
t.Fatalf("bad: %v", e)
|
|
|
|
}
|
|
|
|
if e.Port != 80 {
|
|
|
|
t.Fatalf("bad: %v", e)
|
|
|
|
}
|
|
|
|
if e.Info != "Local web server" {
|
|
|
|
t.Fatalf("bad: %v", e)
|
|
|
|
}
|
|
|
|
found = true
|
|
|
|
|
|
|
|
case <-time.After(80 * time.Millisecond):
|
|
|
|
t.Fatalf("timeout")
|
|
|
|
}
|
2016-04-30 01:34:13 +03:00
|
|
|
close(doneCh)
|
2014-01-30 03:58:17 +04:00
|
|
|
}()
|
|
|
|
|
2014-02-26 02:16:06 +04:00
|
|
|
params := &QueryParam{
|
|
|
|
Service: "_foobar._tcp",
|
|
|
|
Domain: "local",
|
|
|
|
Timeout: 50 * time.Millisecond,
|
|
|
|
Entries: entries,
|
|
|
|
}
|
|
|
|
err = Query(params)
|
2014-01-30 03:58:17 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2016-04-30 01:34:13 +03:00
|
|
|
<-doneCh
|
2014-01-30 03:58:17 +04:00
|
|
|
if !found {
|
|
|
|
t.Fatalf("record not found")
|
|
|
|
}
|
|
|
|
}
|