func SessionTest(t *testing.T, protocol ClientProtocol, codec CodecType, test func(*testing.T, *Session)) { server, err := Serve("tcp://0.0.0.0:0", Stream(), nil) unitest.NotError(t, err) addr := server.listener.Addr().String() serverWait := new(sync.WaitGroup) go server.Loop(func(session *Session) { serverWait.Add(1) Echo(session) serverWait.Done() }) clientWait := new(sync.WaitGroup) for i := 0; i < 60; i++ { clientWait.Add(1) go func() { session, err := Connect("tcp://"+addr, protocol, codec) unitest.NotError(t, err) test(t, session) session.Close() clientWait.Done() }() } clientWait.Wait() server.Stop() serverWait.Wait() MakeSureSessionGoroutineExit(t) }
func Test_Execute(t *testing.T) { param := TestConnParam param.DbName = "mysql" conn, err := Connect(param) unitest.NotError(t, err) defer conn.Close() _, err = conn.Execute("CREATE DATABASE " + TestConnParam.DbName) unitest.NotError(t, err) _, err = conn.Execute("USE " + TestConnParam.DbName) unitest.NotError(t, err) _, err = conn.Execute(`CREATE TABLE test ( id INT PRIMARY KEY, value VARCHAR(10) )`) unitest.NotError(t, err) for i := 0; i < 10; i++ { res, err := conn.Execute("INSERT INTO test VALUES(" + strconv.Itoa(i) + ",'" + strconv.Itoa(i) + "')") unitest.NotError(t, err) unitest.Pass(t, res.RowsAffected() == 1) } }
func Test_Simple(t *testing.T) { backend, err := StartEchoBackend() unitest.NotError(t, err) gateway := StartTestGateway(t, backend.Listener().Addr().String()) gatewayAddr := gateway.server.Listener().Addr().String() client, err := link.Connect("tcp://"+gatewayAddr, link.Packet(link.Uint16BE), link.Bytes()) unitest.NotError(t, err) for i := 0; i < 10000; i++ { msg1 := RandBytes(1024) err1 := client.Send(msg1) unitest.NotError(t, err1) var msg2 []byte err2 := client.Receive(&msg2) unitest.NotError(t, err2) if bytes.Equal(msg1, msg2) == false { t.Log(i, msg1, msg2) t.Fail() } unitest.Pass(t, bytes.Equal(msg1, msg2)) } client.Close() gateway.Stop() backend.Stop() time.Sleep(time.Second * 2) MakeSureSessionGoroutineExit(t) }
func StreamTest(t *testing.T, callback func(*link.Session)) { protocol := New(1024, 1024, 1024) server, err := link.Serve("tcp", "0.0.0.0:0", protocol) unitest.NotError(t, err) addr := server.Listener().Addr().String() go server.Serve(func(session *link.Session) { for { var msg TestMessage if err := session.Receive(&msg); err != nil { break } if err := session.Send(msg); err != nil { break } } }) session, err := link.Connect("tcp", addr, protocol) unitest.NotError(t, err) callback(session) session.Close() server.Stop() MakeSureSessionGoroutineExit(t) }
func PacketTest(t *testing.T, callback func(*link.Session)) { protocol := New(binary.SplitByUvarint, 1024, 1024, 1024) server, err := link.Serve("tcp", "0.0.0.0:0", protocol) unitest.NotError(t, err) go server.Serve(func(session *link.Session) { for { var msg RAW if err := session.Receive(&msg); err != nil { break } if err := session.Send(msg); err != nil { break } } }) session, err := link.Connect("tcp", server.Listener().Addr().String(), protocol) unitest.NotError(t, err) callback(session) session.Close() server.Stop() MakeSureSessionGoroutineExit(t) }
func Test_Execute(t *testing.T) { param := TestConnEnv param.DbName = "mysql" name, _ := json.Marshal(param) conn, err := sql.Open("mysql", string(name)) unitest.NotError(t, err) defer conn.Close() _, err = conn.Exec("CREATE DATABASE " + TestConnEnv.DbName) unitest.NotError(t, err) _, err = conn.Exec("USE " + TestConnEnv.DbName) unitest.NotError(t, err) _, err = conn.Exec(`CREATE TABLE test ( id INT PRIMARY KEY, value VARCHAR(10) )`) unitest.NotError(t, err) for i := 0; i < 10; i++ { res, err := conn.Exec("INSERT INTO test VALUES(" + strconv.Itoa(i) + ",'" + strconv.Itoa(i) + "')") unitest.NotError(t, err) num, _ := res.RowsAffected() unitest.Pass(t, num == 1) } }
func Test_QueryReader(t *testing.T) { conn, err := Connect(TestConnParam) unitest.NotError(t, err) defer conn.Close() var res DataReader res, err = conn.QueryReader("SELECT * FROM test ORDER BY id ASC") unitest.NotError(t, err) defer res.Close() i := 0 for { row, err1 := res.FetchNext() unitest.NotError(t, err1) if row == nil { break } unitest.Pass(t, row[0].Int64() == int64(i)) unitest.Pass(t, row[1].String() == strconv.Itoa(i)) i++ } unitest.Pass(t, i == 10) }
func Test_Connect(t *testing.T) { conn, err := sql.Open("mysql", TestConnParam) unitest.NotError(t, err) err = conn.Close() unitest.NotError(t, err) }
func Test_Query(t *testing.T) { conn, err := sql.Open("mysql", TestConnParam) unitest.NotError(t, err) defer conn.Close() var res *sql.Rows res, err = conn.Query("SELECT * FROM test ORDER BY id ASC") unitest.NotError(t, err) defer res.Close() i := 0 for res.Next() { var ( id int64 value string ) err := res.Scan(&id, &value) unitest.NotError(t, err) unitest.Pass(t, id == int64(i)) unitest.Pass(t, value == strconv.Itoa(i)) i++ } unitest.Pass(t, i == 10) }
func Test_Clean(t *testing.T) { conn, err := sql.Open("mysql", TestConnParam) unitest.NotError(t, err) defer conn.Close() _, err = conn.Exec("DROP DATABASE " + TestConnEnv.DbName) unitest.NotError(t, err) }
func Test_Clean(t *testing.T) { conn, err := Connect(TestConnParam) unitest.NotError(t, err) defer conn.Close() _, err = conn.Execute("DROP DATABASE " + TestConnParam.DbName) unitest.NotError(t, err) }
func Test_Uint32BE(t *testing.T) { ReadWriteTest(t, 10000, func(r *Reader, w *Writer) { v1 := uint32(rand.Intn(0xFFFFFFFF)) w.WriteUint32BE(v1) w.Flush() unitest.NotError(t, w.Error()) v2 := r.ReadUint32BE() unitest.NotError(t, r.Error()) unitest.Pass(t, v1 == v2) }) }
func Test_Float64LE(t *testing.T) { ReadWriteTest(t, 10000, func(r *Reader, w *Writer) { v1 := rand.NormFloat64() w.WriteFloat64LE(v1) w.Flush() unitest.NotError(t, w.Error()) v2 := r.ReadFloat64LE() unitest.NotError(t, r.Error()) unitest.Pass(t, v1 == v2) }) }
func Test_Varint(t *testing.T) { ReadWriteTest(t, 10000, func(r *Reader, w *Writer) { v1 := int64(rand.Int63n(0x7FFFFFFFFFFFFFFF)) w.WriteVarint(v1) w.Flush() unitest.NotError(t, w.Error()) v2 := r.ReadVarint() unitest.NotError(t, r.Error()) unitest.Pass(t, v1 == v2) }) }
func StringTest(t *testing.T, session *Session) { for i := 0; i < 2000; i++ { msg1 := string(RandBytes(512)) err := session.Send(msg1) unitest.NotError(t, err) var msg2 string err = session.Receive(&msg2) unitest.NotError(t, err) unitest.Pass(t, msg1 == msg2) } }
func Test_Uint24BE_Spliter(t *testing.T) { ReadWriteTest(t, 10000, func(r *Reader, w *Writer) { b1 := RandBytes(1024) w.WritePacket(b1, SplitByUint24BE) w.Flush() unitest.NotError(t, w.Error()) b2 := r.ReadPacket(SplitByUint24BE) unitest.NotError(t, r.Error()) unitest.Pass(t, bytes.Equal(b1, b2)) }) }
func BytesTest(t *testing.T, session *Session) { for i := 0; i < 2000; i++ { msg1 := RandBytes(512) err := session.Send(msg1) unitest.NotError(t, err) var msg2 = make([]byte, len(msg1)) err = session.Receive(msg2) unitest.NotError(t, err) unitest.Pass(t, bytes.Equal(msg1, msg2)) } }
func ObjectTest(t *testing.T, session *Session) { for i := 0; i < 2000; i++ { msg1 := RandObject() err := session.Send(&msg1) unitest.NotError(t, err) var msg2 TestObject err = session.Receive(&msg2) unitest.NotError(t, err) unitest.Pass(t, msg1 == msg2) } }
func Test_Gateway_Complex(t *testing.T) { backend := StartTestBackend(t, func(session *link.Session) { var msg packet.RAW for { if err := session.Receive(&msg); err != nil { break } if err := session.Send(msg); err != nil { break } } }) gateway := StartTestGateway(t, backend.Listener().Addr().String()) gatewayAddr := gateway.server.Listener().Addr().String() var wg sync.WaitGroup for i := 0; i < 20; i++ { wg.Add(1) go func() { defer wg.Done() client, err := link.Connect("tcp", gatewayAddr, protocol) unitest.NotError(t, err) for j := 0; j < 500; j++ { msg1 := RandBytes(1024) err1 := client.Send(packet.RAW(msg1)) unitest.NotError(t, err1) var msg2 packet.RAW err2 := client.Receive(&msg2) unitest.NotError(t, err2) if bytes.Equal(msg1, msg2) == false { t.Log(j, msg1, msg2) t.Fail() } unitest.Pass(t, bytes.Equal(msg1, msg2)) } client.Close() }() } wg.Wait() gateway.Stop() backend.Stop() time.Sleep(time.Second * 2) MakeSureSessionGoroutineExit(t) }
func Test_BytesStream(t *testing.T) { SessionTest(t, Stream(), Bytes(), func(t *testing.T, session *Session) { for i := 0; i < 2000; i++ { msg1 := RandBytes(1024) err := session.Send(msg1) unitest.NotError(t, err) var msg2 = make([]byte, len(msg1)) err = session.Receive(msg2) unitest.NotError(t, err) unitest.Pass(t, bytes.Equal(msg1, msg2)) } }) }
func Test_Packet(t *testing.T) { PacketTest(t, func(session *link.Session) { for i := 0; i < 100000; i++ { msg1 := RandBytes(1024) err := session.Send(RAW(msg1)) unitest.NotError(t, err) var msg2 RAW err = session.Receive(&msg2) unitest.NotError(t, err) unitest.Pass(t, bytes.Equal(msg1, msg2)) } }) }
func Test_BytesPacket(t *testing.T) { SessionTest(t, Packet(Uint16BE), Bytes(), func(t *testing.T, session *Session) { for i := 0; i < 2000; i++ { msg1 := RandBytes(1024) err := session.Send(msg1) unitest.NotError(t, err) var msg2 []byte err = session.Receive(&msg2) unitest.NotError(t, err) unitest.Pass(t, bytes.Equal(msg1, msg2)) } }) }
func Test_Stream(t *testing.T) { StreamTest(t, func(session *link.Session) { for i := 0; i < 100000; i++ { msg1 := RandBytes(1024) err := session.Send(TestMessage(msg1)) unitest.NotError(t, err) var msg2 TestMessage err = session.Receive(&msg2) unitest.NotError(t, err) unitest.Pass(t, bytes.Equal(msg1, msg2)) } }) }
func Test_GOB(t *testing.T) { StreamTest(t, func(session *link.Session) { for i := 0; i < 50000; i++ { msg1 := TestObject{ X: rand.Int(), Y: rand.Int(), Z: rand.Int(), } err := session.Send(GOB{msg1}) unitest.NotError(t, err) var msg2 TestObject err = session.Receive(GOB{&msg2}) unitest.NotError(t, err) unitest.Pass(t, msg1 == msg2) } }) }
func Test_PHP_Error(t *testing.T) { rpcErr, err := PHP(` $r = $client->Call("Arith.GetError"); echo $r->error; `) unitest.NotError(t, err) unitest.Pass(t, rpcErr == "Error!") }
func Test_PHP_HTTP(t *testing.T) { result, err := PHP(` $r = $client->Call("Arith.Multiply", array('A'=>7, 'B'=>8)); echo $r->result; `) unitest.NotError(t, err) unitest.Pass(t, result == "56") }
func StartTestBackend(t *testing.T, handler func(*link.Session)) *link.Server { backend, err := link.Serve("tcp", "0.0.0.0:0", NewBackend( 1024, 1024, 1024, )) unitest.NotError(t, err) go backend.Serve(handler) return backend }
func Test_Prepare(t *testing.T) { conn, err := Connect(TestConnParam) unitest.NotError(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") unitest.NotError(t, err) table, err = stmt.QueryTable() unitest.NotError(t, err) rows := table.Rows() unitest.Pass(t, len(rows) == 10) for i := 0; i < 10; i++ { unitest.Pass(t, rows[i][0].Int64() == int64(i)) unitest.Pass(t, rows[i][1].String() == strconv.Itoa(i)) } reader, err = stmt.QueryReader() unitest.NotError(t, err) i := 0 for { row, err1 := reader.FetchNext() unitest.NotError(t, err1) if row == nil { break } unitest.Pass(t, row[0].Int64() == int64(i)) unitest.Pass(t, row[1].String() == strconv.Itoa(i)) i++ } unitest.Pass(t, i == 10) stmt.Close() stmt, err = conn.Prepare("INSERT INTO test VALUES(?, ?)") unitest.NotError(t, err) stmt.BindInt(10) stmt.BindText("10") res, err = stmt.Execute() unitest.NotError(t, err) unitest.Pass(t, res.RowsAffected() == 1) stmt.Close() }
func StartTestGateway(t *testing.T, backendAddr string) *Frontend { listener, err := link.ListenPacket("tcp://0.0.0.0:0", link.Packet(link.Uint16BE)) unitest.NotError(t, err) var linkIds []uint64 gateway := NewFrontend(listener, func(_ *link.Session) (uint64, error) { return linkIds[rand.Intn(len(linkIds))], nil }) for i := 0; i < 1; i++ { id, err := gateway.AddBackend("tcp://"+backendAddr, link.Stream()) unitest.NotError(t, err) linkIds = append(linkIds, id) } return gateway }
func Test_QueryTable(t *testing.T) { conn, err := Connect(TestConnParam) unitest.NotError(t, err) defer conn.Close() var res DataTable res, err = conn.QueryTable("SELECT * FROM test ORDER BY id ASC") unitest.NotError(t, err) rows := res.Rows() unitest.Pass(t, len(rows) == 10) for i := 0; i < 10; i++ { unitest.Pass(t, rows[i][0].Int64() == int64(i)) unitest.Pass(t, rows[i][1].String() == strconv.Itoa(i)) } }