Пример #1
0
func TestDiscover(t *testing.T) {
	t.Parallel()

	for _, scenario := range discoverScenarios {
		t.Run(scenario.description, func(t *testing.T) {
			target, port, err := dnsdisco.Discover(scenario.service, scenario.proto, scenario.name)

			if target != scenario.expectedTarget {
				t.Errorf("mismatch targets. Expecting: “%s”; found “%s”", scenario.expectedTarget, target)
			}

			if port != scenario.expectedPort {
				t.Errorf("mismatch ports. Expecting: “%d”; found “%d”", scenario.expectedPort, port)
			}

			// As the resolver change between machines, we can't guess the DNSError name's attribute. So we
			// need to inject the value on the expected error
			dnsError, ok1 := err.(*net.DNSError)
			expectedDNSError, ok2 := scenario.expectedError.(*net.DNSError)

			if ok1 && ok2 {
				expectedDNSError.Server = dnsError.Server
			}

			if !reflect.DeepEqual(err, scenario.expectedError) {
				t.Errorf("mismatch errors. Expecting: “%v”; found “%v”", scenario.expectedError, err)
			}
		})
	}
}
Пример #2
0
// ExampleDiscover is the fastest way to select a server using all default
// algorithms.
func ExampleDiscover() {
	target, port, err := dnsdisco.Discover("jabber", "tcp", "registro.br")
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Printf("Target: %s\nPort: %d\n", target, port)

	// Output:
	// Target: jabber.registro.br.
	// Port: 5269
}