func TestPool(t *testing.T) { Log.SetHandler(tsthlp.TestHandler(t)) p := NewPool(*dsn) cx1, err := p.Get() if err != nil { t.Fatal(err) } t.Logf("1. %p", cx1.handle) defer cx1.Close() cx2, err := p.Get() if err != nil { t.Fatal(err) } t.Logf("2. %p", cx2.handle) if cx1.handle == cx2.handle { t.Errorf("got the same for second, wanted different") } defer cx2.Close() p.Put(cx2) cx3, err := p.Get() if err != nil { t.Fatal(err) } t.Logf("3. %p", cx3.handle) defer cx3.Close() if cx3.handle != cx2.handle { t.Errorf("put back %v, got %v", cx2, cx3) } }
func TestMailAddress(t *testing.T) { Log.SetHandler(tsthlp.TestHandler(t)) for i, str := range [][3]string{ [3]string{"=?iso-8859-2?Q?Bogl=E1rka_Tak=E1cs?= <*****@*****.**>", "Boglárka Takács", "<*****@*****.**>"}, [3]string{"=?iso-8859-2?Q?K=E1r_Oszt=E1ly_=28kar=40kobe=2Ehu=29?= <*****@*****.**>", "Kár_Osztály [email protected]", "<*****@*****.**>"}, } { mh := make(map[string][]string, 1) k := strconv.Itoa(i) mh[k] = []string{str[0]} mailHeader := mail.Header(mh) if addr, err := mailHeader.AddressList(k); err == nil { t.Errorf("address for %s: %q", k, addr) } else { t.Logf("error parsing address %s(%q): %s", k, mailHeader.Get(k), err) } if addr, err := ParseAddress(str[0]); err != nil { t.Errorf("error parsing address %q: %s", str[0], err) } else { t.Logf("address for %q: %q <%s>", str[0], addr.Name, addr.Address) } } }
func TestDecode(t *testing.T) { Log.SetHandler(tsthlp.TestHandler(t)) check := func(i int, code, name string, got, want float64) { if !closeEnough(got, want) { t.Errorf("%d. %q want %s=%f, got %f", i, code, name, want, got) t.FailNow() } } for i, elt := range encoding { area, err := Decode(elt.code) if err != nil { t.Errorf("%d. %q: %v", i, elt.code, err) continue } code := Encode(elt.lat, elt.lng, area.Len) if code != elt.code { t.Errorf("%d. encode (%f,%f) got %q, awaited %q", i, elt.lat, elt.lng, code, elt.code) } C := func(name string, got, want float64) { check(i, elt.code, name, got, want) } C("latLo", area.LatLo, elt.latLo) C("latHi", area.LatHi, elt.latHi) C("lngLo", area.LngLo, elt.lngLo) C("lngHi", area.LngHi, elt.lngHi) } }
func TestCheck(t *testing.T) { Log.SetHandler(tsthlp.TestHandler(t)) for i, elt := range validity { err := Check(elt.code) got := err == nil if got != elt.isValid { t.Errorf("%d. %q got %t (%v), awaited %t.", i, elt.code, got, err, elt.isValid) } } }
func TestEncode(t *testing.T) { Log.SetHandler(tsthlp.TestHandler(t)) for i, elt := range encoding { n := len(stripCode(elt.code)) code := Encode(elt.lat, elt.lng, n) if code != elt.code { t.Errorf("%d. got %q for (%v,%v,%d), awaited %q.", i, code, elt.lat, elt.lng, n, elt.code) t.FailNow() } } }
func TestScoll(t *testing.T) { Log.SetHandler(tsthlp.TestHandler(t)) c := newScollectorCollector() prometheus.MustRegister(c) http.HandleFunc(*flagScollPref, c.handleScoll) http.Handle("/metrics", prometheus.Handler()) Log.Info("Serving on " + *flagAddr) go http.ListenAndServe(*flagAddr, nil) port := strings.SplitN(*flagAddr, ":", 2)[1] cmd := exec.Command("scollector", "-h=http://localhost:"+port) cmd.Stderr = os.Stderr t.Logf("starting scollector") if err := cmd.Start(); err != nil { t.Skipf("Cannot start scollector: %v", err) return } var body bytes.Buffer for i := 0; i < 10; i++ { if i > 0 { time.Sleep(100 * time.Millisecond) } resp, err := http.Get("http://" + *flagAddr + "/metrics") if err != nil { t.Errorf("cannot get /metrics: %v", err) t.FailNow() } body.Reset() _, err = io.Copy(&body, resp.Body) _ = resp.Body.Close() if err != nil { t.Logf("Response:\n%s", body.Bytes()) t.Errorf("Error reading response body: %v", err) continue } if bytes.Contains(body.Bytes(), []byte("\nos_cpu{")) { //t.Logf("Response:\n%s", body.Bytes()) break } } _ = cmd.Process.Kill() var lines []string for _, line := range bytes.Split(body.Bytes(), []byte("\n")) { if bytes.HasPrefix(line, []byte("os_disk_fs_percent_free{")) { lines = append(lines, string(line)) } } if len(lines) < 2 { t.Errorf("awaited at least 2 lines of \"os_disk_fs_percent_free\", got %d: %q", len(lines), lines) } }
func getConnection(t *testing.T) *sql.DB { var err error if testDB != nil && testDB.Ping() == nil { return testDB } flag.Parse() Log.SetHandler(tsthlp.TestHandler(t)) if testDB, err = sql.Open("goracle", *fDsn); err != nil { t.Fatalf("error connecting to %q: %s", *fDsn, err) } return testDB }
func getConnection(t *testing.T) *Connection { if conn.IsConnected() { return conn } Log.SetHandler(tsthlp.TestHandler(t)) if !(dsn != nil && *dsn != "") { t.Logf("cannot test connection without dsn!") return conn } user, passw, sid := SplitDSN(*dsn) var err error conn, err = NewConnection(user, passw, sid, false) if err != nil { Log.Crit("Create connection", "dsn", *dsn, "error", err) panic("cannot create connection: " + err.Error()) } if err = conn.Connect(0, false); err != nil { Log.Crit("Connecting", "error", err) panic("error connecting: " + err.Error()) } return conn }
func TestShorten(t *testing.T) { Log.SetHandler(tsthlp.TestHandler(t)) for i, elt := range shorten { got, err := Shorten(elt.code, elt.lat, elt.lng) if err != nil { t.Errorf("%d. shorten %q: %v", i, elt.code, err) t.FailNow() } if got != elt.short { t.Errorf("%d. shorten got %q, awaited %q.", i, got, elt.short) t.FailNow() } got, err = RecoverNearest(got, elt.lat, elt.lng) if err != nil { t.Errorf("%d. nearest %q: %v", i, got, err) t.FailNow() } if got != elt.code { t.Errorf("%d. nearest got %q, awaited %q.", i, got, elt.code) t.FailNow() } } }