Skip to content

Commit

Permalink
fix integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
Gregory Russell committed Nov 20, 2019
1 parent 81978dd commit 6f54c24
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 62 deletions.
76 changes: 76 additions & 0 deletions tracker/testClient_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package tracker_test

import (
"context"
"reflect"
"sync"

"cloud.google.com/go/datastore"
"github.com/GoogleCloudPlatform/google-cloud-go-testing/datastore/dsiface"
)

func validateDatastoreEntity(e interface{}) error {
v := reflect.ValueOf(e)
if v.Kind() != reflect.Ptr {
return datastore.ErrInvalidEntityType
}
if reflect.Indirect(v).Kind() != reflect.Struct {
return datastore.ErrInvalidEntityType
}
return nil
}

type testClient struct {
dsiface.Client // For unimplemented methods
lock sync.Mutex
objects map[datastore.Key]reflect.Value
}

func newTestClient() *testClient {
return &testClient{objects: make(map[datastore.Key]reflect.Value, 10)}
}

func (c *testClient) Close() error { return nil }

func (c *testClient) Count(ctx context.Context, q *datastore.Query) (n int, err error) {
return 0, ErrNotImplemented
}

func (c *testClient) Delete(ctx context.Context, key *datastore.Key) error {
c.lock.Lock()
defer c.lock.Unlock()
_, ok := c.objects[*key]
if !ok {
return datastore.ErrNoSuchEntity
}
delete(c.objects, *key)
return nil
}

func (c *testClient) Get(ctx context.Context, key *datastore.Key, dst interface{}) (err error) {
err = validateDatastoreEntity(dst)
if err != nil {
return err
}
c.lock.Lock()
defer c.lock.Unlock()
v := reflect.ValueOf(dst)
o, ok := c.objects[*key]
if !ok {
return datastore.ErrNoSuchEntity
}
v.Elem().Set(o)
return nil
}

func (c *testClient) Put(ctx context.Context, key *datastore.Key, src interface{}) (*datastore.Key, error) {
err := validateDatastoreEntity(src)
if err != nil {
return nil, err
}
c.lock.Lock()
defer c.lock.Unlock()
v := reflect.ValueOf(src)
c.objects[*key] = reflect.Indirect(v)
return key, nil
}
17 changes: 12 additions & 5 deletions tracker/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,19 @@ func InitTracker(ctx context.Context, client dsiface.Client, saveInterval time.D
t.dsKey.Namespace = "gardener"

if client != nil {
var jsonBytes []byte
err := client.Get(ctx, t.dsKey, &jsonBytes) // This should error?
jobStruct := struct {
SaveTime time.Time
JSON []byte `datastore:",noindex"`
}{time.Time{}, make([]byte, 0, 100000)}
err := client.Get(ctx, t.dsKey, &jobStruct) // This should error?
if err != nil {
if err != datastore.ErrNoSuchEntity {
return nil, err
}
log.Println(err)
} else {
log.Println("Unmarshalling", len(jsonBytes))
json.Unmarshal(jsonBytes, &t.jobs)
log.Println("Unmarshalling", len(jobStruct.JSON))
json.Unmarshal(jobStruct.JSON, &t.jobs)
}
}

Expand Down Expand Up @@ -161,9 +164,13 @@ func (tr *Tracker) Sync() error {
}

// Save the full state.
jsonStruct := struct {
SaveTime time.Time
JSON []byte `datastore:",noindex"`
}{time.Now(), bytes}
ctx, cf := context.WithTimeout(context.Background(), 10*time.Second)
defer cf()
_, err = tr.client.Put(ctx, tr.dsKey, &bytes)
_, err = tr.client.Put(ctx, tr.dsKey, &jsonStruct)

return err
}
Expand Down
2 changes: 1 addition & 1 deletion tracker/tracker_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestWithDatastore(t *testing.T) {
}

log.Println("Calling Sync")
must(t, tk.Sync())
must(t, tk.Sync()) // This causes invalid entity type
// Check that the sync (and InitTracker) work.
restore, err := tracker.InitTracker(context.Background(), client, 0)
must(t, err)
Expand Down
56 changes: 0 additions & 56 deletions tracker/tracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@ import (
"fmt"
"log"
"math/rand"
"reflect"
"sync"
"testing"
"time"

"cloud.google.com/go/datastore"
"github.com/GoogleCloudPlatform/google-cloud-go-testing/datastore/dsiface"
"github.com/m-lab/etl-gardener/tracker"
)

Expand All @@ -28,59 +25,6 @@ func must(t *testing.T, err error) {
}
}

type testClient struct {
dsiface.Client // For unimplemented methods
lock sync.Mutex
objects map[datastore.Key]reflect.Value
}

func newTestClient() *testClient {
return &testClient{objects: make(map[datastore.Key]reflect.Value, 10)}
}

func (c *testClient) Close() error { return nil }

func (c *testClient) Count(ctx context.Context, q *datastore.Query) (n int, err error) {
return 0, ErrNotImplemented
}

func (c *testClient) Delete(ctx context.Context, key *datastore.Key) error {
c.lock.Lock()
defer c.lock.Unlock()
_, ok := c.objects[*key]
if !ok {
return datastore.ErrNoSuchEntity
}
delete(c.objects, *key)
return nil
}

func (c *testClient) Get(ctx context.Context, key *datastore.Key, dst interface{}) (err error) {
c.lock.Lock()
defer c.lock.Unlock()
v := reflect.ValueOf(dst)
if v.Kind() != reflect.Ptr {
return datastore.ErrInvalidEntityType
}
o, ok := c.objects[*key]
if !ok {
return datastore.ErrNoSuchEntity
}
v.Elem().Set(o)
return nil
}

func (c *testClient) Put(ctx context.Context, key *datastore.Key, src interface{}) (*datastore.Key, error) {
c.lock.Lock()
defer c.lock.Unlock()
v := reflect.ValueOf(src)
if v.Kind() != reflect.Ptr {
return nil, datastore.ErrInvalidEntityType
}
c.objects[*key] = reflect.Indirect(v)
return key, nil
}

var startDate = time.Date(2011, 1, 1, 0, 0, 0, 0, time.UTC)

func createJobs(t *testing.T, tk *tracker.Tracker, exp string, n int) {
Expand Down

0 comments on commit 6f54c24

Please sign in to comment.