Skip to content

Commit

Permalink
client test done
Browse files Browse the repository at this point in the history
  • Loading branch information
yuanbohan committed Jan 31, 2024
1 parent b1dafe9 commit bdc5e7d
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 103 deletions.
30 changes: 0 additions & 30 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package client

import (
"context"
"fmt"

greptimepb "github.com/GreptimeTeam/greptime-proto/go/greptime/v1"
"google.golang.org/grpc"
Expand Down Expand Up @@ -47,35 +46,6 @@ func New(cfg *config.Config) (*Client, error) {

func (c *Client) Write(ctx context.Context, tables ...*table.Table) (*greptimepb.GreptimeResponse, error) {
req, err := request.New(tables...).Build(c.cfg)
fmt.Printf("dbname: %#v\n", req.Header.GetDbname())

inserts := req.GetRowInserts().GetInserts()

for _, insert := range inserts {
fmt.Printf("table name: %q\n", insert.GetTableName())

fmt.Println("columns:")
for _, schema := range insert.Rows.GetSchema() {
fmt.Printf("name: %#v\n", schema.GetColumnName())
fmt.Printf("semantic: %#v\n", schema.GetSemanticType())
fmt.Printf("type: %#v\n", schema.GetDatatype())
}

fmt.Println()
fmt.Println()
fmt.Println("rows:")

for _, row := range insert.GetRows().GetRows() {
for _, val := range row.GetValues() {
fmt.Printf("value: %#v\n", val.String())
}
fmt.Println()
fmt.Println()
}

fmt.Println()

}
if err != nil {
return nil, err
}
Expand Down
120 changes: 68 additions & 52 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ import (
"context"
"fmt"
"log"
"net/http"
"net/url"
"strconv"
"strings"
"testing"
"time"

Expand All @@ -39,9 +36,14 @@ import (
)

var (
timezone = "UTC"
tableName = "test_insert_monitor"
database = "public"
host = "127.0.0.1"
httpPort, grpcPort, mysqlPort = 4000, 4001, 4002

cli *Client
db *Mysql
)

type monitor struct {
Expand All @@ -55,26 +57,26 @@ type monitor struct {
}

func (monitor) TableName() string {
return "monitor"
return tableName
}

type mysql_ struct {
type Mysql struct {
Host string
Port string
Port int
User string
Password string
Database string

DB *gorm.DB
}

func (m *mysql_) Setup() error {
func (m *Mysql) Setup() error {
if m.DB != nil {
return nil
}

dsn := fmt.Sprintf("tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local",
m.Host, m.Port, m.Database)
dsn := fmt.Sprintf("tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=%s",
m.Host, m.Port, m.Database, timezone)
if m.User != "" && m.Password != "" {
dsn = fmt.Sprintf("%s:%s@%s", m.User, m.Password, dsn)
}
Expand All @@ -87,17 +89,46 @@ func (m *mysql_) Setup() error {
return nil
}

func (p *mysql_) AllMonitors() ([]monitor, error) {
func (p *Mysql) AllMonitors() ([]monitor, error) {
var monitors []monitor
err := p.DB.Find(&monitors).Error
return monitors, err
}

func INIT() {
func newClient() *Client {
options := []grpc.DialOption{
grpc.WithTransportCredentials(insecure.NewCredentials()),
}
cfg := config.New(host).
WithPort(grpcPort).
WithDatabase(database).
WithDialOptions(options...)

client, err := New(cfg)
if err != nil {
log.Fatalf("failed to create client: %s", err.Error())
}
return client
}

func newMysql() *Mysql {
db := &Mysql{
Host: host,
Port: mysqlPort,
User: "",
Password: "",
Database: database,
}
if err := db.Setup(); err != nil {
log.Fatalln("failed to setup mysql" + err.Error())
}
return db
}

func init() {
repo := "greptime/greptimedb"
tag := "v0.6.0"

var err error
pool, err := dockertest.NewPool("")
if err != nil {
log.Fatalln("Could not connect to docker: " + err.Error())
Expand Down Expand Up @@ -136,61 +167,37 @@ func INIT() {
time.Sleep(time.Second * 5)
httpPort, err = strconv.Atoi(resource.GetPort(("4000/tcp")))
grpcPort, err = strconv.Atoi(resource.GetPort(("4001/tcp")))
mysqlPort, err = strconv.Atoi(resource.GetPort(("4002/tcp")))
if err != nil {
return err
}
return nil
}); err != nil {
log.Fatalf("Could not connect to docker: %s", err)
}
}

func newClient(t *testing.T) *Client {
options := []grpc.DialOption{
grpc.WithTransportCredentials(insecure.NewCredentials()),
}
cfg := config.New(host).WithPort(grpcPort).WithDatabase(database).WithDialOptions(options...)
client, err := New(cfg)
assert.Nil(t, err)
return client
}
log.Printf("Container started, http port: %d, grpc port: %d, mysql port: %d\n", httpPort, grpcPort, mysqlPort)

func newMysql(t *testing.T) *mysql_ {
mysql := &mysql_{
Host: "127.0.0.1",
Port: "4002",
User: "",
Password: "",
Database: "public",
}
if err := mysql.Setup(); err != nil {
log.Fatalln("failed to setup mysql" + err.Error())
}
return mysql
cli = newClient()
db = newMysql()
}

func createTable(t *testing.T, schema string) {
data := url.Values{}
data.Set("sql", schema)
body := strings.NewReader(data.Encode())
uri := fmt.Sprintf("http://localhost:%d/v1/sql?db=%s", httpPort, database)
resp, err := http.DefaultClient.Post(uri, "application/x-www-form-urlencoded", body)
func TestInsertMonitor(t *testing.T) {
loc, err := time.LoadLocation(timezone)
assert.Nil(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
ts1 := time.Now().Add(-1 * time.Minute).UnixMilli()
time1 := time.UnixMilli(ts1).In(loc)
ts2 := time.Now().Add(-2 * time.Minute).UnixMilli()
time2 := time.UnixMilli(ts2).In(loc)

defer resp.Body.Close()
}

func TestInsertMonitor(t *testing.T) {
tableName := "test_insert_monitor"
monitors := []monitor{
{
ID: 1,
Host: "127.0.0.1",
Memory: 1,
Cpu: 1.0,
Temperature: -1,
Ts: time.Now(),
Ts: time1,
Running: true,
},
{
Expand All @@ -199,7 +206,7 @@ func TestInsertMonitor(t *testing.T) {
Memory: 2,
Cpu: 2.0,
Temperature: -2,
Ts: time.Now(),
Ts: time2,
Running: true,
},
}
Expand All @@ -222,9 +229,18 @@ func TestInsertMonitor(t *testing.T) {
assert.Nil(t, err)
}

client := newClient(t)
resp, err := client.Write(context.Background(), table)
fmt.Printf("--------- err: %#v\n", err)
resp, err := cli.Write(context.Background(), table)
assert.Nil(t, err)
fmt.Printf("--------- resp: %#v\n", resp)
assert.Zero(t, resp.GetHeader().GetStatus().GetStatusCode())
assert.Empty(t, resp.GetHeader().GetStatus().GetErrMsg())
assert.Equal(t, uint32(len(monitors)), resp.GetAffectedRows().GetValue())

monitors_, err := db.AllMonitors()
assert.Nil(t, err)

assert.Equal(t, len(monitors), len(monitors_))

for i, monitor_ := range monitors_ {
assert.Equal(t, monitors[i], monitor_)
}
}
4 changes: 2 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ import (
// - DialOptions and CallOptions are for gRPC service.
// You can specify them or leave them empty.
type Config struct {
Host string // example: 127.0.0.1
Host string // no scheme or port included. example: 127.0.0.1
Port int // default: 4001
Username string
Password string
Database string // the default database for client
Database string // the default database

// DialOptions are passed to grpc.DialContext
// when a new gRPC connection is to be created.
Expand Down
18 changes: 7 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,12 @@ module github.com/GreptimeTeam/greptimedb-ingester-go
go 1.20

require (
github.com/GreptimeTeam/greptime-proto v0.4.2
github.com/GreptimeTeam/greptime-proto v0.4.3
github.com/stoewer/go-strcase v1.3.0
github.com/stretchr/testify v1.8.4
google.golang.org/grpc v1.56.3
)

require (
github.com/go-sql-driver/mysql v1.7.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
gorm.io/gorm v1.25.6 // indirect
gorm.io/driver/mysql v1.5.2
gorm.io/gorm v1.25.6
)

require (
Expand All @@ -27,22 +22,24 @@ require (
github.com/docker/docker v20.10.7+incompatible // indirect
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.4.0 // indirect
github.com/go-sql-driver/mysql v1.7.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/mitchellh/mapstructure v1.4.1 // indirect
github.com/moby/term v0.0.0-20201216013528-df9cb8a40635 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.2 // indirect
github.com/opencontainers/runc v1.1.5 // indirect
github.com/ory/dockertest/v3 v3.10.0 // indirect
github.com/ory/dockertest/v3 v3.10.0
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.10.0 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
Expand All @@ -56,5 +53,4 @@ require (
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v2 v2.3.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gorm.io/driver/mysql v1.5.2
)
9 changes: 6 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78/go.mod h1:LmzpDX
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/GreptimeTeam/greptime-proto v0.4.2 h1:oX6N1v6+bYQDrh/QM+JmMHqEWjlxAVogCII15JGqbpQ=
github.com/GreptimeTeam/greptime-proto v0.4.2/go.mod h1:jk5XBR9qIbSBiDF2Gix1KALyIMCVktcpx91AayOWxmE=
github.com/GreptimeTeam/greptime-proto v0.4.3 h1:qJC2j03AfP2YbfVrBBlB6t+Sd1RvMG6zKVtGUwtj7N0=
github.com/GreptimeTeam/greptime-proto v0.4.3/go.mod h1:jk5XBR9qIbSBiDF2Gix1KALyIMCVktcpx91AayOWxmE=
github.com/Microsoft/go-winio v0.6.0 h1:slsWYD/zyx7lCXoZVlvQrj0hPTM1HI4+v1sIda2yDvg=
github.com/Microsoft/go-winio v0.6.0/go.mod h1:cTAf44im0RAYeL23bpB+fzCyDH2MJiz2BO69KH/soAE=
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 h1:TngWCqHvy9oXAN6lEVMRuU21PR1EtLVZJmdB18Gu3Rw=
Expand All @@ -17,6 +19,7 @@ github.com/containerd/continuity v0.3.0/go.mod h1:wJEAIwKOm/pBZuBd0JmeTvnLquTB1A
github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/creack/pty v1.1.11 h1:07n33Z8lZxZ2qwegKbObQohDhXDQxiMMz1NOUGYlesw=
github.com/creack/pty v1.1.11/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/cyphar/filepath-securejoin v0.2.3/go.mod h1:aPGpWjXOXUn2NCNjFvBE6aRxGGx79pTxQpKOJNYHHl4=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -62,6 +65,7 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/lib/pq v0.0.0-20180327071824-d34b9ff171c2 h1:hRGSmZu7j271trc9sneMrpOW7GN5ngLm8YUZIPzf394=
github.com/mitchellh/mapstructure v1.4.1 h1:CpVNEelQCZBooIPDn+AR3NpivK/TIKU8bDxdASFVQag=
github.com/mitchellh/mapstructure v1.4.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/moby/sys/mountinfo v0.5.0/go.mod h1:3bMD3Rg+zkqx8MRYPi7Pyb0Ie97QEBmdxbhnCLlSvSU=
Expand Down Expand Up @@ -97,13 +101,11 @@ github.com/stoewer/go-strcase v1.3.0 h1:g0eASXYtp+yvN9fK8sH94oCIk0fau9uV1/ZdJ0AV
github.com/stoewer/go-strcase v1.3.0/go.mod h1:fAH5hQ5pehh+j3nZfvwdk2RgEgQjAoM8wodgtPmh1xo=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
Expand Down Expand Up @@ -137,6 +139,7 @@ golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190606203320-7fc4e5ec1444/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down Expand Up @@ -187,8 +190,8 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/driver/mysql v1.5.2 h1:QC2HRskSE75wBuOxe0+iCkyJZ+RqpudsQtqkp+IMuXs=
gorm.io/driver/mysql v1.5.2/go.mod h1:pQLhh1Ut/WUAySdTHwBpBv6+JKcj+ua4ZFx1QQTBzb8=
gorm.io/gorm v1.25.2-0.20230530020048-26663ab9bf55 h1:sC1Xj4TYrLqg1n3AN10w871An7wJM0gzgcm8jkIkECQ=
gorm.io/gorm v1.25.2-0.20230530020048-26663ab9bf55/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=
gorm.io/gorm v1.25.6 h1:V92+vVda1wEISSOMtodHVRcUIOPYa2tgQtyF+DfFx+A=
gorm.io/gorm v1.25.6/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
gotest.tools/v3 v3.0.2/go.mod h1:3SzNCllyD9/Y+b5r9JIKQ474KzkZyqLqEfYqMsX94Bk=
gotest.tools/v3 v3.3.0 h1:MfDY1b1/0xN1CyMlQDac0ziEy9zJQd9CXBRRDHw2jJo=
1 change: 0 additions & 1 deletion request/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ func (r *Request) Build(cfg *config.Config) (*gpb.GreptimeRequest, error) {
req := &gpb.GreptimeRequest_RowInserts{
RowInserts: &gpb.RowInsertRequests{Inserts: reqs},
}

return &gpb.GreptimeRequest{Header: header, Request: req}, nil

}
Loading

0 comments on commit bdc5e7d

Please sign in to comment.