ws and pruner done!!!

This commit is contained in:
41666 2024-10-28 13:46:52 -07:00
parent c5cc245e25
commit 74add408e6
34 changed files with 1455 additions and 221 deletions

20
util/testutil/db.go Normal file
View file

@ -0,0 +1,20 @@
package testutil
import (
"database/sql"
"testing"
_ "modernc.org/sqlite"
)
// GetTestDB standardizes what type of DB tests use.
func GetTestDB(t *testing.T) *sql.DB {
t.Helper()
db, err := sql.Open("sqlite", t.TempDir()+"/test.db")
if err != nil {
t.Fatalf("test shim: sqlite open failed, %v", err)
}
return db
}

50
util/testutil/ws.go Normal file
View file

@ -0,0 +1,50 @@
package testutil
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/coder/websocket"
)
type MockESS struct {
Server *httptest.Server
LastMessage string
}
func (m MockESS) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
c, err := websocket.Accept(rw, req, nil)
if err != nil {
rw.WriteHeader(400)
rw.Write([]byte("websocket connection failed"))
return
}
defer c.CloseNow()
ctx, cancel := context.WithTimeout(req.Context(), time.Second*30)
defer cancel()
_, body, err := c.Read(ctx)
if err != nil {
rw.WriteHeader(500)
rw.Write([]byte("websocket read failed"))
return
}
m.LastMessage = string(body)
}
func GetMockESS(t *testing.T) MockESS {
t.Helper()
m := MockESS{}
s := httptest.NewServer(m)
m.Server = s
t.Cleanup(s.Close)
return m
}