| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | package forward
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"testing"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/plugin/pkg/dnstest"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/miekg/dns"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestPersistent(t *testing.T) {
 | 
					
						
							|  |  |  | 	s := dnstest.NewServer(func(w dns.ResponseWriter, r *dns.Msg) {
 | 
					
						
							|  |  |  | 		ret := new(dns.Msg)
 | 
					
						
							|  |  |  | 		ret.SetReply(r)
 | 
					
						
							|  |  |  | 		w.WriteMsg(ret)
 | 
					
						
							|  |  |  | 	})
 | 
					
						
							|  |  |  | 	defer s.Close()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-15 10:21:57 +01:00
										 |  |  | 	tr := newTransport(s.Addr, nil /* no TLS */)
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 	defer tr.Stop()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-06 15:41:48 +03:00
										 |  |  | 	c1, cache1, _ := tr.Dial("udp")
 | 
					
						
							|  |  |  | 	c2, cache2, _ := tr.Dial("udp")
 | 
					
						
							|  |  |  | 	c3, cache3, _ := tr.Dial("udp")
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if cache1 || cache2 || cache3 {
 | 
					
						
							|  |  |  | 		t.Errorf("Expected non-cached connection")
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	tr.Yield(c1)
 | 
					
						
							|  |  |  | 	tr.Yield(c2)
 | 
					
						
							|  |  |  | 	tr.Yield(c3)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if x := tr.Len(); x != 3 {
 | 
					
						
							|  |  |  | 		t.Errorf("Expected cache size to be 3, got %d", x)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-06 15:41:48 +03:00
										 |  |  | 	c4, cache4, _ := tr.Dial("udp")
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 	if x := tr.Len(); x != 2 {
 | 
					
						
							|  |  |  | 		t.Errorf("Expected cache size to be 2, got %d", x)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-06 15:41:48 +03:00
										 |  |  | 	c5, cache5, _ := tr.Dial("udp")
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 	if x := tr.Len(); x != 1 {
 | 
					
						
							| 
									
										
										
										
											2018-04-06 15:41:48 +03:00
										 |  |  | 		t.Errorf("Expected cache size to be 1, got %d", x)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if cache4 == false || cache5 == false {
 | 
					
						
							|  |  |  | 		t.Errorf("Expected cached connection")
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	tr.Yield(c4)
 | 
					
						
							|  |  |  | 	tr.Yield(c5)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if x := tr.Len(); x != 3 {
 | 
					
						
							|  |  |  | 		t.Errorf("Expected cache size to be 3, got %d", x)
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | }
 |