本文整理匯總了Golang中github.com/getlantern/testify/assert.Error函數的典型用法代碼示例。如果您正苦於以下問題:Golang Error函數的具體用法?Golang Error怎麽用?Golang Error使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Error函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestTCP
func TestTCP(t *testing.T) {
// Lower maxAssertAttempts to keep this test from running too long
maxAssertAttempts = 2
l0, err := net.Listen("tcp", "localhost:0")
if err != nil {
t.Fatal(err)
}
defer func() {
if err := l0.Close(); err != nil {
t.Fatalf("Unable to close listener: %v", err)
}
}()
start, fdc, err := Matching("TCP")
if err != nil {
t.Fatal(err)
}
assert.Equal(t, 1, start, "Starting count should have been 1")
err = fdc.AssertDelta(0)
if err != nil {
t.Fatal(err)
}
assert.NoError(t, err, "Initial TCP count should be 0")
l, err := net.Listen("tcp", "localhost:0")
if err != nil {
t.Fatal(err)
}
_, middle, err := Matching("TCP")
if err != nil {
t.Fatal(err)
}
err = fdc.AssertDelta(0)
if assert.Error(t, err, "Asserting wrong count should fail") {
assert.Contains(t, err.Error(), "Expected 0, have 1")
assert.True(t, len(err.Error()) > 100)
}
err = fdc.AssertDelta(1)
assert.NoError(t, err, "Ending TCP count should be 1")
err = fdc.AssertDelta(0)
if assert.Error(t, err, "Asserting wrong count should fail") {
assert.Contains(t, err.Error(), "Expected 0, have 1")
assert.Contains(t, err.Error(), "New")
assert.True(t, len(err.Error()) > 100)
}
if err := l.Close(); err != nil {
t.Fatalf("Unable to close listener: %v", err)
}
err = middle.AssertDelta(0)
if assert.Error(t, err, "Asserting wrong count should fail") {
assert.Contains(t, err.Error(), "Expected 0, have -1")
assert.Contains(t, err.Error(), "Removed")
assert.True(t, len(err.Error()) > 100)
}
}
示例2: TestTranslate
func TestTranslate(t *testing.T) {
assertTranslation(t, "", "HELLO")
UseOSLocale()
assertTranslation(t, "I speak America English!", "ONLY_IN_EN_US")
assertTranslation(t, "I speak Generic English!", "ONLY_IN_EN")
assertTranslation(t, "", "NOT_EXISTED")
SetMessagesDir("not-existed-dir")
err := SetLocale("en_US")
assert.Error(t, err, "should error if dir is not existed")
SetMessagesDir("locale")
assert.Error(t, SetLocale("e0"), "should error on malformed locale")
assert.Error(t, SetLocale("e0-DO"), "should error on malformed locale")
assert.Error(t, SetLocale("e0-DO.C"), "should error on malformed locale")
assert.NoError(t, SetLocale("en"), "should change locale")
if assert.NoError(t, SetLocale("en_US"), "should change locale") {
// formatting
assertTranslation(t, "Hello An Argument!", "HELLO", "An Argument")
assertTranslation(t, "", "NOT_EXISTED", "extra args")
}
if assert.NoError(t, SetLocale("zh_CN"), "should change locale") {
assertTranslation(t, "An Argument你好!", "HELLO", "An Argument")
// fallbacks
assertTranslation(t, "I speak Mandarin!", "ONLY_IN_ZH_CN")
assertTranslation(t, "I speak Chinese!", "ONLY_IN_ZH")
assertTranslation(t, "I speak America English!", "ONLY_IN_EN_US")
assertTranslation(t, "I speak Generic English!", "ONLY_IN_EN")
}
}
示例3: TestWaitUntilNoneMatchTimeout
func TestWaitUntilNoneMatchTimeout(t *testing.T) {
conn, err := net.Dial("tcp", "www.google.com:80")
if err != nil {
t.Fatalf("Unable to dial google: %v", err)
}
defer func() {
if err := conn.Close(); err != nil {
fmt.Println("Unable to close connection: %v", err)
}
}()
wait := 500 * time.Millisecond
start := time.Now()
go func() {
time.Sleep(wait)
if err := conn.Close(); err != nil {
t.Fatalf("Unable to close connection: %v", err)
}
}()
err = WaitUntilNoneMatch("TCP", wait/2)
elapsed := time.Now().Sub(start)
assert.Error(t, err, "Waiting should have failed")
assert.True(t, elapsed < wait, "Should have waited less than time to close conn")
}
示例4: TestBadConnectStatus
func TestBadConnectStatus(t *testing.T) {
l, err := net.Listen("tcp", "localhost:0")
if err != nil {
t.Fatalf("Unable to listen: %s", err)
}
hs := &http.Server{
Handler: http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
resp.WriteHeader(403) // forbidden
}),
}
go func() {
if err := hs.Serve(l); err != nil {
t.Fatalf("Unable to serve: %v", err)
}
}()
dialer := NewDialer(Config{
DialServer: func() (net.Conn, error) {
return net.Dial("tcp", l.Addr().String())
},
})
_, err = dialer.Dial("tcp", "www.google.com")
assert.Error(t, err, "Dialing a server that sends a non-successful HTTP status to our CONNECT request should have failed")
}
示例5: doTestNonGlobalAddress
func doTestNonGlobalAddress(t *testing.T, overrideAddr string) {
l := startServer(t, false, nil)
d := dialerFor(t, l, 0)
defer d.Close()
gotConn := false
var gotConnMutex sync.Mutex
tl, err := net.Listen("tcp", "localhost:0")
if err != nil {
t.Fatalf("Unable to listen: %s", err)
}
go func() {
tl.Accept()
gotConnMutex.Lock()
gotConn = true
gotConnMutex.Unlock()
}()
addr := tl.Addr().String()
if overrideAddr != "" {
addr = overrideAddr
}
conn, err := d.Dial("tcp", addr)
if err != nil {
t.Fatalf("Unable to dial %v: %v", addr, err)
}
data := []byte("Some Meaningless Data")
conn.Write(data)
// Give enproxy time to flush
time.Sleep(500 * time.Millisecond)
_, err = conn.Write(data)
assert.Error(t, err, "Sending data after previous attempt to write to local address should have failed")
assert.False(t, gotConn, "Sending data to local address should never have resulted in connection")
}
示例6: TestIdleClientConnections
func TestIdleClientConnections(t *testing.T) {
limitedServer, err := setupNewHTTPServer(0, 100*time.Millisecond)
if err != nil {
assert.Fail(t, "Error starting proxy server")
}
okFn := func(conn net.Conn, proxy *server.Server, originURL *url.URL) {
time.Sleep(time.Millisecond * 90)
conn.Write([]byte("GET / HTTP/1.1\r\nHost: www.google.com\r\n\r\n"))
var buf [400]byte
_, err := conn.Read(buf[:])
assert.NoError(t, err)
}
idleFn := func(conn net.Conn, proxy *server.Server, originURL *url.URL) {
time.Sleep(time.Millisecond * 110)
conn.Write([]byte("GET / HTTP/1.1\r\nHost: www.google.com\r\n\r\n"))
var buf [400]byte
_, err := conn.Read(buf[:])
assert.Error(t, err)
}
go testRoundTrip(t, limitedServer, httpOriginServer, okFn)
testRoundTrip(t, limitedServer, httpOriginServer, idleFn)
}
示例7: TestTimeout
func TestTimeout(t *testing.T) {
offer := Offer(1 * time.Millisecond)
_, err := offer.FiveTuple()
assert.Error(t, err, "There should be an error")
if err != nil {
assert.Contains(t, err.Error(), "Timed out", "Error should mention timing out")
}
}
示例8: TestReadFailure
func TestReadFailure(t *testing.T) {
id1 := Random()
b := make([]byte, EncodedLength)
err := id1.Write(b)
assert.NoError(t, err, "Unable to write")
_, err = Read(b[:EncodedLength-1])
assert.Error(t, err, "Read should have failed")
}
示例9: TestBadEnproxyConn
func TestBadEnproxyConn(t *testing.T) {
d := NewDialer(Config{
Host: "localhost",
Port: 3253,
})
_, err := d.Dial("tcp", "www.google.com")
assert.Error(t, err, "Dialing using a non-existent host should have failed")
}
示例10: TestDialFailure
func TestDialFailure(t *testing.T) {
fail := int32(1)
dialAttempts := int32(0)
addr, err := startTestServer()
if err != nil {
t.Fatalf("Unable to start test server: %s", err)
}
_, fdc, err := fdcount.Matching("TCP")
if err != nil {
t.Fatal(err)
}
poolSize := 10
p := New(Config{
Size: poolSize,
Dial: func() (net.Conn, error) {
atomic.AddInt32(&dialAttempts, 1)
if fail == int32(1) {
return nil, fmt.Errorf("I'm failing intentionally!")
}
return net.DialTimeout("tcp", addr, 15*time.Millisecond)
},
})
// Try to get connection, make sure it fails
conn, err := p.Get()
if !assert.Error(t, err, "Dialing should have failed") {
if err := conn.Close(); err != nil {
t.Fatalf("Unable to close connection: %v", err)
}
}
// Wait for fill to run for a while with a failing connection
time.Sleep(1 * time.Second)
assert.Equal(t, 1, atomic.LoadInt32(&dialAttempts), fmt.Sprintf("There should have been only 1 dial attempt"))
assert.NoError(t, fdc.AssertDelta(0), "There should be no additional file descriptors open")
// Now make connection succeed and verify that it works
atomic.StoreInt32(&fail, 0)
time.Sleep(100 * time.Millisecond)
connectAndRead(t, p, 1)
time.Sleep(fillTime)
log.Debug("Testing")
assert.NoError(t, fdc.AssertDelta(10), "Pool should have filled")
// Now make the connection fail again so that when we stop, we're stopping
// while failing (tests a different code path for stopping)
atomic.StoreInt32(&fail, 1)
time.Sleep(100 * time.Millisecond)
p.Close()
assert.NoError(t, fdc.AssertDelta(0), "All connections should be closed")
}
示例11: TestBadProtocol
func TestBadProtocol(t *testing.T) {
dialer := NewDialer(Config{
DialServer: func() (net.Conn, error) {
return net.Dial("tcp", "www.google.com")
},
})
_, err := dialer.Dial("udp", "www.google.com")
assert.Error(t, err, "Dialing with a non-tcp protocol should have failed")
}
示例12: TestHttpClientWithBadEnproxyConn
func TestHttpClientWithBadEnproxyConn(t *testing.T) {
d := NewDialer(Config{
Host: "localhost",
Port: 3253,
})
hc := d.HttpClientUsing(nil)
_, err := hc.Get("http://www.google.com/humans.txt")
assert.Error(t, err, "HttpClient using a non-existent host should have failed")
}
示例13: doTestTimeout
func doTestTimeout(t *testing.T, timeout time.Duration) {
_, err := DialWithDialer(&net.Dialer{
Timeout: timeout,
}, "tcp", ADDR, false, nil)
assert.Error(t, err, "There should have been a problem dialing", timeout)
if err != nil {
assert.True(t, err.(net.Error).Timeout(), "Dial error should be timeout", timeout)
}
}
示例14: TestBadDialServer
func TestBadDialServer(t *testing.T) {
dialer := NewDialer(Config{
DialServer: func() (net.Conn, error) {
return nil, fmt.Errorf("I refuse to dial")
},
})
_, err := dialer.Dial("tcp", "www.google.com")
assert.Error(t, err, "Dialing with a bad DialServer function should have failed")
}
示例15: TestNotOKWithServerName
func TestNotOKWithServerName(t *testing.T) {
fdStart := countTCPFiles()
conn, err := Dial("tcp", ADDR, true, nil)
assert.Error(t, err, "There should have been a problem dialing")
if err != nil {
assert.Contains(t, err.Error(), CERTIFICATE_ERROR, "Wrong error on dial")
}
<-receivedServerNames
closeAndCountFDs(t, conn, err, fdStart)
}