2021-02-06 19:08:01 +03:00
|
|
|
package wrapper_test
|
|
|
|
|
|
|
|
import (
|
2021-10-27 01:24:35 +03:00
|
|
|
"bytes"
|
2021-02-06 19:08:01 +03:00
|
|
|
"context"
|
2021-10-27 01:24:35 +03:00
|
|
|
"database/sql"
|
2023-01-16 19:44:09 +03:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
|
2023-01-07 19:16:48 +03:00
|
|
|
//"fmt"
|
2021-02-06 19:08:01 +03:00
|
|
|
"testing"
|
2021-10-27 01:24:35 +03:00
|
|
|
"time"
|
2021-02-06 19:08:01 +03:00
|
|
|
|
|
|
|
"github.com/jmoiron/sqlx"
|
2023-01-16 19:44:09 +03:00
|
|
|
"github.com/opentracing/opentracing-go"
|
|
|
|
"github.com/uber/jaeger-client-go"
|
|
|
|
"github.com/uber/jaeger-client-go/config"
|
2021-10-27 01:24:35 +03:00
|
|
|
vmeter "go.unistack.org/micro-meter-victoriametrics/v3"
|
2023-01-18 10:11:41 +03:00
|
|
|
ot "go.unistack.org/micro-tracer-opentracing/v3"
|
2021-10-27 01:24:35 +03:00
|
|
|
wrapper "go.unistack.org/micro-wrapper-sql/v3"
|
2023-01-07 19:16:48 +03:00
|
|
|
"go.unistack.org/micro/v3/logger"
|
2021-10-27 01:24:35 +03:00
|
|
|
"go.unistack.org/micro/v3/meter"
|
2023-01-16 19:44:09 +03:00
|
|
|
"go.unistack.org/micro/v3/tracer"
|
2021-10-27 01:24:35 +03:00
|
|
|
"modernc.org/sqlite"
|
2021-02-06 19:08:01 +03:00
|
|
|
)
|
|
|
|
|
2021-10-27 01:24:35 +03:00
|
|
|
var schema = `
|
2021-02-06 19:08:01 +03:00
|
|
|
CREATE TABLE IF NOT EXISTS person (
|
|
|
|
first_name text,
|
|
|
|
last_name text,
|
|
|
|
email text
|
|
|
|
);`
|
|
|
|
|
|
|
|
type Person struct {
|
|
|
|
FirstName string `db:"first_name"`
|
|
|
|
LastName string `db:"last_name"`
|
|
|
|
Email string `db:"email"`
|
|
|
|
}
|
|
|
|
|
2023-01-16 19:44:09 +03:00
|
|
|
func initJaeger(service string) (opentracing.Tracer, io.Closer) {
|
|
|
|
cfg := &config.Configuration{
|
|
|
|
ServiceName: service,
|
|
|
|
Sampler: &config.SamplerConfig{
|
|
|
|
Type: "const",
|
|
|
|
Param: 1,
|
|
|
|
},
|
|
|
|
Reporter: &config.ReporterConfig{
|
|
|
|
LogSpans: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
tracer, closer, err := cfg.NewTracer(config.Logger(jaeger.StdLogger))
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("ERROR: cannot init Jaeger: %v\n", err))
|
|
|
|
}
|
|
|
|
return tracer, closer
|
|
|
|
}
|
|
|
|
|
2021-02-06 19:08:01 +03:00
|
|
|
func TestWrapper(t *testing.T) {
|
2023-01-07 19:16:48 +03:00
|
|
|
ctx := context.Background()
|
2021-10-27 01:24:35 +03:00
|
|
|
wrapper.DefaultMeterStatsInterval = 100 * time.Millisecond
|
|
|
|
meter.DefaultMeter = vmeter.NewMeter()
|
2023-01-07 19:16:48 +03:00
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
logger.DefaultLogger = logger.NewLogger(logger.WithLevel(logger.DebugLevel), logger.WithOutput(buf))
|
|
|
|
|
|
|
|
if err := logger.DefaultLogger.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-10-27 01:24:35 +03:00
|
|
|
|
2023-01-18 10:11:41 +03:00
|
|
|
tr, c := initJaeger(fmt.Sprintf("Test tracing %s", time.Now().Format(time.RFC1123Z)))
|
2023-01-16 19:44:09 +03:00
|
|
|
defer c.Close()
|
|
|
|
opentracing.SetGlobalTracer(tr)
|
2023-01-18 10:11:41 +03:00
|
|
|
tracer.DefaultTracer = ot.NewTracer(ot.Tracer(tr))
|
2023-01-16 19:44:09 +03:00
|
|
|
if err := tracer.DefaultTracer.Init(); err != nil {
|
|
|
|
logger.Fatal(ctx, err)
|
|
|
|
}
|
|
|
|
|
2023-01-07 19:16:48 +03:00
|
|
|
sql.Register("micro-wrapper-sql", wrapper.NewWrapper(&sqlite.Driver{},
|
|
|
|
wrapper.DatabaseHost("localhost"),
|
|
|
|
wrapper.DatabaseName("memory"),
|
|
|
|
wrapper.LoggerLevel(logger.DebugLevel),
|
|
|
|
wrapper.LoggerEnabled(true),
|
|
|
|
))
|
2023-01-18 10:11:41 +03:00
|
|
|
wdb, err := sql.Open("micro-wrapper-sql", "test.db?cache=shared&mode=memory")
|
2021-02-06 19:08:01 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-10-27 01:24:35 +03:00
|
|
|
db := sqlx.NewDb(wdb, "sqlite")
|
2023-01-07 19:16:48 +03:00
|
|
|
var cancel func()
|
|
|
|
ctx, cancel = context.WithCancel(ctx)
|
2021-10-27 01:24:35 +03:00
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
wrapper.NewStatsMeter(ctx, db, wrapper.DatabaseHost("localhost"), wrapper.DatabaseName("memory"))
|
2023-01-18 10:11:41 +03:00
|
|
|
|
|
|
|
tx, err := wdb.BeginTx(ctx, nil)
|
|
|
|
if err != nil {
|
2021-10-27 01:24:35 +03:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2023-01-18 10:11:41 +03:00
|
|
|
if _, err := tx.ExecContext(wrapper.QueryName(ctx, "schema create"), schema); err != nil {
|
2023-01-16 19:44:09 +03:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2023-01-18 10:11:41 +03:00
|
|
|
|
|
|
|
if err := tx.Commit(); err != nil {
|
2021-10-27 01:24:35 +03:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-02-06 19:08:01 +03:00
|
|
|
|
2023-01-18 10:11:41 +03:00
|
|
|
tx, err = wdb.BeginTx(ctx, nil)
|
|
|
|
if err != nil {
|
2023-01-16 19:44:09 +03:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2023-01-18 10:11:41 +03:00
|
|
|
if _, err := tx.ExecContext(wrapper.QueryName(ctx, "insert one"), "INSERT INTO person (first_name, last_name, email) VALUES ($1, $2, $3)", "Fist1", "Last1", "Email1"); err != nil {
|
2021-10-27 01:24:35 +03:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2023-01-16 19:44:09 +03:00
|
|
|
|
2023-01-18 10:11:41 +03:00
|
|
|
if _, err := wdb.ExecContext(wrapper.QueryName(ctx, "double schema"), schema); err != nil {
|
2021-10-27 01:24:35 +03:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-02-06 19:08:01 +03:00
|
|
|
|
2023-01-18 10:11:41 +03:00
|
|
|
if _, err := tx.ExecContext(wrapper.QueryName(ctx, "insert two"), "INSERT INTO person (first_name, last_name, email) VALUES ($1, $2, $3)", "Fist2", "Last2", "Email2"); err != nil {
|
2023-01-16 19:44:09 +03:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2023-01-18 10:11:41 +03:00
|
|
|
if err := tx.Commit(); err != nil {
|
2021-10-27 01:24:35 +03:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-02-06 19:08:01 +03:00
|
|
|
|
2021-10-27 01:24:35 +03:00
|
|
|
var peoples []*Person
|
2023-01-07 19:16:48 +03:00
|
|
|
if err := sqlx.SelectContext(wrapper.QueryName(ctx, "get_all_person"), db, &peoples, "SELECT * FROM person limit 2"); err != nil {
|
2021-02-06 19:08:01 +03:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-10-27 01:24:35 +03:00
|
|
|
_ = peoples
|
|
|
|
time.Sleep(1 * time.Second)
|
2023-01-18 10:11:41 +03:00
|
|
|
|
2023-01-07 19:16:48 +03:00
|
|
|
mbuf := bytes.NewBuffer(nil)
|
|
|
|
_ = meter.DefaultMeter.Write(mbuf, meter.WriteProcessMetrics(true))
|
|
|
|
|
|
|
|
if !bytes.Contains(mbuf.Bytes(), []byte(`micro_sql_idle_connections`)) {
|
|
|
|
t.Fatalf("micro-wrapper-sql meter output contains invalid output: %s", buf.Bytes())
|
|
|
|
}
|
2021-10-27 01:24:35 +03:00
|
|
|
|
2023-01-07 19:16:48 +03:00
|
|
|
for _, tcase := range [][]byte{
|
|
|
|
[]byte(`"method":"ExecContext"`),
|
|
|
|
[]byte(`"method":"Open"`),
|
|
|
|
[]byte(`"method":"BeginTx"`),
|
|
|
|
[]byte(`"method":"Commit"`),
|
|
|
|
[]byte(`"method":"QueryContext"`),
|
|
|
|
[]byte(`"query":"get_all_person"`),
|
|
|
|
[]byte(`"took":`),
|
|
|
|
} {
|
|
|
|
if !bytes.Contains(buf.Bytes(), tcase) {
|
2023-01-18 10:11:41 +03:00
|
|
|
t.Fatalf("micro-wrapper-sql logger output miss %s in output: %s", tcase, buf.Bytes())
|
2023-01-07 19:16:48 +03:00
|
|
|
}
|
2021-10-27 01:24:35 +03:00
|
|
|
}
|
2023-01-16 19:44:09 +03:00
|
|
|
|
|
|
|
t.Logf("%s", buf.Bytes())
|
2021-02-06 19:08:01 +03:00
|
|
|
}
|