Specs refactor and tweaks

- Make ParseOptions func to take an arg and return struct so we can test it
- Refactor and add more tests for options parsing
- Run test suite on postgres 10.x branch
- Change format for connection idle timer from float64 to in
This commit is contained in:
Dan Sosedoff
2018-02-22 14:20:18 -06:00
parent 6938eb5f35
commit 47500bf92e
8 changed files with 159 additions and 93 deletions

View File

@@ -367,7 +367,13 @@ func (client *Client) Close() error {
}
func (client *Client) IsIdle() bool {
return time.Since(client.lastQueryTime).Minutes() > command.Opts.ConnectionIdleTimeout
mins := int(time.Since(client.lastQueryTime).Minutes())
if command.Opts.ConnectionIdleTimeout > 0 {
return mins >= command.Opts.ConnectionIdleTimeout
}
return false
}
// Fetch all rows as strings for a single column

View File

@@ -2,12 +2,16 @@ package client
import (
"fmt"
"log"
"os"
"os/exec"
"runtime"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/sosedoff/pgweb/pkg/command"
)
var (
@@ -43,6 +47,11 @@ func getVar(name, def string) string {
}
func initVars() {
// We need to load default options to make sure all stuff works
if err := command.SetDefaultOptions(); err != nil {
log.Fatal(err)
}
serverHost = getVar("PGHOST", "localhost")
serverPort = getVar("PGPORT", "5432")
serverUser = getVar("PGUSER", "postgres")
@@ -148,6 +157,21 @@ func test_NewClientFromUrl2(t *testing.T) {
assert.Equal(t, url, client.ConnectionString)
}
func test_ClientIdleTime(t *testing.T) {
examples := map[time.Time]bool{
time.Now(): false, // Current time
time.Now().Add(time.Minute * -30): false, // 30 minutes ago
time.Now().Add(time.Minute * -240): true, // 240 minutes ago
time.Now().Add(time.Minute * 30): false, // 30 minutes in future
time.Now().Add(time.Minute * 128): false, // 128 minutes in future
}
for ts, expected := range examples {
testClient.lastQueryTime = ts
assert.Equal(t, expected, testClient.IsIdle())
}
}
func test_Test(t *testing.T) {
assert.Equal(t, nil, testClient.Test())
}
@@ -367,6 +391,7 @@ func TestAll(t *testing.T) {
setupClient()
test_NewClientFromUrl(t)
test_ClientIdleTime(t)
test_Test(t)
test_Info(t)
test_Activity(t)