A library to wait for a network connection
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

58 lines
1.2 KiB

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()
}