new test cases

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2023-01-16 19:44:09 +03:00
parent 9d7ffcc01a
commit 71f7338a41

View File

@ -4,15 +4,22 @@ import (
"bytes" "bytes"
"context" "context"
"database/sql" "database/sql"
"fmt"
"io"
//"fmt" //"fmt"
"testing" "testing"
"time" "time"
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
"github.com/opentracing/opentracing-go"
"github.com/uber/jaeger-client-go"
"github.com/uber/jaeger-client-go/config"
vmeter "go.unistack.org/micro-meter-victoriametrics/v3" vmeter "go.unistack.org/micro-meter-victoriametrics/v3"
wrapper "go.unistack.org/micro-wrapper-sql/v3" wrapper "go.unistack.org/micro-wrapper-sql/v3"
"go.unistack.org/micro/v3/logger" "go.unistack.org/micro/v3/logger"
"go.unistack.org/micro/v3/meter" "go.unistack.org/micro/v3/meter"
"go.unistack.org/micro/v3/tracer"
"modernc.org/sqlite" "modernc.org/sqlite"
) )
@ -29,6 +36,24 @@ type Person struct {
Email string `db:"email"` Email string `db:"email"`
} }
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
}
func TestWrapper(t *testing.T) { func TestWrapper(t *testing.T) {
ctx := context.Background() ctx := context.Background()
wrapper.DefaultMeterStatsInterval = 100 * time.Millisecond wrapper.DefaultMeterStatsInterval = 100 * time.Millisecond
@ -40,6 +65,16 @@ func TestWrapper(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
tr, c := initJaeger("Test tracing")
defer c.Close()
opentracing.SetGlobalTracer(tr)
tracer.DefaultTracer = &opentracingTracer{
tracer: tr,
}
if err := tracer.DefaultTracer.Init(); err != nil {
logger.Fatal(ctx, err)
}
sql.Register("micro-wrapper-sql", wrapper.NewWrapper(&sqlite.Driver{}, sql.Register("micro-wrapper-sql", wrapper.NewWrapper(&sqlite.Driver{},
wrapper.DatabaseHost("localhost"), wrapper.DatabaseHost("localhost"),
wrapper.DatabaseName("memory"), wrapper.DatabaseName("memory"),
@ -57,23 +92,42 @@ func TestWrapper(t *testing.T) {
defer cancel() defer cancel()
wrapper.NewStatsMeter(ctx, db, wrapper.DatabaseHost("localhost"), wrapper.DatabaseName("memory")) wrapper.NewStatsMeter(ctx, db, wrapper.DatabaseHost("localhost"), wrapper.DatabaseName("memory"))
if _, err := db.ExecContext(ctx, schema); err != nil { if _, err := wdb.ExecContext(wrapper.QueryName(ctx, "schema create"), schema); err != nil {
t.Fatal(err) t.Fatal(err)
} }
tx, err := db.BeginTxx(ctx, nil) fmt.Printf("begintx\n")
tx1, err := wdb.BeginTx(ctx, &sql.TxOptions{Isolation: sql.LevelReadCommitted})
if err != nil {
t.Fatal(err)
}
tx2, err := wdb.BeginTx(ctx, &sql.TxOptions{Isolation: sql.LevelReadCommitted})
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if _, err := tx.NamedExecContext(ctx, "INSERT OR REPLACE INTO person (first_name, last_name, email) VALUES (:first_name, :last_name, :email)", &Person{"Fist1", "Last1", "Email1"}); err != nil { fmt.Printf("exec1\n")
t.Fatal(err) if _, err := tx1.ExecContext(wrapper.QueryName(ctx, "insert one"), "INSERT OR REPLACE INTO person (first_name, last_name, email) VALUES ($1, $2, $3)", "Fist1", "Last1", "Email1"); err != nil {
}
if _, err := tx.NamedExecContext(ctx, "INSERT OR REPLACE INTO person (first_name, last_name, email) VALUES (:first_name, :last_name, :email)", &Person{"Fist2", "Last2", "Email2"}); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if err := tx.Commit(); err != nil { fmt.Printf("exec none\n")
if _, err := wdb.ExecContext(wrapper.QueryName(ctx, "double schema"), schema); err != nil {
t.Fatal(err)
}
fmt.Printf("exec2\n")
if _, err := tx2.ExecContext(wrapper.QueryName(ctx, "insert two"), "INSERT OR REPLACE INTO person (first_name, last_name, email) VALUES ($1, $2, $3)", "Fist2", "Last2", "Email2"); err != nil {
t.Fatal(err)
}
fmt.Printf("commit1\n")
if err := tx1.Commit(); err != nil {
t.Fatal(err)
}
fmt.Printf("commit2\n")
if err := tx2.Commit(); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -104,4 +158,6 @@ func TestWrapper(t *testing.T) {
t.Fatalf("micro-wrapper-sql logger output contains invalid output: %s", buf.Bytes()) t.Fatalf("micro-wrapper-sql logger output contains invalid output: %s", buf.Bytes())
} }
} }
t.Logf("%s", buf.Bytes())
} }