TestServerKeepaliveDefaults 仅做编译/默认填充层面的 sanity check: 调用 ListenAndServe 的代码路径会在所有 keepalive 字段为 0 时填充默认值。 这里直接测 reapOnce 的边界(threshold=now-DeadConnectionTimeout)。 关于 KeepaliveEnforcementPolicy 拒绝过频 ping 的验证:构造 grpc client 用 30ms 间隔 ping 较复杂(需要直接构造 transport-level conn),且 EnforcementPolicy 行为已由 grp
(t *testing.T)
| 104 | // 用 30ms 间隔 ping 较复杂(需要直接构造 transport-level conn),且 |
| 105 | // EnforcementPolicy 行为已由 grpc-go 自身测试覆盖;此处仅做注释说明。 |
| 106 | func TestReaper_thresholdBoundary(t *testing.T) { |
| 107 | hub := New(Options{ |
| 108 | DeadConnectionTimeout: 50 * time.Millisecond, |
| 109 | ReaperInterval: time.Hour, // 不让自动 reaper 干扰 |
| 110 | }) |
| 111 | hub.metrics.markSeen("fresh") |
| 112 | time.Sleep(60 * time.Millisecond) |
| 113 | hub.metrics.markSeen("alive") |
| 114 | |
| 115 | // 手动调一次 reapOnce:fresh 已超时,但它没有 conns 项,所以不会 panic; |
| 116 | // alive 没超时,不会进 stale 列表。 |
| 117 | hub.reapOnce(time.Now()) |
| 118 | // 没有 panic 即通过。 |
| 119 | } |