本文整理汇总了Golang中once.Do函数的典型用法代码示例。如果您正苦于以下问题:Golang Do函数的具体用法?Golang Do怎么用?Golang Do使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Do函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestDo
func TestDo(t *testing.T) {
ncall = 0
once.Do(call)
if ncall != 1 {
t.Fatalf("once.Do(call) didn't call(): ncall=%d", ncall)
}
once.Do(call)
if ncall != 1 {
t.Fatalf("second once.Do(call) did call(): ncall=%d", ncall)
}
once.Do(call)
if ncall != 1 {
t.Fatalf("third once.Do(call) did call(): ncall=%d", ncall)
}
}
示例2: TestStatShouldHaveStat
func TestStatShouldHaveStat(t *testing.T) {
var ch = make(chan Result)
once.Do(startServer)
wsClients := &WSBench{Connections: 3, Ch: ch}
wsClients.Run()
fmt.Printf("B: %v ", wsClients.results)
fmt.Printf("C: %v ", wsClients.Stats)
if wsClients.Stats["sum"] <= 0 {
t.Errorf("Stats should have sum", wsClients.Stats["sum"], 0)
}
if wsClients.Stats["count"] <= 0 {
t.Errorf("Stats should have count", wsClients.Stats["count"], 0)
}
if wsClients.Stats["avg"] <= 0 {
t.Errorf("Stats should have avg", wsClients.Stats["avg"], 0)
}
if wsClients.Stats["max"] <= 0 {
t.Errorf("Stats should have max", wsClients.Stats["max"], 0)
}
if wsClients.Stats["min"] <= 0 {
t.Errorf("Stats should have min", wsClients.Stats["min"], 0)
}
}
示例3: newFD
func newFD(fd, family, proto int, net string, laddr, raddr Addr) (f *netFD, err os.Error) {
once.Do(startServer)
if e := syscall.SetNonblock(fd, true); e != 0 {
return nil, &OpError{"setnonblock", net, laddr, os.Errno(e)}
}
f = &netFD{
sysfd: fd,
family: family,
proto: proto,
net: net,
laddr: laddr,
raddr: raddr,
}
var ls, rs string
if laddr != nil {
ls = laddr.String()
}
if raddr != nil {
rs = raddr.String()
}
f.sysfile = os.NewFile(fd, net+":"+ls+"->"+rs)
f.cr = make(chan bool, 1)
f.cw = make(chan bool, 1)
return f, nil
}
示例4: TestEchoDraft75
func TestEchoDraft75(t *testing.T) {
once.Do(startServer)
// websocket.Dial()
client, err := net.Dial("tcp", "", serverAddr)
if err != nil {
t.Fatal("dialing", err)
}
ws, err := newClient("/echoDraft75", "localhost", "http://localhost",
"ws://localhost/echoDraft75", "", client, draft75handshake)
if err != nil {
t.Errorf("WebSocket handshake: %v", err)
return
}
msg := []byte("hello, world\n")
if _, err := ws.Write(msg); err != nil {
t.Errorf("Write: error %v", err)
}
var actual_msg = make([]byte, 512)
n, err := ws.Read(actual_msg)
if err != nil {
t.Errorf("Read: error %v", err)
}
actual_msg = actual_msg[0:n]
if !bytes.Equal(msg, actual_msg) {
t.Errorf("Echo: expected %q got %q", msg, actual_msg)
}
ws.Close()
}
示例5: Prime
func Prime(number int64) bool {
once.Do(inits)
if number == 2 {
return true
}
l := int64(len(primes))
i := int64(0)
for ; i < l && primes[i] < number; i++ {
if number%primes[i] == 0 {
return false
}
}
i = primes[i-1] + 1
if i%2 == 0 {
i--
}
for ; i < number; i += 2 {
if number%i == 0 {
return false
}
}
l = int64(len(primes))
if number > primes[l-1] && l < 10000 {
primes = primes[0 : l+1]
primes[l] = number
}
return true
}
示例6: LookupHost
// LookupHost looks for name using the local hosts file and DNS resolver.
// It returns the canonical name for the host and an array of that
// host's addresses.
func LookupHost(name string) (cname string, addrs []string, err os.Error) {
if !isDomainName(name) {
return name, nil, &DNSError{"invalid domain name", name, ""}
}
once.Do(loadConfig)
if dnserr != nil || cfg == nil {
err = dnserr
return
}
// Use entries from /etc/hosts if they match.
addrs = lookupStaticHost(name)
if len(addrs) > 0 {
cname = name
return
}
// If name is rooted (trailing dot) or has enough dots,
// try it by itself first.
rooted := len(name) > 0 && name[len(name)-1] == '.'
if rooted || count(name, '.') >= cfg.ndots {
rname := name
if !rooted {
rname += "."
}
// Can try as ordinary name.
addrs, err = tryOneName(cfg, rname)
if err == nil {
cname = rname
return
}
}
if rooted {
return
}
// Otherwise, try suffixes.
for i := 0; i < len(cfg.search); i++ {
rname := name + "." + cfg.search[i]
if rname[len(rname)-1] != '.' {
rname += "."
}
addrs, err = tryOneName(cfg, rname)
if err == nil {
cname = rname
return
}
}
// Last ditch effort: try unsuffixed.
rname := name
if !rooted {
rname += "."
}
addrs, err = tryOneName(cfg, rname)
if err == nil {
cname = rname
return
}
return
}
示例7: Setenv
// Setenv sets the value of the environment variable named by the key.
// It returns an Error, if any.
func Setenv(key, value string) Error {
once.Do(copyenv)
if len(key) == 0 {
return EINVAL
}
env[key] = value
return nil
}
示例8: createTempDir
func createTempDir() string {
once.Do(seedRand)
result := fmt.Sprintf("/tmp/files_test.%d", rand.Uint32())
err := os.Mkdir(result, 0700)
if err != nil {
panic(fmt.Sprintf("Can't create dir [%s]: %s", result, err))
}
return result
}
示例9: NewTicker
// NewTicker returns a new Ticker containing a channel that will
// send the time, in nanoseconds, every ns nanoseconds. It adjusts the
// intervals to make up for pauses in delivery of the ticks.
func NewTicker(ns int64) *Ticker {
if ns <= 0 {
return nil
}
c := make(chan int64, 1) // See comment on send in tickerLoop
t := &Ticker{c, c, ns, false, Nanoseconds() + ns, nil}
once.Do(startTickerLoop)
// must be run in background so global Tickers can be created
go func() { newTicker <- t }()
return t
}
示例10: TestRunCreatesMultipleConnections
func TestRunCreatesMultipleConnections(t *testing.T) {
var ch = make(chan Result)
once.Do(startServer)
wsClients := &WSBench{Connections: 2, Ch: ch}
wsClients.Run()
fmt.Printf("A: %v ", wsClients.results)
if len(wsClients.results) != 2 {
t.Errorf("Running WSBench w 2 connections should return 2 results ", wsClients.results, 2)
}
}
示例11: TestSetClientConnections
func TestSetClientConnections(t *testing.T) {
once.Do(startServer)
wsClients := &WSBench{Connections: 2, target: "ws://0.0.0.0:5555/echo"}
if wsClients.Connections != 2 {
t.Errorf("Setting connections ", wsClients.Connections, 2)
}
if wsClients.target != "ws://0.0.0.0:5555/echo" {
t.Errorf("Setting target ", wsClients.target, "ws://0.0.0.0:5555/echo")
}
}
示例12: Getenverror
// Getenverror retrieves the value of the environment variable named by the key.
// It returns the value and an error, if any.
func Getenverror(key string) (value string, err Error) {
once.Do(copyenv)
if len(key) == 0 {
return "", EINVAL
}
v, ok := env[key]
if !ok {
return "", ENOENV
}
return v, nil
}
示例13: TestMoreThan30Connections
func TestMoreThan30Connections(t *testing.T) {
var ch = make(chan Result)
num := 1000000
once.Do(startServer)
wsClients := &WSBench{Connections: num, Ch: ch}
wsClients.Run()
// fmt.Printf("A: %v ", wsClients.results)
if len(wsClients.results) < num {
t.Errorf("Running WSBench w 2 connections should return 30 results ", len(wsClients.results), num)
}
}
示例14: TestHTTPDraft75
func TestHTTPDraft75(t *testing.T) {
once.Do(startServer)
r, _, err := http.Get(fmt.Sprintf("http://%s/echoDraft75", serverAddr))
if err != nil {
t.Errorf("Get: error %#v", err)
return
}
if r.StatusCode != http.StatusBadRequest {
t.Errorf("Get: got status %d", r.StatusCode)
}
}
示例15: TestRPC
func TestRPC(t *testing.T) {
once.Do(startServer)
client, err := Dial("tcp", serverAddr)
if err != nil {
t.Fatal("dialing", err)
}
// Synchronous calls
args := &Args{7, 8}
reply := new(Reply)
err = client.Call("Arith.Add", args, reply)
if reply.C != args.A+args.B {
t.Errorf("Add: expected %d got %d", reply.C, args.A+args.B)
}
args = &Args{7, 8}
reply = new(Reply)
err = client.Call("Arith.Mul", args, reply)
if reply.C != args.A*args.B {
t.Errorf("Mul: expected %d got %d", reply.C, args.A*args.B)
}
// Out of order.
args = &Args{7, 8}
mulReply := new(Reply)
mulCall := client.Go("Arith.Mul", args, mulReply, nil)
addReply := new(Reply)
addCall := client.Go("Arith.Add", args, addReply, nil)
<-addCall.Done
if addReply.C != args.A+args.B {
t.Errorf("Add: expected %d got %d", addReply.C, args.A+args.B)
}
<-mulCall.Done
if mulReply.C != args.A*args.B {
t.Errorf("Mul: expected %d got %d", mulReply.C, args.A*args.B)
}
// Error test
args = &Args{7, 0}
reply = new(Reply)
err = client.Call("Arith.Div", args, reply)
// expect an error: zero divide
if err == nil {
t.Error("Div: expected error")
} else if err.String() != "divide by zero" {
t.Error("Div: expected divide by zero error; got", err)
}
}