| Jan Schär | 75ea9f4 | 2024-07-29 17:01:41 +0200 | [diff] [blame^] | 1 | package proxy |
| 2 | |
| 3 | // Taken and modified from CoreDNS, under Apache 2.0. |
| 4 | |
| 5 | import ( |
| 6 | "testing" |
| 7 | "time" |
| 8 | |
| 9 | "github.com/miekg/dns" |
| 10 | |
| 11 | "source.monogon.dev/osbase/net/dns/test" |
| 12 | ) |
| 13 | |
| 14 | func TestCached(t *testing.T) { |
| 15 | s := test.NewServer(func(w dns.ResponseWriter, r *dns.Msg) { |
| 16 | ret := new(dns.Msg) |
| 17 | ret.SetReply(r) |
| 18 | w.WriteMsg(ret) |
| 19 | }) |
| 20 | defer s.Close() |
| 21 | |
| 22 | tr := newTransport(s.Addr) |
| 23 | tr.Start() |
| 24 | defer tr.Stop() |
| 25 | |
| 26 | c1, cache1, _ := tr.Dial("udp") |
| 27 | c2, cache2, _ := tr.Dial("udp") |
| 28 | |
| 29 | if cache1 || cache2 { |
| 30 | t.Errorf("Expected non-cached connection") |
| 31 | } |
| 32 | |
| 33 | tr.Yield(c1) |
| 34 | tr.Yield(c2) |
| 35 | c3, cached3, _ := tr.Dial("udp") |
| 36 | if !cached3 { |
| 37 | t.Error("Expected cached connection (c3)") |
| 38 | } |
| 39 | if c2 != c3 { |
| 40 | t.Error("Expected c2 == c3") |
| 41 | } |
| 42 | |
| 43 | tr.Yield(c3) |
| 44 | |
| 45 | // dial another protocol |
| 46 | c4, cached4, _ := tr.Dial("tcp") |
| 47 | if cached4 { |
| 48 | t.Errorf("Expected non-cached connection (c4)") |
| 49 | } |
| 50 | tr.Yield(c4) |
| 51 | } |
| 52 | |
| 53 | func TestCleanupByTimer(t *testing.T) { |
| 54 | s := test.NewServer(func(w dns.ResponseWriter, r *dns.Msg) { |
| 55 | ret := new(dns.Msg) |
| 56 | ret.SetReply(r) |
| 57 | w.WriteMsg(ret) |
| 58 | }) |
| 59 | defer s.Close() |
| 60 | |
| 61 | tr := newTransport(s.Addr) |
| 62 | tr.SetExpire(10 * time.Millisecond) |
| 63 | tr.Start() |
| 64 | defer tr.Stop() |
| 65 | |
| 66 | c1, _, _ := tr.Dial("udp") |
| 67 | c2, _, _ := tr.Dial("udp") |
| 68 | tr.Yield(c1) |
| 69 | time.Sleep(2 * time.Millisecond) |
| 70 | tr.Yield(c2) |
| 71 | |
| 72 | time.Sleep(15 * time.Millisecond) |
| 73 | c3, cached, _ := tr.Dial("udp") |
| 74 | if cached { |
| 75 | t.Error("Expected non-cached connection (c3)") |
| 76 | } |
| 77 | tr.Yield(c3) |
| 78 | |
| 79 | time.Sleep(15 * time.Millisecond) |
| 80 | c4, cached, _ := tr.Dial("udp") |
| 81 | if cached { |
| 82 | t.Error("Expected non-cached connection (c4)") |
| 83 | } |
| 84 | tr.Yield(c4) |
| 85 | } |
| 86 | |
| 87 | func TestCleanupAll(t *testing.T) { |
| 88 | s := test.NewServer(func(w dns.ResponseWriter, r *dns.Msg) { |
| 89 | ret := new(dns.Msg) |
| 90 | ret.SetReply(r) |
| 91 | w.WriteMsg(ret) |
| 92 | }) |
| 93 | defer s.Close() |
| 94 | |
| 95 | tr := newTransport(s.Addr) |
| 96 | |
| 97 | c1, _ := dns.DialTimeout("udp", tr.addr, maxDialTimeout) |
| 98 | c2, _ := dns.DialTimeout("udp", tr.addr, maxDialTimeout) |
| 99 | c3, _ := dns.DialTimeout("udp", tr.addr, maxDialTimeout) |
| 100 | |
| 101 | tr.conns[typeUDP] = []*persistConn{{c1, time.Now()}, {c2, time.Now()}, {c3, time.Now()}} |
| 102 | |
| 103 | if len(tr.conns[typeUDP]) != 3 { |
| 104 | t.Error("Expected 3 connections") |
| 105 | } |
| 106 | tr.cleanup(true) |
| 107 | |
| 108 | if len(tr.conns[typeUDP]) > 0 { |
| 109 | t.Error("Expected no cached connections") |
| 110 | } |
| 111 | } |