mdb/mdatastore: refactor to use new mctx/mlog/mcfg stuff
This commit is contained in:
parent
40794d83b8
commit
8ddf1348db
@ -3,13 +3,12 @@
|
||||
package mdatastore
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"cloud.google.com/go/datastore"
|
||||
"github.com/mediocregopher/mediocre-go-lib/m"
|
||||
"github.com/mediocregopher/mediocre-go-lib/mcfg"
|
||||
"github.com/mediocregopher/mediocre-go-lib/mctx"
|
||||
"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/mrun"
|
||||
)
|
||||
|
||||
// Datastore is a wrapper around a datastore client providing more
|
||||
@ -21,23 +20,38 @@ type Datastore struct {
|
||||
log *mlog.Logger
|
||||
}
|
||||
|
||||
// Cfg configures and returns a Datastore instance which will be usable once
|
||||
// StartRun is called on the passed in Cfg instance.
|
||||
func Cfg(cfg *mcfg.Cfg) *Datastore {
|
||||
cfg = cfg.Child("datastore")
|
||||
var ds Datastore
|
||||
ds.gce = mdb.CfgGCE(cfg)
|
||||
ds.log = m.Log(cfg, &ds)
|
||||
cfg.Start.Then(func(ctx context.Context) error {
|
||||
// MNew returns a Datastore instance which will be initialized and configured
|
||||
// when the start event is triggered on ctx (see mrun.Start). The Datastore
|
||||
// instance will have Close called on it when the stop event is triggered on ctx
|
||||
// (see mrun.Stop).
|
||||
//
|
||||
// gce is optional and can be passed in if there's an existing gce object which
|
||||
// should be used, otherwise a new one will be created with mdb.MGCE.
|
||||
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")
|
||||
var err error
|
||||
ds.Client, err = datastore.NewClient(ctx, ds.gce.Project, ds.gce.ClientOptions()...)
|
||||
return mlog.ErrWithKV(err, &ds)
|
||||
ds.Client, err = datastore.NewClient(innerCtx, ds.gce.Project, ds.gce.ClientOptions()...)
|
||||
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.
|
||||
func (ds *Datastore) KV() mlog.KV {
|
||||
func (ds *Datastore) KV() map[string]interface{} {
|
||||
return ds.gce.KV()
|
||||
}
|
||||
|
@ -1,31 +1,20 @@
|
||||
package mdatastore
|
||||
|
||||
import (
|
||||
"context"
|
||||
. "testing"
|
||||
"time"
|
||||
|
||||
"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/mtest"
|
||||
"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
|
||||
func TestBasic(t *T) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
ctx := mtest.NewCtx()
|
||||
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)
|
||||
@ -37,14 +26,15 @@ func TestBasic(t *T) {
|
||||
B: mrand.Int(),
|
||||
}
|
||||
|
||||
if _, err := testDS.Put(ctx, key, &val); err != nil {
|
||||
if _, err := ds.Put(ctx, key, &val); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
var val2 valType
|
||||
if err := testDS.Get(ctx, key, &val2); err != nil {
|
||||
if err := ds.Get(ctx, key, &val2); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
massert.Fatal(t, massert.Equal(val, val2))
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user