waitfor/waitfor_test.go

59 lines
1.2 KiB
Go
Raw Normal View History

2020-08-06 11:41:26 +02:00
package waitfor
import (
"net"
"testing"
"time"
"git.fuyu.moe/Fuyu/assert"
)
func TestDeadline(t *testing.T) {
assert := assert.New(t)
start := time.Now()
ok := Try(`tcp`, `127.0.0.1:65398`, time.Microsecond*100, time.Millisecond)
assert.False(ok)
assert.Eq(int64(1), time.Since(start).Milliseconds())
start = time.Now()
ok = Try(`tcp`, `127.0.0.1:65398`, time.Microsecond*100, 0)
assert.False(ok)
assert.Eq(int64(0), time.Since(start).Milliseconds())
}
func TestSuccess(t *testing.T) {
assert := assert.New(t)
conn, err := net.Listen(`tcp`, `127.0.0.1:65398`)
assert.NoError(err)
defer conn.Close()
start := time.Now()
ok := Try(`tcp`, `127.0.0.1:65398`, time.Microsecond*100, time.Millisecond*10)
assert.True(ok)
assert.True(time.Since(start).Milliseconds() < 10)
}
func TestInfiniteLimit(t *testing.T) {
assert := assert.New(t)
var conn net.Listener
go func() {
time.Sleep(time.Millisecond)
var err error
conn, err = net.Listen(`tcp`, `127.0.0.1:65398`)
assert.NoError(err)
}()
start := time.Now()
ok := Try(`tcp`, `127.0.0.1:65398`, time.Microsecond*100, time.Millisecond*-1)
assert.True(ok)
assert.True(time.Since(start).Milliseconds() < 2)
conn.Close()
}