Adding tests for instances

Minor modification to db code
This commit is contained in:
Farhan Khan 2021-08-08 23:46:29 +00:00
parent b231629e37
commit ad8b7d12a7
3 changed files with 81 additions and 1 deletions

View File

@ -10,7 +10,8 @@ var pool *pgxpool.Pool
func getDbPool() *pgxpool.Pool { func getDbPool() *pgxpool.Pool {
// Setup Database // 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 { if err != nil {
logFatal.Fatal("Unable to connect to database:", err) logFatal.Fatal("Unable to connect to database:", err)
} }

View File

@ -30,6 +30,8 @@ func DoTries(o *RunningInstance, req *http.Request) (*http.Response, error) {
} }
func BuildClient(endpoint string) http.Client { 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{ tr := &http.Transport{
MaxIdleConns: 2, MaxIdleConns: 2,
IdleConnTimeout: 3600 * time.Second, IdleConnTimeout: 3600 * time.Second,
@ -56,6 +58,7 @@ func BuildClient(endpoint string) http.Client {
} }
func GetRunner(endpoint string) (RunningInstance, bool) { func GetRunner(endpoint string) (RunningInstance, bool) {
// Tests: TestGetRunnerNonExist, TestGetRunnerExists
ri_mutex.Lock() ri_mutex.Lock()
o, exists := runninginstances[endpoint] o, exists := runninginstances[endpoint]
@ -73,6 +76,7 @@ func GetRunner(endpoint string) (RunningInstance, bool) {
} }
func UpdateRunner(endpoint string, o RunningInstance) { func UpdateRunner(endpoint string, o RunningInstance) {
// Tests: None necessary
ri_mutex.Lock() ri_mutex.Lock()
runninginstances[endpoint] = o runninginstances[endpoint] = o
ri_mutex.Unlock() ri_mutex.Unlock()

View 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")
// }
}