package main import ( "fmt" "io" "net" "os" "syscall" "testing" "github.com/gorilla/websocket" ) func TestClassifyWSError(t *testing.T) { tests := []struct { name string err error want string }{ {name: "none", want: "none"}, {name: "normal close", err: &websocket.CloseError{Code: websocket.CloseNormalClosure}, want: "close_normal"}, {name: "abnormal close", err: &websocket.CloseError{Code: websocket.CloseAbnormalClosure}, want: "close_abnormal"}, {name: "timeout", err: &net.OpError{Op: "write", Err: os.ErrDeadlineExceeded}, want: "timeout"}, {name: "reset", err: fmt.Errorf("write failed: %w", syscall.ECONNRESET), want: "connection_reset"}, {name: "broken pipe", err: fmt.Errorf("write failed: %w", syscall.EPIPE), want: "broken_pipe"}, {name: "closed", err: net.ErrClosed, want: "closed"}, {name: "eof", err: io.EOF, want: "eof"}, {name: "other", err: fmt.Errorf("unknown"), want: "other"}, } for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { if got := classifyWSError(tc.err); got != tc.want { t.Fatalf("classifyWSError()=%q want=%q", got, tc.want) } }) } } func TestShouldLogWSWriteError(t *testing.T) { tests := []struct { reason string want bool }{ {reason: "close_normal", want: false}, {reason: "close_going_away", want: false}, {reason: "closed", want: false}, {reason: "eof", want: false}, {reason: "broken_pipe", want: false}, {reason: "connection_reset", want: false}, {reason: "timeout", want: true}, {reason: "close_abnormal", want: true}, {reason: "other", want: true}, } for _, tc := range tests { t.Run(tc.reason, func(t *testing.T) { if got := shouldLogWSWriteError(tc.reason); got != tc.want { t.Fatalf("shouldLogWSWriteError(%q)=%v want %v", tc.reason, got, tc.want) } }) } } func TestWebSocketOutboundQueueTiming(t *testing.T) { sess := &Session{proto: WEBSOCK, send: make(chan any, 2)} payload := []byte("message") if !sess.queueOutBytes(payload) { t.Fatal("queueOutBytes rejected payload") } queued := <-sess.send sess.observeOutboundDequeue(false) if string(queued.([]byte)) != string(payload) { t.Fatalf("payload=%q want=%q", queued, payload) } }