<commit_before>package server

import (
	"crypto/md5"
	"encoding/hex"
	"fmt"
	"net"
	"strconv"
	"strings"
	"sync/atomic"
	"time"

	. "github.com/flynn/flynn/Godeps/_workspace/src/github.com/flynn/go-check"
	"github.com/flynn/flynn/Godeps/_workspace/src/github.com/miekg/dns"
	"github.com/flynn/flynn/discoverd/client"
)

type DNSSuite struct {
	srv   *DNSServer
	store DNSServerStore
}

var _ = Suite(&DNSSuite{})

func (s *DNSSuite) SetUpTest(c *C) {
	s.srv = s.newServer(c, []string{"8.8.8.8", "8.8.4.4"})
	s.srv.SetStore(&s.store)
	s.store.InstancesFn = func(service string) ([]*discoverd.Instance, error) { return nil, nil }
	s.store.ServiceLeaderFn = func(service string) (*discoverd.Instance, error) { return nil, nil }
}

func (s *DNSSuite) newServer(c *C, recursors []string) *DNSServer {
	srv := &DNSServer{
		UDPAddr:   "127.0.0.1:0",
		TCPAddr:   "127.0.0.1:0",
		Recursors: recursors,
	}
	srv.SetStore(&s.store)
	c.Assert(srv.ListenAndServe(), IsNil)
	return srv
}

func (s *DNSSuite) TearDownTest(c *C) {
	if s.srv != nil {
		c.Assert(s.srv.Close(), IsNil)
	}
}

func (s *DNSSuite) TestRecursorParsing(c *C) {
	srv := s.newServer(c, []string{
		"fd00:c2b6:b24b:be67:2827:688d:e6a1:6a3b",
		"[2001:4860:fdf8:f53e:61e4::18]:553",
		"8.8.8.8",
		"8.8.4.4:5553",
		"google-public-dns-a.google.com",
		"google-public-dns-b.google.com:55553",
	})
	srv.Close()
	c.Assert(srv.Recursors, DeepEquals, []string{
		"[2001:fc00:e968:6179::de52:7100]:53",
		"[2001:fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b]:553",
		"8.8.8.8:53",
		"8.8.4.4:5553",
		"8.8.8.8:53",
		"8.8.4.4:55553",
	})
}

func (s *DNSSuite) TestRecursor(c *C) {
	s.srv.Close()<commit_msg>discoverd/server: Don't assert that DNS server has closed in test suite<commit_after>package server

import (
	"crypto/md5"
	"encoding/hex"
	"fmt"
	"net"
	"strconv"
	"strings"
	"sync/atomic"
	"time"

	. "github.com/flynn/flynn/Godeps/_workspace/src/github.com/flynn/go-check"
	"github.com/flynn/flynn/Godeps/_workspace/src/github.com/miekg/dns"
	"github.com/flynn/flynn/discoverd/client"
)

type DNSSuite struct {
	srv   *DNSServer
	store DNSServerStore
}

var _ = Suite(&DNSSuite{})

func (s *DNSSuite) SetUpTest(c *C) {
	s.srv = s.newServer(c, []string{"8.8.8.8", "8.8.4.4"})
	s.srv.SetStore(&s.store)
	s.store.InstancesFn = func(service string) ([]*discoverd.Instance, error) { return nil, nil }
	s.store.ServiceLeaderFn = func(service string) (*discoverd.Instance, error) { return nil, nil }
}

func (s *DNSSuite) newServer(c *C, recursors []string) *DNSServer {
	srv := &DNSServer{
		UDPAddr:   "127.0.0.1:0",
		TCPAddr:   "127.0.0.1:0",
		Recursors: recursors,
	}
	srv.SetStore(&s.store)
	c.Assert(srv.ListenAndServe(), IsNil)
	return srv
}

func (s *DNSSuite) TearDownTest(c *C) {
	if s.srv != nil {
		s.srv.Close()
	}
}

func (s *DNSSuite) TestRecursorParsing(c *C) {
	srv := s.newServer(c, []string{
		"fd00:c2b6:b24b:be67:2827:688d:e6a1:6a3b",
		"[2001:4fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b]:553",
		"8.8.8.8",
		"8.8.4.4:5553",
		"google-public-dns-a.google.com",
		"google-public-dns-b.google.com:55553",
	})
	srv.Close()
	c.Assert(srv.Recursors, DeepEquals, []string{
		"[2001:4860:4fc00:e968:6179::de52:7100]:53",
		"[2001:4860:4860::8888]:553",
		"8.8.8.8:53",
		"8.8.4.4:5553",
		"8.8.8.8:53",
		"8.8.4.4:55553",
	})
}

func (s *DNSSuite) TestRecursor(c *C) {
	s.srv.Close()
{'fraction_non_alphanumeric': 0.20561864913329347, 'fraction_numerical': 0.09384339509862523, 'mean_word_length': 4.279179810725552, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 20, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4711158', 'n_tokens_mistral': 1832, 'n_tokens_neox': 1652, 'n_words': 228}