|
| 1 | +import http from 'node:http'; |
1 | 2 | import test from 'ava'; |
2 | 3 | import got from '../source/index.js'; |
3 | 4 | import withServer from './helpers/with-server.js'; |
@@ -109,6 +110,53 @@ test('dns timing is 0 for IP addresses', withServer, async (t, server) => { |
109 | 110 | t.is(timings.lookup, timings.socket); |
110 | 111 | }); |
111 | 112 |
|
| 113 | +test('dns timing is 0 for cached DNS lookups', withServer, async (t, server, got) => { |
| 114 | + server.get('/', (_request, response) => { |
| 115 | + response.end('ok'); |
| 116 | + }); |
| 117 | + |
| 118 | + // Enable DNS cache and disable keep-alive to get new connections |
| 119 | + const instance = got.extend({ |
| 120 | + dnsCache: true, |
| 121 | + agent: { |
| 122 | + http: new http.Agent({ |
| 123 | + keepAlive: false, |
| 124 | + }), |
| 125 | + }, |
| 126 | + }); |
| 127 | + |
| 128 | + // First request: real DNS lookup |
| 129 | + const response1 = await instance(''); |
| 130 | + const firstDns = response1.timings.phases.dns; |
| 131 | + |
| 132 | + // First request should have some DNS time (for localhost lookup) |
| 133 | + // or 0 if it's fast enough to trigger the cache threshold |
| 134 | + t.true(firstDns! >= 0); |
| 135 | + |
| 136 | + // Subsequent requests: DNS should be cached |
| 137 | + const response2 = await instance(''); |
| 138 | + const response3 = await instance(''); |
| 139 | + |
| 140 | + // When DNS is cached, if lookup and connect happen at the exact same time (tcp=0), |
| 141 | + // then dns is set to 0 to indicate no actual DNS resolution occurred. |
| 142 | + // Otherwise, dns will be small but may vary on CI due to system load. |
| 143 | + // The key fix from http-timer #35 is that we handle this case, not enforce exact values. |
| 144 | + const secondIsInstant = response2.timings.phases.tcp === 0; |
| 145 | + const thirdIsInstant = response3.timings.phases.tcp === 0; |
| 146 | + |
| 147 | + if (secondIsInstant) { |
| 148 | + t.is(response2.timings.phases.dns, 0, 'instant cached DNS (tcp=0) should have dns=0'); |
| 149 | + } else { |
| 150 | + t.true(response2.timings.phases.dns! >= 0, 'cached DNS should have dns >= 0'); |
| 151 | + } |
| 152 | + |
| 153 | + if (thirdIsInstant) { |
| 154 | + t.is(response3.timings.phases.dns, 0, 'instant cached DNS (tcp=0) should have dns=0'); |
| 155 | + } else { |
| 156 | + t.true(response3.timings.phases.dns! >= 0, 'cached DNS should have dns >= 0'); |
| 157 | + } |
| 158 | +}); |
| 159 | + |
112 | 160 | test('redirect timings preserve connection timings from initial request', withServer, async (t, server, got) => { |
113 | 161 | // Set up a redirect chain to test socket reuse scenario |
114 | 162 | server.get('/', (_request, response) => { |
|
0 commit comments