func TestInitClones(t *testing.T) { connPool = &clones{ Num: 1, MaxNum: 5, } err := connPool.Init() if err != nil { t.Fatal(e.Trace(e.Forward(err))) } connPoolErr := &clones{ Num: 0, MaxNum: 5, } err = connPoolErr.Init() if err != nil && !e.Equal(err, "number of clones invalid") { t.Fatal(e.Trace(e.Forward(err))) } else if err == nil { t.Fatal("nil error") } connPoolErr = &clones{ Num: 10, MaxNum: 5, } err = connPoolErr.Init() if err != nil && !e.Equal(err, "number of streams is greater than max") { t.Fatal(e.Trace(e.Forward(err))) } else if err == nil { t.Fatal("nil error") } }
func TestAddPlainTextCredentials(t *testing.T) { ptc := NewPlainTextCredentials() pt1 := &PlainText{"id", "pass1234"} pt1b := &PlainText{"id", "pass1234"} pt2 := &PlainText{"id2", "pass2345"} hash := &Hashed{"a", "b"} err := ptc.Add(pt1) if err != nil { t.Fatal(e.Trace(e.Forward(err))) } err = ptc.Add(pt2) if err != nil { t.Fatal(e.Trace(e.Forward(err))) } err = ptc.Add(pt1b) if err != nil && !e.Equal(err, ErrCredExist) { t.Fatal(e.Trace(e.Forward(err))) } err = ptc.Add(hash) if err != nil && !e.Equal(err, ErrNotComp) { t.Fatal(e.Trace(e.Forward(err))) } }
func TestBarrierWriterReaderPartial(t *testing.T) { buf := NewBuffer() defer buf.Close() b, err := rand.Bytes(1e6, "go") if err != nil { t.Fatal(e.Trace(e.Forward(err))) } n, err := buf.Write(b) if err != nil { t.Fatal(e.Trace(e.Forward(err))) } if n != len(b) { t.Fatal("length invalid") } b2 := make([]byte, len(b)) count := 0 l := 100 for count < len(b) { n, err = buf.Read(b2[count : count+l]) if err == io.EOF { break } else if err != nil { t.Fatal(e.Trace(e.Forward(err))) } count += n } if count != len(b) { t.Fatal("length invalid", count, len(b)) } if !bytes.Equal(b, b2) { t.Fatal("not equal") } }
func (a *Server) bind() (conn *net.UDPConn, err error) { if !a.NotMulticast && a.iface.Flags&net.FlagMulticast == net.FlagMulticast { gaddr, err := a.groupAddr() if err != nil { return nil, e.Forward(err) } conn, err = net.ListenMulticastUDP(a.Proto(), a.iface, gaddr) if err != nil { return nil, e.New(err) } } else { server, err := net.ResolveUDPAddr(a.Proto(), ":"+a.Port) if err != nil { return nil, e.New(err) } conn, err = net.ListenUDP(a.Proto(), server) if err != nil { return nil, e.New(err) } } a.ipver(conn.LocalAddr()) _, a.Port, err = utilNet.SplitHostPort(conn.LocalAddr().String()) if err != nil { return nil, e.Forward(err) } return }
func TestDeletePlainTextCredentials(t *testing.T) { ptc := NewPlainTextCredentials() pt1 := &PlainText{"id", "pass1234"} err := ptc.Add(pt1) if err != nil { t.Fatal(e.Trace(e.Forward(err))) } _, err = ptc.Get("id") if err != nil { t.Fatal(e.Trace(e.Forward(err))) } err = ptc.Delete("id") if err != nil { t.Fatal(e.Trace(e.Forward(err))) } err = ptc.Delete("id") if err != nil && !e.Equal(err, ErrCredNotExist) { t.Fatal(e.Trace(e.Forward(err))) } _, err = ptc.Get("id") if err != nil && !e.Equal(err, ErrCredNotExist) { t.Fatal(e.Trace(e.Forward(err))) } }
func TestProtoReqInst(t *testing.T) { buf := bytes.NewBuffer([]byte{}) enc := msgpack.NewEncoder(buf) dec := msgpack.NewDecoder(buf) req := &ReqInst{ Inst: "inst", Obj: "obj", Args: []interface{}{"string", int(42)}, } err := enc.Encode(req) if err != nil { t.Fatal(e.Trace(e.Forward(err))) } recv := ReqInst{} err = dec.Decode(&recv) if err != nil { t.Fatal(e.Trace(e.Forward(err))) } if recv.Inst != "inst" || recv.Obj != "obj" || len(recv.Args) != 2 || recv.Args[0].(string) != "string" || recv.Args[1].(int) != 42 { t.Fatal("don't decode") } }
// Discover funtion discovers the server and returns the data sent by the server. func (c *Client) Discover() (*Response, error) { if c.Port == "" { c.Port = "3456" } if c.BufSize <= 0 { c.BufSize = 1024 } if c.Timeout <= 0 { c.Timeout = 2 * time.Minute } if c.Deadline <= 0 { c.Deadline = 10 * time.Second } if c.Keepalive <= 0 { c.Keepalive = 10 * time.Second } var err error if c.Id == "" { c.Id, err = rand.Uuid() if err != nil { return nil, e.Forward(err) } } c.stopKa = make(chan chan struct{}) c.InitMCast() err = c.getInt() if err != nil { return nil, e.Forward(err) } resp, err := c.getAddr() if err != nil { return nil, e.Forward(err) } return resp, nil }
func (cd *CacheDisk) Hits(width, height uint, keys ...interface{}) (h uint32, err error) { hash, err := hashKeys(keys...) if err != nil { return 0, e.Forward(err) } bufw := encLength(uint64(width)) bufh := encLength(uint64(height)) hitshash := []byte(string(hash) + string(bufw) + string(bufh)) err = cd.db.View(func(tx *bolt.Tx) error { var err error bhits := tx.Bucket([]byte(hits)) buf := bhits.Get(hitshash) if buf == nil { return e.New(ErrImageNotFound) } h, err = decHits(buf) if err != nil { return e.Forward(err) } return nil }) err = e.Forward(err) return }
func (c *CacheMem) Put(img Image, keys ...interface{}) error { c.lck.Lock() defer c.lck.Unlock() num, err := c.numImages(keys...) if err != nil { return e.Forward(err) } if num >= c.maximages { err = c.removeLFU(keys...) if err != nil { return e.Forward(err) } } rect, err := img.Bounds() if err != nil { return e.Forward(err) } width := uint(rect.Max.X - rect.Min.X) height := uint(rect.Max.Y - rect.Min.Y) item, err := newItem(img, width, height, keys...) if err != nil { return e.Forward(err) } c.images.ReplaceOrInsert(item) return nil }
func (c *client) Init() error { var err error c.session, err = openSession(c.Proto, c.Addr, c.TlsConfig, c.SessionName, c.Auth, c.DialTimeout, c.ConnTimeout) if err != nil { return e.Forward(err) } c.clones = &clones{ Num: c.NumClones, MaxNum: c.MaxNumClones, } err = c.clones.Init() if err != nil { return e.Forward(err) } if c.KeepAlive == 0 { return nil } go func() { for { time.Sleep(c.KeepAlive) err := c.session.Keepalive() if err != nil { log.ProtoLevel().Tag("gormethods", "client", "keepalive").Printf("Keepalive failed: %v", err) } } }() return nil }
func (cd *CacheDisk) Put(img Image, keys ...interface{}) error { err := cd.db.Update(func(tx *bolt.Tx) error { hash, err := hashKeys(keys...) if err != nil { return e.Forward(err) } l := lenImg(tx, hash) if l >= cd.images { err = removeLFU(tx, hash) if err != nil { return e.Forward(err) } } err = store(tx, img, hash) if err != nil { return e.Forward(err) } return nil }) return e.Forward(err) }
func (g *Generic) Commit(entry Entry) { var err error defer func() { if err != nil { CommitFail(entry, err) } }() if g.r != nil && !g.r.Result(entry) { return } if g.f == nil { err = e.New("formater not set") return } err = g.s.Tx(true, func(tx Transaction) error { entry.Formatter(g.f) err := tx.Put(entry.Date().Format(time.RFC3339Nano), entry) if err != nil { return e.Forward(err) } return nil }) if err != nil { err = e.Forward(err) return } }
func (p *photo) Transform(crop image.Rectangle, width, height uint, dontenlarge bool, format string, resample resize.InterpolationFunction) (Image, error) { img, err := p.Image() if err != nil { return nil, e.Forward(err) } imgResized := img if !(width == 0 && height == 0) { rect := img.Bounds() w := uint(rect.Max.X - rect.Min.X) h := uint(rect.Max.Y - rect.Min.Y) if (width >= w || height >= h) && !dontenlarge { imgResized = resize.Resize(width, height, img, resample) } else if width < w && height < h { imgResized = resize.Resize(width, height, img, resample) } } b := make([]byte, 0, 1024) buf := bytes.NewBuffer(b) err = jpeg.Encode(buf, imgResized, nil) if err != nil { return nil, e.Forward(err) } return &photo{Buf: buf.Bytes()}, nil }
// PingHttp connect a http or https server and try to // receive something. If the server return a code different // of 2xx, it will fail. Ignores insecure certificates. func PingHttp(url *url.URL) error { resp, err := httpClient.Get(url.String()) if e.Contains(err, "connection refused") { return e.Push(e.New(err), "get failed: connection refused") } else if err != nil { return e.Push(e.New(err), "get failed") } defer resp.Body.Close() buf := make([]byte, 4096) if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusMultipleChoices { n, err := resp.Body.Read(buf) if err != nil && err != io.EOF { return e.Forward(err) } buf = buf[:n] //status.Log(status.Protocol, "PingHttp status code is %v and received it from server: %v", resp.StatusCode, string(buf)) log.ProtoLevel().Printf("PingHttp status code is %v and received it from server: %v", resp.StatusCode, string(buf)) return e.New("returned status code %v, expected 2xx", resp.StatusCode) } _, err = resp.Body.Read(buf) if err != nil && err != io.EOF { return e.Forward(err) } return nil }
func put(t *testing.T, s Storer, num int) { err := s.Tx(true, func(tx Transaction) error { var err error for i := 0; i < num; i++ { key := strconv.Itoa(i) err = tx.Put(key, &TestStruct{I: i, Key: key}) if err != nil { return err } } err = tx.Put("3a", &TestStruct{I: 3, Key: "3a"}) if err != nil { return err } err = tx.Put("3a", &TestStruct{I: 3, Key: "3a"}) if err != nil { return err } return nil }) if err != nil { t.Fatal(e.Trace(e.Forward(err))) } l, err := s.Len() if err != nil { t.Fatal(e.Trace(e.Forward(err))) } if l != uint(num+1) { t.Fatalf("wrong len %v %v", l, num) } }
func (c *clones) Request(sess *protoSession, inst string) (connInst, error) { c.lck.Lock() defer c.lck.Unlock() instances, found := c.clones[sess.sess] if !found { instances = make(map[string]*clone) c.clones[sess.sess] = instances } clonner, found := instances[inst] if !found { clonner = &clone{ Num: c.Num, MaxNum: c.MaxNum, Session: sess, } err := clonner.Init(inst) if err != nil { return nil, e.Forward(err) } instances[inst] = clonner } conn, err := clonner.Request() if err != nil { return nil, e.Forward(err) } return conn, nil }
func del(t *testing.T, s Storer, num int) { err := s.Tx(true, func(tx Transaction) error { var err error for i := 0; i < num; i++ { key := strconv.Itoa(i) err = tx.Del(key) if err != nil { return err } } err = tx.Del("3a") if err != nil { return err } return nil }) if err != nil { t.Fatal(e.Trace(e.Forward(err))) } l, err := s.Len() if err != nil { t.Fatal(e.Trace(e.Forward(err))) } if l != 0 { t.Fatalf("something remining %v", l) } }
func TestConstructFail(t *testing.T) { err := constructor.New(&struct3{}, func(count int) int { return 42 }) if err != nil { t.Fatal(e.Trace(e.Forward(err))) } _, err = constructor.Construct(types.Name(&struct3{}), 0) if err != nil && !e.Equal(err, ErrInvReturn) { t.Fatal(e.Trace(e.Forward(err))) } else if err == nil { t.Fatal("error is nil") } err = constructor.Delete(types.Name(&struct3{})) if err != nil { t.Fatal(e.Trace(e.Forward(err))) } err = constructor.New(&struct3{}, func(count int) (int, int) { return 42, 69 }) if err != nil { t.Fatal(e.Trace(e.Forward(err))) } _, err = constructor.Construct(types.Name(&struct3{}), 0) if err != nil && !e.Equal(err, ErrInvReturn) { t.Fatal(e.Trace(e.Forward(err))) } else if err == nil { t.Fatal("error is nil") } }
func DecInterface(dec *msgpack.Decoder) (interface{}, error) { var st status err := dec.Decode(&st) if err != nil { return nil, e.Forward(err) } switch st { case statusNotNil: var typename string err = dec.Decode(&typename) if err != nil { return nil, e.Forward(err) } t, err := types.GetType(typename) if err != nil { return nil, e.Forward(err) } ptr := types.Make(reflect.PtrTo(t)) err = dec.DecodeValue(ptr) if err != nil { return nil, e.Forward(err) } return reflect.Indirect(ptr).Interface(), nil case statusNil: return nil, nil } panic("not here") }
func BenchmarkLogFileBuffer(b *testing.B) { name, err := rand.FileName("BenchmarkLogFile", ".log", 10) if err != nil { b.Error(e.Trace(e.Forward(err))) } name = os.TempDir() + name file, err := os.Create(name) if err != nil { b.Error(e.Trace(e.Forward(err))) } defer os.Remove(name) defer file.Close() back := NewOutBuffer( NewWriter(file).F(DefFormatter), b.N/2, ) defer back.(*OutBuffer).Close() logger := New(back, false).Domain("test") b.ResetTimer() for i := 0; i < b.N; i++ { logger.Print(msg) b.SetBytes(l) } }
func (etc *etcd) Init() error { var err error if len(etc.Endpoints) == 0 { return e.New("no end points") } cfg := client.Config{ Endpoints: etc.Endpoints, //Transport: http.DefaultTransport, } c, err := client.New(cfg) if err != nil { return e.Forward(err) } etc.kapi = client.NewKeysAPI(c) if etc.SecKeyRing == "" { return nil } kr, err := os.Open(etc.SecKeyRing) if err != nil { return e.Forward(err) } defer kr.Close() etc.cm, err = config.NewEtcdConfigManager(etc.Endpoints, kr) if err != nil { return e.Forward(err) } return nil }
func BenchmarkBoltDbBuffer(b *testing.B) { name, err := rand.FileName("boltdb", ".db", 10) if err != nil { b.Error(e.Trace(e.Forward(err))) } name = os.TempDir() + "/" + name gob := &Gob{ TypeName: types.Name(&log{}), } bolt, err := NewBoltDb("test", name, 0600, nil, gob, gob) if err != nil { b.Error(e.Trace(e.Forward(err))) } logger := New( NewOutBuffer( NewGeneric(bolt).F(DefFormatter), b.N/2, ), false, ).Domain("test") b.ResetTimer() for i := 0; i < b.N; i++ { logger.Print(msg) b.SetBytes(l) } }
func (d *Daemon) exec(cmd string, args ...string) error { done := make(chan error, 1) c := exec.Command(cmd, args...) c.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} err := c.Start() if err != nil { return e.Forward(err) } go func() { done <- c.Wait() }() select { case <-time.After(d.ExecTimeout): <-done // allow goroutine to exit err := c.Process.Kill() if err != nil { d.status.Log(status.Debug, "Failed to kill command %v for %v.", cmd, d.DaemonName) return e.Forward(err) } d.status.Log(status.Debug, "Command %v exec for %v was killed, timeout.", cmd, d.DaemonName) case err := <-done: if e.Contains(err, "exit status") { msg := "Command %v exec for %v exited with this error: %v" d.status.Log(status.Debug, msg, cmd, d.DaemonName, e.Trace(e.Forward(err))) return e.Forward(err) } else if err != nil { return e.Forward(err) } } return nil }
func BenchmarkMongoDbBuffer(b *testing.B) { mongodb, err := NewMongoDb("mongodb://localhost/test", "test", nil, Log, 30*time.Second) if err != nil { b.Error(e.Trace(e.Forward(err))) } err = mongodb.Drop() if err != nil { b.Error(e.Trace(e.Forward(err))) } logger := New( NewOutBuffer( NewGeneric(mongodb).F(DefFormatter), b.N/2, ), false, ).Domain("test") b.ResetTimer() for i := 0; i < b.N; i++ { logger.Print(msg) b.SetBytes(l) } b.StopTimer() err = mongodb.Drop() if err != nil { b.Error(e.Trace(e.Forward(err))) } }
func TestValidate(t *testing.T) { ptc := NewPlainTextCredentials() pt1 := &PlainText{"id", "passpass"} pt2 := &PlainText{"id2", "passpass2"} pt3 := &PlainText{"id3", "passpass2"} pt1b := &PlainText{"id", "passpass2"} err := ptc.Add(pt1) if err != nil { t.Fatal(e.Trace(e.Forward(err))) } err = ptc.Add(pt2) if err != nil { t.Fatal(e.Trace(e.Forward(err))) } err = ptc.Add(pt3) if err != nil { t.Fatal(e.Trace(e.Forward(err))) } if err := Validate(ptc, pt1); err != nil { t.Fatal("invalid") } if err := Validate(ptc, pt2); err != nil { t.Fatal("invalid") } if err := Validate(ptc, pt3); err != nil { t.Fatal("invalid") } if err := Validate(ptc, pt1b); err == nil { t.Fatal("invalid") } }
func BenchmarkLogOuterFile(b *testing.B) { name, err := rand.FileName("BenchmarkLogFile", ".log", 10) if err != nil { b.Error(e.Trace(e.Forward(err))) } name = os.TempDir() + "/" + name file, err := os.Create(name) if err != nil { b.Error(e.Trace(e.Forward(err))) } defer os.Remove(name) defer file.Close() backend := NewWriter(file).F(DefFormatter) olog, ok := backend.(OuterLogger) if !ok { return } w := olog.OuterLog(DebugPrio, "tag") defer olog.Close() b.ResetTimer() for i := 0; i < b.N; i++ { n, err := w.Write([]byte(msg + "\n")) if err != nil { b.Error(e.Trace(e.Forward(err))) } if int64(n) != l+1 { b.Error("write failed", n, l) } b.SetBytes(l) } }
func TestEditPlainTextCredentials(t *testing.T) { ptc := NewPlainTextCredentials() pt1 := &PlainText{"id", "pass1234"} err := ptc.Add(pt1) if err != nil { t.Fatal(e.Trace(e.Forward(err))) } pt1b := &PlainText{"id", "pass2345"} err = ptc.Edit(pt1b) if err != nil { t.Fatal(e.Trace(e.Forward(err))) } auth, err := ptc.Get("id") if err != nil { t.Fatal(e.Trace(e.Forward(err))) } if !auth.Auth(pt1b) { t.Fatal("auth not equal") } pt2 := &PlainText{"id2", "pass2345"} err = ptc.Edit(pt2) if err != nil && !e.Equal(err, ErrCredNotExist) { t.Fatal(e.Trace(e.Forward(err))) } }
func startServer() (*core.Server, error) { instances := core.NewInstances() owners := auth.NewPlainTextCredentials() id := &auth.PlainText{Ident: "id", Pass: "******"} owners.Add(id) constructor := core.NewConstructor() server := &core.Server{ Proto: "tcp", Addr: "localhost:0", ConnTimeout: 30 * time.Second, Insts: instances, Cons: constructor, FifoLength: 1, Ttl: 24 * time.Hour, Cleanup: 5 * time.Minute, } err := server.Start() if err != nil { return nil, e.Forward(err) } err = instances.New("1", "1", reflect.ValueOf(&DummyServer{}), owners) if err != nil { return nil, e.Forward(err) } return server, nil }
func PrintDb(db *bolt.DB, buckets []string) error { err := db.View(func(tx *bolt.Tx) error { // err := tx.ForEach(func(name []byte, b *bolt.Bucket) error { // fmt.Println(string(name)) // return nil // }) for _, bucket := range buckets { b := tx.Bucket([]byte(bucket)) if b == nil { log.Printf("Bucket %v not found.", bucket) continue } fmt.Println(bucket, b.Stats().KeyN) err := b.ForEach(func(k, v []byte) error { fmt.Printf("\t%v -> %v\n", string(k), string(v)) err := goInside(tx, v, 2) if err != nil { return e.Forward(err) } return nil }) if err != nil { return e.Forward(err) } } return nil }) if err != nil { return e.Forward(err) } return nil }
func TestGet(t *testing.T) { inst, err := instances.Get("1", "1") if err != nil { t.Fatal(e.Trace(e.Forward(err))) } if inst.Instance().Type() != reflect.TypeOf(&teststruct{}) { t.Fatal("get fail") } inst, err = instances.Get("1", "3") if err != nil { t.Fatal(e.Trace(e.Forward(err))) } if inst.Instance().Type() != reflect.TypeOf(&teststruct{}) { t.Fatal("get fail") } _, err = instances.Get("1", "1") if err != nil && !e.Equal(err, ErrAuthUserNotAuthorized) { t.Fatal(e.Trace(e.Forward(err))) } _, err = instances.Get("2", "2") if err != nil && !e.Equal(err, ErrSessionNotFound) { t.Fatal(e.Trace(e.Forward(err))) } _, err = instances.Get("1", "2") if err != nil && !e.Equal(err, ErrInstanceNotFound) { t.Fatal(e.Trace(e.Forward(err))) } }