ws and pruner done!!!
This commit is contained in:
parent
c5cc245e25
commit
74add408e6
34 changed files with 1455 additions and 221 deletions
22
util/db_connector.go
Normal file
22
util/db_connector.go
Normal file
|
@ -0,0 +1,22 @@
|
|||
package util
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
|
||||
_ "github.com/jackc/pgx/v5/stdlib"
|
||||
)
|
||||
|
||||
func GetDBConnection(addr string) (*sql.DB, error) {
|
||||
db, err := sql.Open("pgx", addr)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("db failed to open, %w", err)
|
||||
}
|
||||
|
||||
err = db.Ping()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("db failed to ping, %w", err)
|
||||
}
|
||||
|
||||
return db, nil
|
||||
}
|
20
util/testutil/db.go
Normal file
20
util/testutil/db.go
Normal 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
50
util/testutil/ws.go
Normal 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
|
||||
}
|
8
util/timestrings.go
Normal file
8
util/timestrings.go
Normal file
|
@ -0,0 +1,8 @@
|
|||
package util
|
||||
|
||||
import "time"
|
||||
|
||||
// Makes times compatible with old Saerro API
|
||||
func TimeToString(t time.Time) string {
|
||||
return t.UTC().Format(time.RFC3339)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue