Adding tests for instances
Minor modification to db code
This commit is contained in:
parent
b99ad6da99
commit
6776cd28e5
@ -10,7 +10,8 @@ var pool *pgxpool.Pool
|
||||
|
||||
func getDbPool() *pgxpool.Pool {
|
||||
// Setup Database
|
||||
pool, err := pgxpool.Connect(context.Background(), os.Getenv("DATABASE_URL"))
|
||||
dburl := os.Getenv("DATABASE_URL")
|
||||
pool, err := pgxpool.Connect(context.Background(), dburl)
|
||||
if err != nil {
|
||||
logFatal.Fatal("Unable to connect to database:", err)
|
||||
}
|
||||
|
@ -31,6 +31,8 @@ func DoTries(o *RunningInstance, req *http.Request) (*http.Response, error) {
|
||||
}
|
||||
|
||||
func BuildClient(endpoint string) http.Client {
|
||||
// Test: TestBuildClient, TestBuildClientProxy
|
||||
/* The seemingly unused 'endpoint' variable is for proxying based on endpoint, ie for Tor */
|
||||
tr := &http.Transport{
|
||||
MaxIdleConns: 2,
|
||||
IdleConnTimeout: 3600 * time.Second,
|
||||
@ -57,6 +59,7 @@ func BuildClient(endpoint string) http.Client {
|
||||
}
|
||||
|
||||
func GetRunner(endpoint string) (RunningInstance, bool) {
|
||||
// Tests: TestGetRunnerNonExist, TestGetRunnerExists
|
||||
ri_mutex.Lock()
|
||||
o, exists := runninginstances[endpoint]
|
||||
|
||||
@ -74,6 +77,7 @@ func GetRunner(endpoint string) (RunningInstance, bool) {
|
||||
}
|
||||
|
||||
func UpdateRunner(endpoint string, o RunningInstance) {
|
||||
// Tests: None necessary
|
||||
ri_mutex.Lock()
|
||||
runninginstances[endpoint] = o
|
||||
ri_mutex.Unlock()
|
||||
|
75
fedilogue/instance_test.go
Normal file
75
fedilogue/instance_test.go
Normal file
@ -0,0 +1,75 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"net/http"
|
||||
"testing"
|
||||
"time"
|
||||
"net"
|
||||
)
|
||||
|
||||
func TestBuildClient(t *testing.T) {
|
||||
tr := &http.Transport{
|
||||
MaxIdleConns: 2,
|
||||
IdleConnTimeout: 3600 * time.Second,
|
||||
DialContext: (&net.Dialer{
|
||||
Timeout: 30 * time.Second,
|
||||
KeepAlive: 30 * time.Second,
|
||||
DualStack: true,
|
||||
}).DialContext,
|
||||
}
|
||||
want := http.Client{Transport: tr}
|
||||
have := BuildClient("testdomain.com")
|
||||
|
||||
if reflect.DeepEqual(want, have) {
|
||||
t.Fatalf("TestBuildClient client different from expected.")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildClientProxy(t *testing.T) {
|
||||
// Currently not implemented
|
||||
}
|
||||
|
||||
func TestGetRunnerNonExist(t *testing.T) {
|
||||
defer func() {
|
||||
runninginstances = map[string]RunningInstance{}
|
||||
}()
|
||||
want_o := RunningInstance{}
|
||||
want_o.client = BuildClient("some-non-existent-domain.tld")
|
||||
want_o.Status = KEEPALIVE
|
||||
want_o.recentactivities = newUniqueFifo(10)
|
||||
want_o.recentactors = newUniqueFifo(10)
|
||||
want_exists := false
|
||||
have_o, have_exists := GetRunner("some-non-existent-domain.tld")
|
||||
|
||||
if reflect.DeepEqual(want_o, have_o) {
|
||||
t.Fatalf("TestGetRunnerBlank expected asfasfsf")
|
||||
}
|
||||
|
||||
if have_exists != false {
|
||||
t.Fatalf("TestGetRunnerBlank expected %v, got %v", want_exists, have_exists)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetRunnerExists(t *testing.T) {
|
||||
defer func() {
|
||||
runninginstances = map[string]RunningInstance{}
|
||||
}()
|
||||
|
||||
want_o := RunningInstance{}
|
||||
want_o.client = BuildClient("some-non-existent-domain.tld")
|
||||
want_o.Status = KEEPALIVE
|
||||
want_o.recentactivities = newUniqueFifo(10)
|
||||
want_o.recentactors = newUniqueFifo(10)
|
||||
runninginstances["some-non-existent-domain.tld"] = want_o
|
||||
|
||||
want_exists := true
|
||||
_, have_exists := GetRunner("some-non-existent-domain.tld")
|
||||
|
||||
if have_exists != want_exists {
|
||||
t.Fatalf("TestGetRunnerBlank expected %v, got %v", want_exists, have_exists)
|
||||
}
|
||||
// if reflect.DeepEqual(want_o, have_o) {
|
||||
// t.Fatalf("TestGetRunnerExists failed, should have the same value")
|
||||
// }
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user