本文整理匯總了Golang中github.com/getlantern/testify/assert.True函數的典型用法代碼示例。如果您正苦於以下問題:Golang True函數的具體用法?Golang True怎麽用?Golang True使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了True函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestEnsureRegistered
func TestEnsureRegistered(t *testing.T) {
_, counter, err := fdcount.Matching("TCP")
if err != nil {
t.Fatalf("Unable to get starting fdcount: %v", err)
}
u := getUtil()
// Test with no existing record
name, ip := "cfl-test-entry", "127.0.0.1"
rec, proxying, err := u.EnsureRegistered(name, ip, nil)
if assert.NoError(t, err, "Should be able to register with no record") {
assert.NotNil(t, rec, "A new record should have been returned")
assert.True(t, proxying, "Proxying (orange cloud) should be on")
}
// Test with existing record, but not passing it in
rec, proxying, err = u.EnsureRegistered(name, ip, nil)
if assert.NoError(t, err, "Should be able to register with unspecified existing record") {
assert.NotNil(t, rec, "Existing record should have been returned")
assert.True(t, proxying, "Proxying (orange cloud) should be on")
// Test with existing record, passing it in
rec, proxying, err = u.EnsureRegistered(name, ip, rec)
if assert.NoError(t, err, "Should be able to register with specified existing record") {
assert.NotNil(t, rec, "Existing record should have been returned")
assert.True(t, proxying, "Proxying (orange cloud) should be on")
}
}
if rec != nil {
err := u.DestroyRecord(rec)
assert.NoError(t, err, "Should be able to destroy record")
}
assert.NoError(t, counter.AssertDelta(0), "All file descriptors should have been closed")
}
示例2: doTestTLS
func doTestTLS(buffered bool, t *testing.T) {
startServers(t, false)
_, counter, err := fdcount.Matching("TCP")
if err != nil {
t.Fatalf("Unable to get fdcount: %v", err)
}
conn, err := prepareConn(httpsAddr, buffered, false, t, nil)
if err != nil {
t.Fatalf("Unable to prepareConn: %s", err)
}
tlsConn := tls.Client(conn, &tls.Config{
ServerName: "localhost",
RootCAs: cert.PoolContainingCert(),
})
defer func() {
err := conn.Close()
assert.Nil(t, err, "Closing conn should succeed")
if !assert.NoError(t, counter.AssertDelta(2), "All file descriptors except the connection from proxy to destination site should have been closed") {
DumpConnTrace()
}
}()
err = tlsConn.Handshake()
if err != nil {
t.Fatalf("Unable to handshake: %s", err)
}
doRequests(tlsConn, t)
assert.True(t, destsSent[httpsAddr], "https address wasn't recorded as sent destination")
assert.True(t, destsReceived[httpsAddr], "https address wasn't recorded as received destination")
}
示例3: 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)
}
}
示例4: doTestPlainText
func doTestPlainText(buffered bool, useHostFn bool, t *testing.T) {
var counter *fdcount.Counter
var err error
startServers(t, useHostFn)
err = fdcount.WaitUntilNoneMatch("CLOSE_WAIT", 5*time.Second)
if err != nil {
t.Fatalf("Unable to wait until no more connections are in CLOSE_WAIT: %v", err)
}
_, counter, err = fdcount.Matching("TCP")
if err != nil {
t.Fatalf("Unable to get fdcount: %v", err)
}
var reportedHost string
var reportedHostMutex sync.Mutex
onResponse := func(resp *http.Response) {
reportedHostMutex.Lock()
reportedHost = resp.Header.Get(X_ENPROXY_PROXY_HOST)
reportedHostMutex.Unlock()
}
conn, err := prepareConn(httpAddr, buffered, false, t, onResponse)
if err != nil {
t.Fatalf("Unable to prepareConn: %s", err)
}
defer func() {
err := conn.Close()
assert.Nil(t, err, "Closing conn should succeed")
if !assert.NoError(t, counter.AssertDelta(2), "All file descriptors except the connection from proxy to destination site should have been closed") {
DumpConnTrace()
}
}()
doRequests(conn, t)
assert.Equal(t, 208, bytesReceived, "Wrong number of bytes received")
assert.Equal(t, 284, bytesSent, "Wrong number of bytes sent")
assert.True(t, destsSent[httpAddr], "http address wasn't recorded as sent destination")
assert.True(t, destsReceived[httpAddr], "http address wasn't recorded as received destination")
reportedHostMutex.Lock()
rh := reportedHost
reportedHostMutex.Unlock()
assert.Equal(t, "localhost", rh, "Didn't get correct reported host")
}
示例5: TestConcurrent
func TestConcurrent(t *testing.T) {
v := NewValue()
var sets int32 = 0
go func() {
var wg sync.WaitGroup
wg.Add(1)
// Do some concurrent setting to make sure that it works
for i := 0; i < concurrency; i++ {
go func() {
// Wait for waitGroup so that all goroutines run at basically the same
// time.
wg.Wait()
v.Set("hi")
atomic.AddInt32(&sets, 1)
}()
}
wg.Done()
}()
time.Sleep(50 * time.Millisecond)
r, ok := v.Get(20 * time.Millisecond)
assert.True(t, ok, "Get should have succeed")
assert.Equal(t, "hi", r, "Wrong result")
assert.Equal(t, concurrency, atomic.LoadInt32(&sets), "Wrong number of successful Sets")
}
示例6: TestCreateAndRefresh
func TestCreateAndRefresh(t *testing.T) {
if true {
t.Log("We don't currently use peerscanner, so this test is disabled. To reenable, we'll need to delete test distributions to avoid hitting our limit")
return
}
_, counter, err := fdcount.Matching("TCP")
if err != nil {
t.Fatalf("Unable to get starting fdcount: %v", err)
}
cfr := getCfr()
// Deleting cloudfront distributions is actually quite an involved process.
// Fortunately, distributions per se cost us nothing. A separate service
// will be implemented to delete test and otherwise unused distributions.
name := uuid.NewV4().String()
dist, err := CreateDistribution(cfr, name, name+"-grey.flashlightproxy.org", COMMENT)
assert.NoError(t, err, "Should be able to create distribution")
assert.Equal(t, "InProgress", dist.Status, "New distribution should have Status: \"InProgress\"")
assert.Equal(t, dist.Comment, COMMENT, "New distribution should have the comment we've set for it")
assert.Equal(t, name, dist.InstanceId, "New distribution should have the right InstanceId")
assert.True(t, strings.HasSuffix(dist.Domain, ".cloudfront.net"), "Domain should be a .cloudfront.net subdomain, not '"+dist.Domain+"'")
dist.Status = "modified to check it really gets overwritten"
err = RefreshStatus(cfr, dist)
assert.NoError(t, err, "Should be able to refresh status")
// Just check that Status stays a valid one. Checking that it eventually
// gets refreshed to "Deployed" would take a few minutes, and thus is out
// of the scope of this unit test.
assert.Equal(t, "InProgress", dist.Status, "New distribution should have Status: \"InProgress\" even after refreshing right away")
assert.NoError(t, counter.AssertDelta(0), "All file descriptors should have been closed")
}
示例7: TestEquals
func TestEquals(t *testing.T) {
a := csFor(&Config{
Cloud: []string{"A", "B", "C"},
Delta: &Delta{
Additions: []string{"D"},
Deletions: []string{"C"},
},
})
b := csFor(&Config{
Cloud: []string{"A", "B"},
Delta: &Delta{
Additions: []string{"D"},
Deletions: []string{"C"},
},
})
c := csFor(&Config{
Cloud: []string{"A", "B", "C"},
Delta: &Delta{
Additions: []string{"D", "E"},
Deletions: []string{"C"},
},
})
d := csFor(&Config{
Cloud: []string{"A", "B", "C"},
Delta: &Delta{
Additions: []string{"D"},
Deletions: []string{"C", "E"},
},
})
assert.True(t, a.equals(a), "a should equal itself")
assert.False(t, a.equals(b), "a should not equal b")
assert.False(t, a.equals(c), "a should not equal c")
assert.False(t, a.equals(d), "a should not equal d")
}
示例8: TestWaitUntilNoneMatchOK
func TestWaitUntilNoneMatchOK(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 := 250 * 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.NoError(t, err, "Waiting should have succeeded")
assert.True(t, elapsed >= wait, "Should have waited a while")
}
示例9: 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.Printf("Unable to close connection: %v", err)
}
}()
wait := 200 * 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/4)
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")
}
示例10: TestBadMethodToServer
func TestBadMethodToServer(t *testing.T) {
l := startServer(t)
resp, err := http.Get("http://" + l.Addr().String() + "/")
assert.NoError(t, err, "Making a Get request to the server should not have errored")
if err == nil {
assert.True(t, resp.StatusCode == 405, "Response should have indicated a bad method")
}
}
示例11: assertTimeoutError
func assertTimeoutError(t *testing.T, err error) {
switch e := err.(type) {
case net.Error:
assert.True(t, e.Timeout(), "Error should be timeout")
default:
assert.Fail(t, "Error should be net.Error")
}
}
示例12: 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)
}
}
示例13: TestIntegration
// TestIntegration tests against existing domain-fronted servers running on
// CloudFlare.
func TestIntegration(t *testing.T) {
dialedDomain := ""
dialedAddr := ""
actualResolutionTime := time.Duration(0)
actualConnectTime := time.Duration(0)
actualHandshakeTime := time.Duration(0)
var statsMutex sync.Mutex
statsFunc := func(success bool, domain, addr string, resolutionTime, connectTime, handshakeTime time.Duration) {
if success {
statsMutex.Lock()
defer statsMutex.Unlock()
dialedDomain = domain
dialedAddr = addr
actualResolutionTime = resolutionTime
actualConnectTime = connectTime
actualHandshakeTime = handshakeTime
}
}
d := integrationDialer(t, statsFunc)
defer func() {
if err := d.Close(); err != nil {
t.Fatalf("Unable to close dialer: %v", err)
}
}()
hc := &http.Client{
Transport: &http.Transport{
Dial: d.Dial,
},
}
resp, err := hc.Get("https://www.google.com/humans.txt")
if err != nil {
t.Fatalf("Unable to fetch from Google: %s", err)
}
defer func() {
if err := resp.Body.Close(); err != nil {
t.Fatalf("Unable to close response body: %v", err)
}
}()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatalf("Unable to read response from Google: %s", err)
}
assert.Equal(t, expectedGoogleResponse, string(b), "Didn't get expected response from Google")
statsMutex.Lock()
defer statsMutex.Unlock()
assert.True(t, dialedDomain == "100partnerprogramme.de" || dialedDomain == "10minutemail.com", "Dialed domain didn't match one of the masquerade domains", dialedDomain)
assert.NotEqual(t, "", dialedAddr, "Should have received an addr")
assert.NotEqual(t, time.Duration(0), actualResolutionTime, "Should have received a resolutionTime")
assert.NotEqual(t, time.Duration(0), actualConnectTime, "Should have received a connectTime")
assert.NotEqual(t, time.Duration(0), actualHandshakeTime, "Should have received a handshakeTime")
}
示例14: TestTimeout
func TestTimeout(t *testing.T) {
text, timedOut, err := Do(10*time.Millisecond, func() (interface{}, error) {
time.Sleep(11 * time.Millisecond)
return expectedText, expectedErr
})
assert.True(t, timedOut, "Should have timed out")
assert.NotNil(t, err, "There should be an error")
assert.Nil(t, text, "Text should be nil")
assert.Equal(t, timeoutErrorString, err.Error(), "Error should contain correct string")
}
示例15: TestTampering
func TestTampering(t *testing.T) {
defer stopMockServers()
proxiedURL, _ := newMockServer(detourMsg)
client := newClient(proxiedURL, 100*time.Millisecond)
resp, err := client.Get("http://255.0.0.1") // it's reserved for future use so will always time out
if assert.NoError(t, err, "should have no error when dial a timeout host") {
time.Sleep(50 * time.Millisecond)
assert.True(t, wlTemporarily("255.0.0.1:80"), "should be added to whitelist if dialing times out")
assertContent(t, resp, detourMsg, "should detour if dialing times out")
}
client = newClient(proxiedURL, 100*time.Millisecond)
resp, err = client.Get("http://127.0.0.1:4325") // hopefully this port didn't open, so connection will be refused
if assert.NoError(t, err, "should have no error if connection is refused") {
time.Sleep(60 * time.Millisecond)
assert.True(t, wlTemporarily("127.0.0.1:4325"), "should be added to whitelist if connection is refused")
assertContent(t, resp, detourMsg, "should detour if connection is refused")
}
}