Naming, layout and comments: descriptive full-word names, one item per line in wrapped calls and literals, and doc comments on exported names

Short and single-letter names in application code become full words (tests
keep theirs). Wrapped calls, signatures and composite literals put one item
per line, and one-line function bodies are expanded. Exported names and
groups of constants get doc comments, and comments refer to the renamed
identifiers.
This commit is contained in:
bwilliams committed 2026-10-15 16:12:56 +01:00
1 parent 043e307663
commit 465358609a
78 files changed
+2374 -1402

No files matched your search

+51 -13
View File
@@ -23,7 +23,11 @@ type fakeConn struct {
closed chan ws.StatusCode
}
func newFakeConn() *fakeConn { return &fakeConn{closed: make(chan ws.StatusCode, 1)} }
func newFakeConn() *fakeConn {
return &fakeConn{
closed: make(chan ws.StatusCode, 1),
}
}
func (f *fakeConn) Read(ctx context.Context) (ws.MessageType, []byte, error) {
<-ctx.Done()
@@ -52,7 +56,10 @@ func newTestClient(f *fakeConn) (*client, context.Context) {
// An envelope that can't be marshalled is dropped, not queued.
func TestSendEnvelopeBadPayload(t *testing.T) {
c, _ := newTestClient(newFakeConn())
c.sendEnvelope(Envelope{Type: TypeEcho, Payload: json.RawMessage("{")})
c.sendEnvelope(Envelope{
Type: TypeEcho,
Payload: json.RawMessage("{"),
})
if len(c.send) != 0 {
t.Errorf("queued %d messages; want none", len(c.send))
}
@@ -63,7 +70,9 @@ func TestSendEnvelopeSlowConsumer(t *testing.T) {
f := newFakeConn()
c, ctx := newTestClient(f)
for range clientSendBuffer {
c.sendEnvelope(Envelope{Type: TypePong})
c.sendEnvelope(Envelope{
Type: TypePong,
})
}
select {
case code := <-f.closed:
@@ -71,7 +80,9 @@ func TestSendEnvelopeSlowConsumer(t *testing.T) {
default:
}
c.sendEnvelope(Envelope{Type: TypePong})
c.sendEnvelope(Envelope{
Type: TypePong,
})
select {
case code := <-f.closed:
if code != ws.StatusPolicyViolation {
@@ -109,13 +120,31 @@ func TestClientMessageRateLimit(t *testing.T) {
rule config.JSCRateLimitRule
limited bool
}{
{config.JSCRateLimitRule{Enabled: false, PerSecond: 1, Burst: 1}, false},
{config.JSCRateLimitRule{Enabled: true, PerSecond: 0, Burst: 1}, false},
{config.JSCRateLimitRule{Enabled: true, PerSecond: 1, Burst: 1}, true},
{config.JSCRateLimitRule{
Enabled: false,
PerSecond: 1,
Burst: 1,
}, false},
{config.JSCRateLimitRule{
Enabled: true,
PerSecond: 0,
Burst: 1,
}, false},
{config.JSCRateLimitRule{
Enabled: true,
PerSecond: 1,
Burst: 1,
}, true},
}
for _, tt := range tests {
h := NewHub(config.JSCServerWebSocket{MessageRateLimit: tt.rule}, WithLogger(discardLog))
c := newClient(h, newFakeConn(), "", func() {})
h := NewHub(
config.JSCServerWebSocket{
MessageRateLimit: tt.rule,
},
WithLogger(discardLog),
)
c := newClient(h, newFakeConn(), "", func() {
})
c.limiter.Allow()
if limited := !c.limiter.Allow(); limited != tt.limited {
t.Errorf("rule %+v: second message limited = %v; want %v", tt.rule, limited, tt.limited)
@@ -133,7 +162,8 @@ func TestMustRaw(t *testing.T) {
{"nil", nil, ""},
{"raw message as is", raw, `{"a":1}`},
{"marshalled", map[string]int{"b": 2}, `{"b":2}`},
{"unmarshallable", func() {}, ""},
{"unmarshallable", func() {
}, ""},
}
for _, tt := range tests {
if got := string(mustRaw(tt.v)); got != tt.want {
@@ -187,13 +217,20 @@ func (b *syncBuffer) entries(t *testing.T) []map[string]any {
// buffer.
func jsonLog(level slog.Level) (*slog.Logger, *syncBuffer) {
buf := &syncBuffer{}
return slog.New(slog.NewJSONHandler(buf, &slog.HandlerOptions{Level: level})), buf
return slog.New(slog.NewJSONHandler(buf, &slog.HandlerOptions{
Level: level,
})), buf
}
// At debug level, messages received from and sent to a client are logged.
func TestLogMessages(t *testing.T) {
log, buf := jsonLog(slog.LevelDebug)
h := NewHub(config.JSCServerWebSocket{WriteTimeout: time.Second}, WithLogger(log))
h := NewHub(
config.JSCServerWebSocket{
WriteTimeout: time.Second,
},
WithLogger(log),
)
peer, _ := pipeClient(t, h)
ping := `{"type":"ping","id":"1"}`
@@ -236,7 +273,8 @@ func TestLogMessages(t *testing.T) {
func TestLogMessageLimit(t *testing.T) {
log, buf := jsonLog(slog.LevelDebug)
h := NewHub(config.JSCServerWebSocket{}, WithLogger(log))
c := newClient(h, newFakeConn(), "", func() {})
c := newClient(h, newFakeConn(), "", func() {
})
long := strings.Repeat("x", logMessageLimit+100)
c.logMessage(context.Background(), "message sent", []byte(long))