(t *testing.T)
| 116 | } |
| 117 | |
| 118 | func TestMaxIdleConns(t *testing.T) { |
| 119 | s := dnstest.NewServer(func(w dns.ResponseWriter, r *dns.Msg) { |
| 120 | ret := new(dns.Msg) |
| 121 | ret.SetReply(r) |
| 122 | w.WriteMsg(ret) |
| 123 | }) |
| 124 | defer s.Close() |
| 125 | |
| 126 | tr := newTransport("TestMaxIdleConns", s.Addr) |
| 127 | tr.SetMaxIdleConns(2) // Limit to 2 connections per type |
| 128 | tr.Start() |
| 129 | defer tr.Stop() |
| 130 | |
| 131 | // Dial 3 connections |
| 132 | c1, _, _ := tr.Dial("udp") |
| 133 | c2, _, _ := tr.Dial("udp") |
| 134 | c3, _, _ := tr.Dial("udp") |
| 135 | |
| 136 | // Yield all 3 |
| 137 | tr.Yield(c1) |
| 138 | tr.Yield(c2) |
| 139 | tr.Yield(c3) // This should be discarded (pool full) |
| 140 | |
| 141 | // Check pool size is capped at 2 |
| 142 | tr.mu.Lock() |
| 143 | poolSize := len(tr.conns[typeUDP]) |
| 144 | tr.mu.Unlock() |
| 145 | |
| 146 | if poolSize != 2 { |
| 147 | t.Errorf("Expected pool size 2, got %d", poolSize) |
| 148 | } |
| 149 | |
| 150 | // Verify we get the first 2 back (FIFO) |
| 151 | d1, cached1, _ := tr.Dial("udp") |
| 152 | d2, cached2, _ := tr.Dial("udp") |
| 153 | _, cached3, _ := tr.Dial("udp") |
| 154 | |
| 155 | if !cached1 || !cached2 { |
| 156 | t.Error("Expected first 2 dials to be cached") |
| 157 | } |
| 158 | if cached3 { |
| 159 | t.Error("Expected 3rd dial to be non-cached (pool was limited to 2)") |
| 160 | } |
| 161 | if d1 != c1 || d2 != c2 { |
| 162 | t.Error("Expected FIFO order: d1==c1, d2==c2") |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | func TestMaxIdleConnsUnlimited(t *testing.T) { |
| 167 | s := dnstest.NewServer(func(w dns.ResponseWriter, r *dns.Msg) { |
nothing calls this directly
no test coverage detected
searching dependent graphs…