Factor out channel

This commit is contained in:
Bryan Boreham committed 2014-10-09 14:46:53 +01:00
1 parent 17d192caee
commit 65e95cd46a
1 file changed
+27 -30
+27 -30
View File
@@ -84,9 +84,18 @@ func setup(t *testing.T) (*MDNSClient, *dns.Server, error) {
type testContext struct { type testContext struct {
received_addr net.IP received_addr net.IP
received_count int received_count int
channel chan *ResponseA
} }
func (c *testContext) checkResponse(t *testing.T, resp *ResponseA) { func newTestContext() *testContext {
return &testContext{channel: make(chan *ResponseA, 4)}
}
func (c *testContext) checkResponse(t *testing.T, channel_ok bool, resp *ResponseA) {
if !channel_ok {
c.channel = nil
return
}
if resp.err != nil { if resp.err != nil {
t.Fatal(resp.err) t.Fatal(resp.err)
} }
@@ -101,13 +110,12 @@ func TestSimpleQuery(t *testing.T) {
defer mdnsClient.Shutdown() defer mdnsClient.Shutdown()
defer server.Shutdown() defer server.Shutdown()
var context testContext context := newTestContext()
channel := make(chan *ResponseA, 4)
// First, a test we expect to succeed // First, a test we expect to succeed
mdnsClient.SendQuery(success_test_name, dns.TypeA, channel) mdnsClient.SendQuery(success_test_name, dns.TypeA, context.channel)
for resp := range channel { for resp := range context.channel {
context.checkResponse(t, resp) context.checkResponse(t, true, resp)
} }
if !context.received_addr.Equal(test_addr) { if !context.received_addr.Equal(test_addr) {
@@ -116,11 +124,10 @@ func TestSimpleQuery(t *testing.T) {
} }
// Now, a test we expect to time out with no responses // Now, a test we expect to time out with no responses
context.received_count = 0 context = newTestContext()
channel2 := make(chan *ResponseA, 4) mdnsClient.SendQuery("test2.weave.", dns.TypeA, context.channel)
mdnsClient.SendQuery("test2.weave.", dns.TypeA, channel2) for resp := range context.channel {
for resp := range channel2 { context.checkResponse(t, true, resp)
context.checkResponse(t, resp)
} }
if context.received_count > 0 { if context.received_count > 0 {
@@ -135,29 +142,19 @@ func TestParallelQuery(t *testing.T) {
defer mdnsClient.Shutdown() defer mdnsClient.Shutdown()
defer server.Shutdown() defer server.Shutdown()
var context1 testContext context1 := newTestContext()
var context2 testContext context2 := newTestContext()
channel1 := make(chan *ResponseA, 4)
channel2 := make(chan *ResponseA, 4)
go mdnsClient.SendQuery(success_test_name, dns.TypeA, channel1) go mdnsClient.SendQuery(success_test_name, dns.TypeA, context1.channel)
go mdnsClient.SendQuery(success_test_name, dns.TypeA, channel2) go mdnsClient.SendQuery(success_test_name, dns.TypeA, context2.channel)
timeout := time.After(2 * time.Second) timeout := time.After(2 * time.Second)
outerloop: outerloop:
for channel1 != nil || channel2 != nil { for context1.channel != nil || context2.channel != nil {
select { select {
case resp, ok := <-channel1: case resp, ok := <-context1.channel:
if !ok { context1.checkResponse(t, ok, resp)
channel1 = nil case resp, ok := <-context2.channel:
continue context2.checkResponse(t, ok, resp)
}
context1.checkResponse(t, resp)
case resp, ok := <-channel2:
if !ok {
channel2 = nil
continue
}
context2.checkResponse(t, resp)
case <-timeout: case <-timeout:
break outerloop break outerloop
} }