func Test_QueryReader(t *testing.T) { conn, err := Connect(TestConnParam) utest.IsNilNow(t, err) defer conn.Close() var res DataReader res, err = conn.QueryReader("SELECT * FROM test ORDER BY id ASC") utest.IsNilNow(t, err) defer res.Close() i := 0 for { row, err1 := res.FetchNext() utest.IsNilNow(t, err1) if row == nil { break } utest.EqualNow(t, row[0].Int64(), int64(i)) utest.EqualNow(t, row[1].String(), strconv.Itoa(i)) i++ } utest.EqualNow(t, i, 10) }
func Test_Execute(t *testing.T) { param := TestConnParam param.DbName = "mysql" conn, err := Connect(param) utest.IsNilNow(t, err) defer conn.Close() _, err = conn.Execute("CREATE DATABASE " + TestConnParam.DbName) utest.IsNilNow(t, err) _, err = conn.Execute("USE " + TestConnParam.DbName) utest.IsNilNow(t, err) _, err = conn.Execute(`CREATE TABLE test ( id INT PRIMARY KEY, value VARCHAR(10) )`) utest.IsNilNow(t, err) for i := 0; i < 10; i++ { res, err := conn.Execute("INSERT INTO test VALUES(" + strconv.Itoa(i) + ",'" + strconv.Itoa(i) + "')") utest.IsNilNow(t, err) utest.EqualNow(t, res.RowsAffected(), 1) } }
func Test_Connect(t *testing.T) { conn, err := sql.Open("mysql", TestConnParam) utest.IsNilNow(t, err) err = conn.Close() utest.IsNilNow(t, err) }
func Test_Query(t *testing.T) { conn, err := sql.Open("mysql", TestConnParam) utest.IsNilNow(t, err) defer conn.Close() var res *sql.Rows res, err = conn.Query("SELECT * FROM test ORDER BY id ASC") utest.IsNilNow(t, err) defer res.Close() i := 0 for res.Next() { var ( id int64 value string ) err := res.Scan(&id, &value) utest.IsNilNow(t, err) utest.Equal(t, id, int64(i)) utest.Equal(t, value, strconv.Itoa(i)) i++ } utest.Equal(t, i, 10) }
func Test_Execute(t *testing.T) { param := TestConnEnv param.DbName = "mysql" name, _ := json.Marshal(param) conn, err := sql.Open("mysql", string(name)) utest.IsNilNow(t, err) defer conn.Close() _, err = conn.Exec("CREATE DATABASE " + TestConnEnv.DbName) utest.IsNilNow(t, err) _, err = conn.Exec("USE " + TestConnEnv.DbName) utest.IsNilNow(t, err) _, err = conn.Exec(`CREATE TABLE test ( id INT PRIMARY KEY, value VARCHAR(10) )`) utest.IsNilNow(t, err) for i := 0; i < 10; i++ { res, err := conn.Exec("INSERT INTO test VALUES(" + strconv.Itoa(i) + ",'" + strconv.Itoa(i) + "')") utest.IsNilNow(t, err) num, _ := res.RowsAffected() utest.Equal(t, num, 1) } }
func Test_Clean(t *testing.T) { conn, err := Connect(TestConnParam) utest.IsNilNow(t, err) defer conn.Close() _, err = conn.Execute("DROP DATABASE " + TestConnParam.DbName) utest.IsNilNow(t, err) }
func Test_Clean(t *testing.T) { conn, err := sql.Open("mysql", TestConnParam) utest.IsNilNow(t, err) defer conn.Close() _, err = conn.Exec("DROP DATABASE " + TestConnEnv.DbName) utest.IsNilNow(t, err) }
func Test_ReadWrite_Uint24BE(t *testing.T) { ReadWriteTest(t, 10000, func(r *Reader, w *Writer) { v1 := uint32(rand.Intn(0xFFFFFF)) w.WriteUint24BE(v1) utest.IsNilNow(t, w.Error()) v2 := r.ReadUint24BE() utest.IsNilNow(t, r.Error()) utest.EqualNow(t, v1, v2) }) }
func Test_ReadWrite_IntLE(t *testing.T) { ReadWriteTest(t, 10000, func(r *Reader, w *Writer) { v1 := int(rand.Int63n(0x7FFFFFFFFFFFFFFF)) w.WriteIntLE(v1) utest.IsNilNow(t, w.Error()) v2 := r.ReadIntLE() utest.IsNilNow(t, r.Error()) utest.EqualNow(t, v1, v2) }) }
func Test_ReadWrite_Float64LE(t *testing.T) { ReadWriteTest(t, 10000, func(r *Reader, w *Writer) { v1 := rand.NormFloat64() w.WriteFloat64LE(v1) utest.IsNilNow(t, w.Error()) v2 := r.ReadFloat64LE() utest.IsNilNow(t, r.Error()) utest.EqualNow(t, v1, v2) }) }
func Test_BadAddr(t *testing.T) { conn, err := net.Dial("tcp", cfgGatewayAddr) utest.IsNilNow(t, err) defer conn.Close() _, err = conn.Write([]byte("abc\n")) utest.IsNilNow(t, err) code := make([]byte, 3) _, err = io.ReadFull(conn, code) utest.IsNilNow(t, err) utest.EqualNow(t, string(code), string(codeBadAddr)) }
func Test_ReadWrite_String(t *testing.T) { ReadWriteTest(t, 10000, func(r *Reader, w *Writer) { b := string(RandBytes(256)) w.WriteString(b) utest.IsNilNow(t, w.Error()) c := r.ReadString(len(b)) utest.IsNilNow(t, r.Error()) utest.EqualNow(t, b, c) }) }
func ObjectTest(t *testing.T, session *Session) { for i := 0; i < 2000; i++ { msg1 := RandObject() err := session.Send(&msg1) utest.IsNilNow(t, err) var msg2 TestObject err = session.Receive(&msg2) utest.IsNilNow(t, err) utest.EqualNow(t, msg1, msg2) } }
func BytesTest(t *testing.T, session *Session) { for i := 0; i < 2000; i++ { msg1 := RandBytes(512) err := session.Send(msg1) utest.IsNilNow(t, err) var msg2 = make([]byte, len(msg1)) err = session.Receive(msg2) utest.IsNilNow(t, err) utest.EqualNow(t, msg1, msg2) } }
func Test_BadReq1(t *testing.T) { conn, err := net.Dial("tcp", cfgGatewayAddr) utest.IsNilNow(t, err) defer conn.Close() err = conn.(*net.TCPConn).CloseWrite() utest.IsNilNow(t, err) code := make([]byte, 3) _, err = io.ReadFull(conn, code) utest.IsNilNow(t, err) utest.EqualNow(t, string(code), string(codeBadReq)) }
func Test_ReadWrite(t *testing.T) { ReadWriteTest(t, 10000, func(r *Reader, w *Writer) { b := RandBytes(256) n, err := w.Write(b) utest.IsNilNow(t, err) utest.EqualNow(t, n, len(b)) c := make([]byte, len(b)) n, err = r.Read(c) utest.IsNilNow(t, err) utest.EqualNow(t, n, len(b)) utest.EqualNow(t, b, c) }) }
func Test_Buffer_ReadWrite(t *testing.T) { var buf Buffer for i := 0; i < 10000; i++ { b := RandBytes(256) buf.Grow(len(b)) n, err := buf.Write(b) utest.IsNilNow(t, err) utest.EqualNow(t, n, len(b)) c := make([]byte, len(b)) n, err = buf.Read(c) utest.IsNilNow(t, err) utest.EqualNow(t, n, len(b)) utest.EqualNow(t, b, c) } }
func SessionTest(t *testing.T, codecType CodecType, test func(*testing.T, *Session)) { server, err := Serve("tcp", "0.0.0.0:0", TestCodec{}) utest.IsNilNow(t, err) addr := server.listener.Addr().String() serverWait := new(sync.WaitGroup) go func() { for { session, err := server.Accept() if err != nil { break } serverWait.Add(1) go func() { io.Copy(session.conn, session.conn) serverWait.Done() }() } }() clientWait := new(sync.WaitGroup) testFunc := func() { session, err := Connect("tcp", addr, codecType) utest.IsNilNow(t, err) test(t, session) session.Close() clientWait.Done() } for i := 0; i < 30; i++ { clientWait.Add(1) go testFunc() } clientWait.Wait() for i := 0; i < 30; i++ { clientWait.Add(1) go testFunc() } clientWait.Wait() server.Stop() serverWait.Wait() }
func Test_Prepare(t *testing.T) { conn, err := Connect(TestConnParam) utest.IsNilNow(t, err) defer conn.Close() var ( stmt *Stmt res Result table DataTable reader DataReader ) stmt, err = conn.Prepare("SELECT * FROM test ORDER BY id ASC") utest.IsNilNow(t, err) table, err = stmt.QueryTable() utest.IsNilNow(t, err) rows := table.Rows() utest.EqualNow(t, len(rows), 10) for i := 0; i < 10; i++ { utest.EqualNow(t, rows[i][0].Int64(), int64(i)) utest.EqualNow(t, rows[i][1].String(), strconv.Itoa(i)) } reader, err = stmt.QueryReader() utest.IsNilNow(t, err) i := 0 for { row, err1 := reader.FetchNext() utest.IsNilNow(t, err1) if row == nil { break } utest.EqualNow(t, row[0].Int64(), int64(i)) utest.EqualNow(t, row[1].String(), strconv.Itoa(i)) i++ } utest.EqualNow(t, i, 10) stmt.Close() stmt, err = conn.Prepare("INSERT INTO test VALUES(?, ?)") utest.IsNilNow(t, err) stmt.BindInt(10) stmt.BindText("10") res, err = stmt.Execute() utest.IsNilNow(t, err) utest.EqualNow(t, res.RowsAffected(), 1) stmt.Close() }
func Test_CodeDialErr(t *testing.T) { conn, err := net.Dial("tcp", cfgGatewayAddr) utest.IsNilNow(t, err) defer conn.Close() encryptedAddr, err := aes256cbc.EncryptString("test", "0.0.0.0:0") utest.IsNilNow(t, err) _, err = conn.Write([]byte(encryptedAddr)) utest.IsNilNow(t, err) _, err = conn.Write([]byte("\n")) utest.IsNilNow(t, err) code := make([]byte, 3) _, err = io.ReadFull(conn, code) utest.IsNilNow(t, err) utest.EqualNow(t, string(code), string(codeDialErr)) }
func Test_QueryTable(t *testing.T) { conn, err := Connect(TestConnParam) utest.IsNilNow(t, err) defer conn.Close() var res DataTable res, err = conn.QueryTable("SELECT * FROM test ORDER BY id ASC") utest.IsNilNow(t, err) rows := res.Rows() utest.EqualNow(t, len(rows), 10) for i := 0; i < 10; i++ { utest.EqualNow(t, rows[i][0].Int64(), int64(i)) utest.EqualNow(t, rows[i][1].String(), strconv.Itoa(i)) } }
func Test_Connect(t *testing.T) { param := TestConnParam param.DbName = "mysql" conn, err := Connect(param) utest.IsNilNow(t, err) conn.Close() utest.Assert(t, conn.IsClosed()) }
func Test_BufioReader_ReadWrite(t *testing.T) { var buf bytes.Buffer var optimizer = BufioOptimizer{ R: bufio.NewReaderSize(&buf, 20), } for i := 0; i < 10000; i++ { b := RandBytes(256) buf.Write(b) r, err := optimizer.Next(len(b)) utest.IsNilNow(t, err) c := make([]byte, len(b)) n, err := r.Read(c) utest.IsNilNow(t, err) utest.EqualNow(t, n, len(b)) utest.EqualNow(t, b, c) } }
func Test_Null(t *testing.T) { conn, err := Connect(TestConnParam) utest.IsNilNow(t, err) defer conn.Close() var res DataTable res, err = conn.QueryTable("SELECT SUM(value) FROM test") utest.IsNilNow(t, err) rows1 := res.Rows() utest.EqualNow(t, len(rows1), 1) utest.Assert(t, !rows1[0][0].IsNull()) res, err = conn.QueryTable("SELECT SUM(value) FROM test WHERE id > 8888") utest.IsNilNow(t, err) rows2 := res.Rows() utest.EqualNow(t, len(rows2), 1) utest.Assert(t, rows2[0][0].IsNull()) }
func Test_DialTimeout(t *testing.T) { oldTimeout := cfgDialTimeout cfgDialTimeout = 10 defer func() { cfgDialTimeout = oldTimeout }() listener, err := net.Listen("tcp", "0.0.0.0:0") utest.IsNilNow(t, err) defer listener.Close() conn, err := net.Dial("tcp", cfgGatewayAddr) utest.IsNilNow(t, err) defer conn.Close() encryptedAddr, err := aes256cbc.EncryptString("test", listener.Addr().String()) utest.IsNilNow(t, err) _, err = conn.Write([]byte(encryptedAddr)) utest.IsNilNow(t, err) _, err = conn.Write([]byte("\n")) utest.IsNilNow(t, err) code := make([]byte, 3) _, err = io.ReadFull(conn, code) utest.IsNilNow(t, err) utest.EqualNow(t, string(code), string(codeDialTimeout)) }
func Test_Transfer(t *testing.T) { listener, err := net.Listen("tcp", "0.0.0.0:0") utest.IsNilNow(t, err) defer listener.Close() go func() { for { conn, err := listener.Accept() if err != nil { continue } go func() { defer conn.Close() io.Copy(conn, conn) }() } }() for i := 0; i < 20; i++ { conn, err := net.Dial("tcp", cfgGatewayAddr) utest.IsNilNow(t, err) defer conn.Close() encryptedAddr, err := aes256cbc.EncryptString(string(cfgSecret), listener.Addr().String()) utest.IsNilNow(t, err) _, err = conn.Write([]byte(encryptedAddr)) utest.IsNilNow(t, err) _, err = conn.Write([]byte("\nabc")) utest.IsNilNow(t, err) code := make([]byte, 6) _, err = io.ReadFull(conn, code) utest.IsNilNow(t, err) utest.EqualNow(t, string(code[:3]), string(codeOK)) utest.EqualNow(t, string(code[3:]), "abc") for j := 0; j < 10000; j++ { b1 := RandBytes(256) _, err = conn.Write(b1) utest.IsNilNow(t, err) b2 := make([]byte, len(b1)) _, err = io.ReadFull(conn, b2) utest.IsNilNow(t, err) utest.EqualNow(t, b1, b2) } } }
func Test_Fastbin(t *testing.T) { fbCodecType := Fastbin(4096, nil) fbCodecType.Register(TestFbService{}) SessionTest(t, fbCodecType, func(t *testing.T, session *Session) { for i := 0; i < 2000; i++ { var msgID byte = 1 if rand.Intn(100) < 50 { msgID = 2 } msg1 := RandObject() err := session.Send(&TestFbMessage{ msgID, msg1, }) utest.IsNilNow(t, err) var msg2 FbRequest err = session.Receive(&msg2) utest.IsNilNow(t, err) utest.EqualNow(t, msg1, msg2.message.(*TestFbMessage).TestObject) } }) }
func Test_BufioReader_String(t *testing.T) { var buf bytes.Buffer var optimizer = BufioOptimizer{ R: bufio.NewReaderSize(&buf, 20), } for i := 0; i < 10000; i++ { b := string(RandBytes(256)) buf.WriteString(b) r, err := optimizer.Next(len(b)) utest.IsNilNow(t, err) c := r.ReadString(len(b)) utest.EqualNow(t, b, c) } }
func BufioReaderTest(t *testing.T, f1 func(w *Writer), f2 func(r BinaryReader)) { var buf bytes.Buffer var optimizer = BufioOptimizer{ R: bufio.NewReaderSize(&buf, 20), } var w Writer w.Reset(&buf) for i := 0; i < 10000; i++ { f1(&w) } r, err := optimizer.Next(buf.Len()) utest.IsNilNow(t, err) for i := 0; i < 10000; i++ { f2(r) } }
func Test_Accept(t *testing.T) { _, err := accept(&TestListener{ 9, TestError{false, true}, }) utest.IsNilNow(t, err) _, err = accept(&TestListener{ -1, TestError{true, false}, }) utest.NotNilNow(t, err) func() { defer func() { err := recover() utest.NotNilNow(t, err) utest.Assert(t, strings.Contains(err.(string), "Gateway accept failed")) }() loop(&TestListener{ -1, TestError{true, false}, }) }() }