53 lines
1.5 KiB
Go
53 lines
1.5 KiB
Go
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 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)
|
|
}
|
|
}
|