本文整理匯總了Golang中github.com/getlantern/fdcount.Matching函數的典型用法代碼示例。如果您正苦於以下問題:Golang Matching函數的具體用法?Golang Matching怎麽用?Golang Matching使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Matching函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: 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")
}
示例2: TestClose
func TestClose(t *testing.T) {
_, fdc, err := fdcount.Matching("TCP")
if err != nil {
t.Fatal(err)
}
l, err := net.Listen("tcp", "localhost:0")
if err != nil {
t.Fatalf("Unable to listen: %s", err)
}
defer func() {
if err := l.Close(); err != nil {
t.Fatalf("Unable to close listener: %v", err)
}
time.Sleep(1 * time.Second)
err = fdc.AssertDelta(0)
if err != nil {
t.Errorf("File descriptors didn't return to original: %s", err)
}
}()
addr := l.Addr().String()
conn, err := net.Dial("tcp", addr)
if err != nil {
t.Fatalf("Unable to dial %s: %s", addr, err)
}
c := Conn(conn, clientTimeout, func() {})
for i := 0; i < 100; i++ {
_ = c.Close()
}
}
示例3: 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")
}
示例4: 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")
}
示例5: TestHTTPRedirect
// This test stimulates a connection leak as seen in
// https://github.com/getlantern/lantern/issues/2174.
func TestHTTPRedirect(t *testing.T) {
startProxy(t, false)
client := &http.Client{
Transport: &http.Transport{
Dial: func(network, addr string) (net.Conn, error) {
return Dial(addr, &Config{
DialProxy: func(addr string) (net.Conn, error) {
return net.Dial("tcp", proxyAddr)
},
NewRequest: newRequest,
})
},
DisableKeepAlives: true,
},
}
_, counter, err := fdcount.Matching("TCP")
if err != nil {
t.Fatalf("Unable to get fdcount: %v", err)
}
resp, err := client.Head("http://www.facebook.com")
if assert.NoError(t, err, "Head request to facebook should have succeeded") {
resp.Body.Close()
}
assert.NoError(t, counter.AssertDelta(2), "All file descriptors except the connection from proxy to destination site should have been closed")
}
示例6: 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")
}
示例7: TestOKWithInsecureSkipVerify
func TestOKWithInsecureSkipVerify(t *testing.T) {
_, fdc, err := fdcount.Matching("TCP")
if err != nil {
t.Fatal(err)
}
conn, err := Dial("tcp", ADDR, false, &tls.Config{
InsecureSkipVerify: true,
})
assert.NoError(t, err, "Unable to dial")
<-receivedServerNames
closeAndCountFDs(t, conn, err, fdc)
}
示例8: TestNotOKWithServerName
func TestNotOKWithServerName(t *testing.T) {
_, fdc, err := fdcount.Matching("TCP")
if err != nil {
t.Fatal(err)
}
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, fdc)
}
示例9: TestList
func TestList(t *testing.T) {
_, counter, err := fdcount.Matching("TCP")
if err != nil {
t.Fatalf("Unable to get starting fdcount: %v", err)
}
cfr := getCfr()
dists, err := ListDistributions(cfr)
if assert.NoError(t, err, "Should be able to get all distributions") {
for _, d := range dists {
log.Tracef("%v : %v (%v)", d.InstanceId, d.Domain, d.Status)
}
assert.True(t, len(dists) > 0, "There should be some distributions")
}
assert.NoError(t, counter.AssertDelta(0), "All file descriptors should have been closed")
}
示例10: TestVariableTimeouts
func TestVariableTimeouts(t *testing.T) {
// Timeouts can happen in different places, run a bunch of randomized trials
// to try to cover all of them.
_, fdc, err := fdcount.Matching("TCP")
if err != nil {
t.Fatal(err)
}
doTestTimeout := func(timeout time.Duration) (didTimeout bool) {
_, err := DialWithDialer(&net.Dialer{
Timeout: timeout,
}, "tcp", ADDR, false, &tls.Config{
RootCAs: cert.PoolContainingCert(),
})
if err == nil {
return false
} else {
if neterr, isNetError := err.(net.Error); isNetError {
assert.True(t, neterr.Timeout(), "Dial error should be timeout", timeout)
} else {
t.Fatal(err)
}
return true
}
}
// The 1000-5000 microseconds limits are arbitrary. In some systems this may be too low/high.
// The algorithm will try to adapt if connections succeed and will lower the current limit,
// but it won't be allowed to timeout below the established lower boundary.
timeoutMin := 1000
timeoutMax := 5000
for i := 0; i < 500; i++ {
timeout := rand.Intn(timeoutMax) + 1
didTimeout := doTestTimeout(time.Duration(timeout) * time.Microsecond)
if !didTimeout {
if timeout < timeoutMin {
t.Fatalf("The connection succeeded in an unexpected short time: %d", timeout)
}
timeoutMax = int(float64(timeoutMax) * 0.75)
i-- // repeat the test
}
}
// Wait to give the sockets time to close
time.Sleep(1 * time.Second)
assert.NoError(t, fdc.AssertDelta(0), "Number of open files should be the same after test as before")
}
示例11: 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")
}
示例12: TestAll
func TestAll(t *testing.T) {
_, counter, err := fdcount.Matching("TCP")
if err != nil {
t.Fatalf("Unable to get starting fdcount: %v", err)
}
u := getUtil()
recs, err := u.GetAllRecords()
if assert.NoError(t, err, "Should be able to get all records") {
for _, r := range recs {
log.Tracef("%v : %v", r.Name, r.Content)
}
assert.True(t, len(recs) > 0, "There should be some records")
}
assert.NoError(t, counter.AssertDelta(0), "All file descriptors should have been closed")
}
示例13: TestVariableTimeouts
func TestVariableTimeouts(t *testing.T) {
// Timeouts can happen in different places, run a bunch of randomized trials
// to try to cover all of them.
_, fdc, err := fdcount.Matching("TCP")
if err != nil {
t.Fatal(err)
}
for i := 0; i < 500; i++ {
doTestTimeout(t, time.Duration(rand.Intn(5000)+1)*time.Microsecond)
}
// Wait to give the sockets time to close
time.Sleep(1 * time.Second)
assert.NoError(t, fdc.AssertDelta(0), "Number of open files should be the same after test as before")
}
示例14: TestDeadlineBeforeTimeout
func TestDeadlineBeforeTimeout(t *testing.T) {
_, fdc, err := fdcount.Matching("TCP")
if err != nil {
t.Fatal(err)
}
conn, err := DialWithDialer(&net.Dialer{
Timeout: 500 * time.Second,
Deadline: time.Now().Add(5 * time.Microsecond),
}, "tcp", ADDR, false, nil)
assert.Error(t, err, "There should have been a problem dialing")
if err != nil {
assert.True(t, err.(net.Error).Timeout(), "Dial error should be timeout")
}
closeAndCountFDs(t, conn, err, fdc)
}
示例15: TestOKWithServerNameAndLongTimeout
func TestOKWithServerNameAndLongTimeout(t *testing.T) {
_, fdc, err := fdcount.Matching("TCP")
if err != nil {
t.Fatal(err)
}
conn, err := DialWithDialer(&net.Dialer{
Timeout: 25 * time.Second,
}, "tcp", ADDR, true, &tls.Config{
RootCAs: cert.PoolContainingCert(),
})
assert.NoError(t, err, "Unable to dial")
serverName := <-receivedServerNames
assert.Equal(t, "localhost", serverName, "Unexpected ServerName on server")
closeAndCountFDs(t, conn, err, fdc)
}