From ad8b7d12a72f31b71581987d705a7a0fabcf0b4c Mon Sep 17 00:00:00 2001 From: Farhan Khan Date: Sun, 8 Aug 2021 23:46:29 +0000 Subject: [PATCH] Adding tests for instances Minor modification to db code --- fedilogue/db.go | 3 +- fedilogue/instance.go | 4 ++ fedilogue/instance_test.go | 75 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 fedilogue/instance_test.go diff --git a/fedilogue/db.go b/fedilogue/db.go index eca817b..b8e8b38 100644 --- a/fedilogue/db.go +++ b/fedilogue/db.go @@ -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) } diff --git a/fedilogue/instance.go b/fedilogue/instance.go index b279d49..2bb34ab 100644 --- a/fedilogue/instance.go +++ b/fedilogue/instance.go @@ -30,6 +30,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, @@ -56,6 +58,7 @@ func BuildClient(endpoint string) http.Client { } func GetRunner(endpoint string) (RunningInstance, bool) { + // Tests: TestGetRunnerNonExist, TestGetRunnerExists ri_mutex.Lock() o, exists := runninginstances[endpoint] @@ -73,6 +76,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() diff --git a/fedilogue/instance_test.go b/fedilogue/instance_test.go new file mode 100644 index 0000000..474441c --- /dev/null +++ b/fedilogue/instance_test.go @@ -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") +// } +}