mdb/mdatastore: refactor to use new mctx/mlog/mcfg stuff

This commit is contained in:
Brian Picciano 2019-02-03 16:05:33 -05:00
parent 40794d83b8
commit 8ddf1348db
2 changed files with 54 additions and 50 deletions

View File

@ -3,13 +3,12 @@
package mdatastore package mdatastore
import ( import (
"context"
"cloud.google.com/go/datastore" "cloud.google.com/go/datastore"
"github.com/mediocregopher/mediocre-go-lib/m" "github.com/mediocregopher/mediocre-go-lib/mctx"
"github.com/mediocregopher/mediocre-go-lib/mcfg"
"github.com/mediocregopher/mediocre-go-lib/mdb" "github.com/mediocregopher/mediocre-go-lib/mdb"
"github.com/mediocregopher/mediocre-go-lib/merr"
"github.com/mediocregopher/mediocre-go-lib/mlog" "github.com/mediocregopher/mediocre-go-lib/mlog"
"github.com/mediocregopher/mediocre-go-lib/mrun"
) )
// Datastore is a wrapper around a datastore client providing more // Datastore is a wrapper around a datastore client providing more
@ -21,23 +20,38 @@ type Datastore struct {
log *mlog.Logger log *mlog.Logger
} }
// Cfg configures and returns a Datastore instance which will be usable once // MNew returns a Datastore instance which will be initialized and configured
// StartRun is called on the passed in Cfg instance. // when the start event is triggered on ctx (see mrun.Start). The Datastore
func Cfg(cfg *mcfg.Cfg) *Datastore { // instance will have Close called on it when the stop event is triggered on ctx
cfg = cfg.Child("datastore") // (see mrun.Stop).
var ds Datastore //
ds.gce = mdb.CfgGCE(cfg) // gce is optional and can be passed in if there's an existing gce object which
ds.log = m.Log(cfg, &ds) // should be used, otherwise a new one will be created with mdb.MGCE.
cfg.Start.Then(func(ctx context.Context) error { func MNew(ctx mctx.Context, gce *mdb.GCE) *Datastore {
if gce == nil {
gce = mdb.MGCE(ctx, "")
}
ctx = mctx.ChildOf(ctx, "datastore")
ds := &Datastore{
gce: gce,
log: mlog.From(ctx),
}
ds.log.SetKV(ds)
mrun.OnStart(ctx, func(innerCtx mctx.Context) error {
ds.log.Info("connecting to datastore") ds.log.Info("connecting to datastore")
var err error var err error
ds.Client, err = datastore.NewClient(ctx, ds.gce.Project, ds.gce.ClientOptions()...) ds.Client, err = datastore.NewClient(innerCtx, ds.gce.Project, ds.gce.ClientOptions()...)
return mlog.ErrWithKV(err, &ds) return merr.WithKV(err, ds.KV())
}) })
return &ds mrun.OnStop(ctx, func(mctx.Context) error {
return ds.Client.Close()
})
return ds
} }
// KV implements the mlog.KVer interface. // KV implements the mlog.KVer interface.
func (ds *Datastore) KV() mlog.KV { func (ds *Datastore) KV() map[string]interface{} {
return ds.gce.KV() return ds.gce.KV()
} }

View File

@ -1,50 +1,40 @@
package mdatastore package mdatastore
import ( import (
"context"
. "testing" . "testing"
"time"
"cloud.google.com/go/datastore" "cloud.google.com/go/datastore"
"github.com/mediocregopher/mediocre-go-lib/mcfg"
"github.com/mediocregopher/mediocre-go-lib/mdb"
"github.com/mediocregopher/mediocre-go-lib/mrand" "github.com/mediocregopher/mediocre-go-lib/mrand"
"github.com/mediocregopher/mediocre-go-lib/mtest"
"github.com/mediocregopher/mediocre-go-lib/mtest/massert" "github.com/mediocregopher/mediocre-go-lib/mtest/massert"
) )
var testDS *Datastore
func init() {
mdb.DefaultGCEProject = "test"
cfg := mcfg.New()
testDS = Cfg(cfg)
cfg.StartTestRun()
}
// Requires datastore emulator to be running // Requires datastore emulator to be running
func TestBasic(t *T) { func TestBasic(t *T) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) ctx := mtest.NewCtx()
defer cancel() mtest.SetEnv(ctx, "GCE_PROJECT", "test")
ds := MNew(ctx, nil)
mtest.Run(ctx, t, func() {
name := mrand.Hex(8)
key := datastore.NameKey("testKind", name, nil)
key.Namespace = "TestBasic_" + mrand.Hex(8)
type valType struct {
A, B int
}
val := valType{
A: mrand.Int(),
B: mrand.Int(),
}
name := mrand.Hex(8) if _, err := ds.Put(ctx, key, &val); err != nil {
key := datastore.NameKey("testKind", name, nil) t.Fatal(err)
key.Namespace = "TestBasic_" + mrand.Hex(8) }
type valType struct {
A, B int
}
val := valType{
A: mrand.Int(),
B: mrand.Int(),
}
if _, err := testDS.Put(ctx, key, &val); err != nil { var val2 valType
t.Fatal(err) if err := ds.Get(ctx, key, &val2); err != nil {
} t.Fatal(err)
}
var val2 valType massert.Fatal(t, massert.Equal(val, val2))
if err := testDS.Get(ctx, key, &val2); err != nil { })
t.Fatal(err)
}
massert.Fatal(t, massert.Equal(val, val2))
} }