Skip to content

Commit

Permalink
Add a basic idle timeout test
Browse files Browse the repository at this point in the history
  • Loading branch information
perk11 committed Oct 7, 2024
1 parent fa5c67c commit aa39ed8
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 8 deletions.
59 changes: 51 additions & 8 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,45 +25,82 @@ func connectOnly(test *testing.T, proxyAddress string) {
time.Sleep(1 * time.Second)
}

func minimal(test *testing.T, proxyAddress string) {
func idleTimeout(test *testing.T, proxyAddress string) {
pid := runReadPidCloseConnection(test, proxyAddress)
if pid == 0 {
//runReadPidCloseConnection already failed the test
return
}
secondPid := runReadPidCloseConnection(test, proxyAddress)
if secondPid != pid {
test.Errorf("pid is different during second connection")
return
}

time.Sleep(4 * time.Second)
if isProcessRunning(pid) {
test.Errorf("Process is still running after connections is closed and ShutDownAfterInactivitySeconds have passed")
return
}

thirdPid := runReadPidCloseConnection(test, proxyAddress)
if thirdPid == 0 {
return
}
if thirdPid == pid {
test.Errorf("pid during third connection is the same as during first connection ")
return
}

time.Sleep(4 * time.Second)
if isProcessRunning(pid) {
test.Errorf("Process is still running after connections is closed and ShutDownAfterInactivitySeconds have passed")
}
}
func runReadPidCloseConnection(test *testing.T, proxyAddress string) int {
conn, err := net.Dial("tcp", proxyAddress)
if err != nil {
test.Error(err)
return
return 0
}

buffer := make([]byte, 1024)
bytesRead, err := conn.Read(buffer)
if err != nil {
if err != io.EOF {
test.Error(err)
return
return 0
}
}
pidString := string(buffer[:bytesRead])
if !isNumeric(pidString) {
test.Errorf("value \"%s\" is not numeric, expected a pid", pidString)
return
return 0
}
pidInt, err := strconv.Atoi(pidString)
if err != nil {
test.Error(err, pidString)
return
return 0
}
if pidInt <= 0 {
test.Errorf("value \"%s\" is not a valid pid", pidString)
return
return 0
}
if !isProcessRunning(pidInt) {
test.Errorf("process \"%s\" is not running while connection is still open", pidString)
return
return 0
}

err = conn.Close()
if err != nil {
test.Error(err)
return
return 0
}

return pidInt
}
func minimal(test *testing.T, proxyAddress string) {
runReadPidCloseConnection(test, proxyAddress)
}

func isNumeric(s string) bool {
Expand Down Expand Up @@ -198,6 +235,12 @@ func TestAppScenarios(test *testing.T) {
"2006",
connectOnly,
},
{
"idle-timeout",
"test-server/idle-timeout.json",
"2007",
idleTimeout,
},
}

for _, testCase := range tests {
Expand Down
13 changes: 13 additions & 0 deletions test-server/idle-timeout.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"ShutDownAfterInactivitySeconds": 3,
"Services": [
{
"Name": "test-server_healthcheck-stuck",
"ListenPort": "2007",
"ProxyTargetHost": "localhost",
"ProxyTargetPort": "12007",
"Command": "test-server/test-server",
"Args": "-p 12007"
}
]
}

0 comments on commit aa39ed8

Please sign in to comment.