Esempio n. 1
0
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)
}
Esempio n. 2
0
func Test_SyncPool_AllocSmall(t *testing.T) {
	pool := NewSyncPool(128, 1024, 2)
	mem := pool.Alloc(64)
	utest.EqualNow(t, len(mem), 64)
	utest.EqualNow(t, cap(mem), 128)
	pool.Free(mem)
}
Esempio n. 3
0
func Test_SyncPool_AllocLarge(t *testing.T) {
	pool := NewSyncPool(128, 1024, 2)
	mem := pool.Alloc(2048)
	utest.EqualNow(t, len(mem), 2048)
	utest.EqualNow(t, cap(mem), 2048)
	pool.Free(mem)
}
Esempio n. 4
0
func Test_Cmd(t *testing.T) {
	Register("lookup api", "save api time record to file", func(args []string) interface{} {
		utest.EqualNow(t, args[0], "lookup api")
		return 1
	})

	Register("lookup gc", "save gc summary to file", func(args []string) interface{} {
		utest.EqualNow(t, args[0], "lookup gc")
		return 2
	})

	Register("lookup heap ([0-2])", "save heap status to file", func(args []string) interface{} {
		utest.EqualNow(t, args[1], "1")
		return 3
	})

	n, ok := Process("lookup api")
	utest.Assert(t, ok && n == 1, n)
	Process("lookup gc")

	n, ok = Process("lookup gc")
	utest.Assert(t, ok && n == 2, n)

	n, ok = Process("lookup heap 1")
	utest.Assert(t, ok && n == 3, n)
}
Esempio n. 5
0
func Test_LockPool_AllocSlow(t *testing.T) {
	pool := NewLockPool(128, 1024, 2, 1024)
	mem := pool.classes[len(pool.classes)-1].Pop()
	utest.EqualNow(t, cap(mem), 1024)

	mem = pool.Alloc(1024)
	utest.EqualNow(t, cap(mem), 1024)
}
Esempio n. 6
0
func Test_AtomPool_AllocSlow(t *testing.T) {
	pool := NewAtomPool(128, 1024, 2, 1024)
	mem := pool.classes[len(pool.classes)-1].Pop()
	utest.EqualNow(t, cap(mem), 1024)
	utest.Assert(t, pool.classes[len(pool.classes)-1].head == 0)

	mem = pool.Alloc(1024)
	utest.EqualNow(t, cap(mem), 1024)
}
Esempio n. 7
0
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)
		}
	}
}
Esempio n. 8
0
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)
	})
}
Esempio n. 9
0
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)
	}
}
Esempio n. 10
0
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))
}
Esempio n. 11
0
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)
	}
}
Esempio n. 12
0
func Test_Buffer_Uvarint(t *testing.T) {
	var buf = Buffer{Data: make([]byte, 10)}
	v1 := uint64(rand.Int63n(0x7FFFFFFFFFFFFFFF))
	buf.WriteUvarint(v1)

	v2 := buf.ReadUvarint()
	utest.EqualNow(t, v1, v2)
}
Esempio n. 13
0
func Test_Buffer_Uint24LE(t *testing.T) {
	var buf = Buffer{Data: make([]byte, 10)}
	v1 := uint32(rand.Intn(0xFFFFFF))
	buf.WriteUint24LE(v1)

	v2 := buf.ReadUint24LE()
	utest.EqualNow(t, v1, v2)
}
Esempio n. 14
0
func Test_Buffer_IntLE(t *testing.T) {
	var buf = Buffer{Data: make([]byte, 10)}
	v1 := int(rand.Int63n(0x7FFFFFFFFFFFFFFF))
	buf.WriteIntLE(v1)

	v2 := buf.ReadIntLE()
	utest.EqualNow(t, v1, v2)
}
Esempio n. 15
0
func Test_Buffer_Int8(t *testing.T) {
	var buf = Buffer{Data: make([]byte, 10)}
	v1 := int8(rand.Intn(256))
	buf.WriteInt8(v1)

	v2 := buf.ReadInt8()
	utest.EqualNow(t, v1, v2)
}
Esempio n. 16
0
func Test_Buffer_Float64LE(t *testing.T) {
	var buf = Buffer{Data: make([]byte, 10)}
	v1 := rand.NormFloat64()
	buf.WriteFloat64LE(v1)

	v2 := buf.ReadFloat64LE()
	utest.EqualNow(t, v1, v2)
}
Esempio n. 17
0
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()
}
Esempio n. 18
0
func Test_UvarintSize(t *testing.T) {
	b := make([]byte, MaxVarintLen64)
	for i := 0; i < 100000; i++ {
		v := uint64(rand.Int63())
		size1 := UvarintSize(v)
		size2 := PutUvarint(b, v)
		utest.EqualNow(t, size1, size2)
	}
}
Esempio n. 19
0
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))
	}
}
Esempio n. 20
0
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)
	})
}
Esempio n. 21
0
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)
	}
}
Esempio n. 22
0
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)
	})
}
Esempio n. 23
0
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)
	})
}
Esempio n. 24
0
func Test_Buffer_String(t *testing.T) {
	var buf Buffer
	for i := 0; i < 10000; i++ {
		b := string(RandBytes(256))
		buf.Grow(len(b))

		buf.WriteString(b)

		c := buf.ReadString(len(b))
		utest.EqualNow(t, b, c)
	}
}
Esempio n. 25
0
func Test_Buffer_Bytes(t *testing.T) {
	var buf Buffer
	for i := 0; i < 10000; i++ {
		b := RandBytes(256)
		buf.Grow(len(b))

		buf.WriteBytes(b)

		c := buf.ReadBytes(len(b))
		utest.EqualNow(t, b, c)
	}
}
Esempio n. 26
0
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)
	})
}
Esempio n. 27
0
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)
	}
}
Esempio n. 28
0
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())
}
Esempio n. 29
0
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)
	}
}
Esempio n. 30
0
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))
}