mirror of
https://github.com/binwiederhier/ntfy.git
synced 2025-05-28 09:25:37 +02:00
Tests for cliet package
This commit is contained in:
parent
6a7e9071b6
commit
fe5734d9f0
8 changed files with 162 additions and 43 deletions
test
44
test/util.go
Normal file
44
test/util.go
Normal file
|
@ -0,0 +1,44 @@
|
|||
package test
|
||||
|
||||
import (
|
||||
"net"
|
||||
"strconv"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// WaitForPortUp waits up to 7s for a port to come up and fails t if that fails
|
||||
func WaitForPortUp(t *testing.T, port int) {
|
||||
success := false
|
||||
for i := 0; i < 500; i++ {
|
||||
startTime := time.Now()
|
||||
conn, _ := net.DialTimeout("tcp", net.JoinHostPort("127.0.0.1", strconv.Itoa(port)), 10*time.Millisecond)
|
||||
if conn != nil {
|
||||
success = true
|
||||
conn.Close()
|
||||
break
|
||||
}
|
||||
if time.Since(startTime) < 10*time.Millisecond {
|
||||
time.Sleep(10*time.Millisecond - time.Since(startTime))
|
||||
}
|
||||
}
|
||||
if !success {
|
||||
t.Fatalf("Failed waiting for port %d to be UP", port)
|
||||
}
|
||||
}
|
||||
|
||||
// WaitForPortDown waits up to 5s for a port to come down and fails t if that fails
|
||||
func WaitForPortDown(t *testing.T, port int) {
|
||||
success := false
|
||||
for i := 0; i < 100; i++ {
|
||||
conn, _ := net.DialTimeout("tcp", net.JoinHostPort("", strconv.Itoa(port)), 50*time.Millisecond)
|
||||
if conn == nil {
|
||||
success = true
|
||||
break
|
||||
}
|
||||
conn.Close()
|
||||
}
|
||||
if !success {
|
||||
t.Fatalf("Failed waiting for port %d to be DOWN", port)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue