gae

package module
v0.0.0-...-f695821 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 12, 2018 License: Apache-2.0 Imports: 0 Imported by: 0

README

gae: A Google AppEngine SDK wrapper

designed for testing and extensibility

THIS PACKAGE HAS NO API COMPATIBILITY GUARANTEES. USE UNPINNED AT YOUR OWN PERIL.

(but generally it should be pretty stableish).

GoDoc Build Status Coverage Status

Installing

go get -u go.chromium.org/gae/...

Why/What/How

See the godocs.

Versioning

  • Branch master contains the latest code.

Contributing

  • Sign the Google CLA.
  • Make sure your user.email and user.name are configured in git config.
  • Install test-only packages: go get -u -t go.chromium.org/luci/client/...
  • Install the pcg git hook: go get -u github.com/maruel/pre-commit-go/cmd/... && pcg

Run the following to setup the code review tool and create your first review:

git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git $HOME/src/depot_tools
export PATH="$PATH:$HOME/src/depot_tools"
cd $GOROOT/go.chromium.org/luci
git checkout -b work origin/master

# hack hack
git commit -a -m "This is awesome"

# We use Gerrit for code review. Visit
# https://chromium-review.googlesource.com/new-password
# and follow instructions.

git cl upload -s --r-owners
# This will upload your change to Gerrit and pick a random owner (as defined
# in the OWNERS file) for review.
# Wait for approval and submit your code through Commit Queue.
# Commit queue will test your change on multiple platforms and land it
# automatically.

# Once you get a review with comments, you can do additional commits to your
# feature branch, and then upload again to update the review in gerrit.
$ git cl upload

Use git cl help and git cl help <cmd> for more details.

Documentation

Overview

Package gae provides a fakable wrapped interface for the appengine SDK's APIs. This means that it's possible to mock all of the supported appengine APIs for testing (or potentially implement a different backend for them).

Features

gae currently provides interfaces for:

  • Datastore
  • Memcache
  • TaskQueue
  • Info (e.g. Namespace, AppID, etc.)

Additional features include:

  • true service interfaces (not package-level functions)
  • methods don't need explicit context passed to them, increasing readability.
  • service filters allow for composition of functionality. For example:
  • transparent memcaching of datastore access
  • transparent transaction buffering
  • statistics-gathering shims
  • deterministic and probabalistic API failure simulation
  • transparent retries
  • truly parallel in-memory testing implementation. No more need for dev_appserver.py subprocesses :).
  • Separate service and user-facing interfaces
  • Allows easier filter and service implementation while retaining the benefits of a user-friendly interface.

Package Organization

The gae library is organized into several subpackages:

  • service/* supported service definitions
  • impl/* implementations of the services
  • filter/* extra filter functionality for the services, agnostic to the underlying implementation.

TLDR

In production, do:

import (
  "fmt"
  "net/http"

  "go.chromium.org/gae/impl/prod"
  "go.chromium.org/gae/service/datastore"
  "golang.org/x/net/context"
)

func handler(w http.ResponseWriter, r *http.Request) {
  c := prod.UseRequest(r)
  // add production filters, etc. here
  innerHandler(c, w)
}

type CoolStruct struct {
  ID `gae:"$id"`

  Value string
}

func innerHandler(c context.Context, w http.ResponseWriter) {
  obj := &CoolStruct{Value: "hello"}
  if err := rds.Put(c, obj); err != nil {
    http.Error(w, err.String(), http.StatusInternalServerError)
  }
  fmt.Fprintf(w, "I wrote: %s", ds.KeyForObj(obj))
}

And in your test do:

import (
  "testing"
  "fmt"
  "net/http"

  "go.chromium.org/gae/impl/memory"
  "go.chromium.org/gae/service/datastore"
  "golang.org/x/net/context"
)

func TestHandler(t *testing.T) {
  t.Parallel()
  c := memory.Use(context.Background())
  // use datastore here to monkey with the database, install testing
  // filters like featureBreaker to test error conditions in innerHandler,
  // etc.
  innerHandler(c, ...)
}

Service Definitions

A service defintion lives under the `service` subfolder, and defines the user-facing interface for a service. Each service has a few common types and functions. Common types are:

service.Interface    - the main user-friendly service interface.

service.RawInterface - the internal service interface used by service
                       and filter implementations. Note that some services
                       like Info don't distinguish between the service
                       interface and the user interface. This interface is
                       typically a bit lower level than Interface and
                       lacks convenience methods.

service.Testable     - any additional methods that a 'testing'
                       implementation should provide. This can be accessed
                       via the Testable method on RawInterface. If the
                       current implementation is not testable, it will
                       return nil. This is only meant to be accessed when
                       testing.

service.RawFactory   - a function returning a RawInterface

service.RawFilter    - a function returning a new RawInterface based on
                       the previous filtered interface. Filters chain
                       together to allow behavioral service features
                       without needing to agument the underlying service
                       implementations directly.

And common functions are:

service.GetRaw        - Retrieve the current, filtered RawInterface
                        implementation from the context. This is less
                        frequently used, but can be useful if you want to
                        avoid some of the overhead of the user-friendly
                        Interface, which can do sometimes-unnecessary amounts
                        of reflection or allocation. The RawInterface and
                        Interface for a service are fully interchangable and
                        usage of them can be freely mixed in an application.

service.AddRawFilters - adds one or more RawFilters to the context.

service.SetRawFactory - adds a RawFactory to the context

service.SetRaw        - adds an implementation of RawInterface to the context
                        (shorthand for SetRawFactory, useful for testing)

Implementations

The impl subdirectory contains a couple different service implementations, depending on your needs.

'prod' is the production (e.g. real appengine-backed) implementation. It calls through to the original appengine SDK.

'memory' is a truly parallel in-memory testing implementation. It should be functionally the same as the production appengine services, implementing many of the real-world quirks of the actual services. It also implements the services' Testable interface, for those services which define those interfaces.

'dummy' provides a bunch of implementations of the various RawInterfaces. These implementations just panic with an appropriate message, depending on which API method was called. They're useful to embed in filter or service implementations as stubs while you're implementing the filter.

Usage

You will typically access one of the service interfaces in your code like:

// This is the 'production' code
func HTTPHandler(r *http.Request) {
  c := prod.Use(appengine.NewContext(r))
  CoolFunc(c)
}

// This is the 'testing' code
func TestCoolFunc(t *testing.T) {
  c := memory.Use(context.Background())
  CoolFunc(c)
}

func CoolFunc(c context.Context, ...) {
  SomeOtherFunction(c, ...)

  // because you might need to:
  ds.RunInTransaction(c, func (c context.Context) error {
    SomeOtherFunction(c, ...)  // c contains transactional versions of everything
  }, nil)
}

Filters

Each service also supports "filters". Filters are proxy objects which have the same interface as the service they're filtering, and pass data through to the previous filter in the stack. Conceptually, a filtered version of, for example, the Datastore, could look like:

User code
<count filter (counts how many times each API is called by the user)>
<dscache filter (attempts to use memcache as a cache for datastore)>
<count filter (counts how many times each API is actually hit)>
memory datastore.RawInterface implementation

Filters may or may not have state, it's up to the filter itself. In the case of the count filter, it returns its state from the Filter<Service> method, and the state can be observed to see how many times each API was invoked. Since filters stack, we can compare counts from rawCount versus userCount to see how many calls to the actual real datastore went through, vs. how many went to memcache, for example.

Note that Filters apply only to the service.RawInterface. All implementations of service.Interface boil down to calls to service.RawInterface methods, but it's possible that bad calls to the service.Interface methods could return an error before ever reaching the filters or service implementation.

Index

Constants

This section is empty.

Variables

View Source
var Stop error = stopErr{}

Stop is understood by various services to stop iterative processes. Examples include datastore.Interface.Run's callback.

Functions

This section is empty.

Types

This section is empty.

Directories

Path Synopsis
docs
present/lightning
Package demo exists to make pre-commit-go happy.
Package demo exists to make pre-commit-go happy.
filter
count
Package count contains 'counter' filters for all the gae services.
Package count contains 'counter' filters for all the gae services.
dscache
Package dscache provides a transparent cache for RawDatastore which is backed by Memcache.
Package dscache provides a transparent cache for RawDatastore which is backed by Memcache.
featureBreaker
Package featureBreaker contains filters for dynamically disabling/breaking API features at test-time.
Package featureBreaker contains filters for dynamically disabling/breaking API features at test-time.
featureBreaker/flaky
Package flaky can be used to emulate flaky GAE errors that happen randomly with some probability.
Package flaky can be used to emulate flaky GAE errors that happen randomly with some probability.
readonly
Package readonly implements a filter that enforces read-only accesses to services.
Package readonly implements a filter that enforces read-only accesses to services.
txnBuf
Package txnBuf contains a transaction buffer filter for the datastore service.
Package txnBuf contains a transaction buffer filter for the datastore service.
impl
dummy
Package dummy provides panicking dummy implementations of all service Interfaces.
Package dummy provides panicking dummy implementations of all service Interfaces.
memory
Package memory provides an implementation of infra/gae/libs/wrapper which backs to local memory ONLY.
Package memory provides an implementation of infra/gae/libs/wrapper which backs to local memory ONLY.
prod
Package prod provides an implementation of infra/gae/libs/wrapper which backs to appengine.
Package prod provides an implementation of infra/gae/libs/wrapper which backs to appengine.
prod/constraints
Package constraints contains production datastore constraints.
Package constraints contains production datastore constraints.
service
blobstore
Package blobstore is a PLACEHOLDER for the blobstore implementation.
Package blobstore is a PLACEHOLDER for the blobstore implementation.
datastore/dumper
Package dumper implements a very VERY dumb datastore-dumping debugging aid.
Package dumper implements a very VERY dumb datastore-dumping debugging aid.
datastore/internal/protos/datastore
Package datastore is a generated protocol buffer package.
Package datastore is a generated protocol buffer package.
datastore/meta
Package meta contains some methods for interacting with GAE's metadata APIs.
Package meta contains some methods for interacting with GAE's metadata APIs.
datastore/serialize
Package serialize provides methods for reading and writing concatenable, bytewise-sortable forms of the datatypes defined in the datastore package.
Package serialize provides methods for reading and writing concatenable, bytewise-sortable forms of the datatypes defined in the datastore package.
info/support
Package support provides Info-related support functionality.
Package support provides Info-related support functionality.
logging
Package logging is simply documentation :) In order to use logging, please import and use the "go.chromium.org/luci/common/logging" package.
Package logging is simply documentation :) In order to use logging, please import and use the "go.chromium.org/luci/common/logging" package.
urlfetch
Package urlfetch provides a way for an application to get http.RoundTripper that can make outbound HTTP requests.
Package urlfetch provides a way for an application to get http.RoundTripper that can make outbound HTTP requests.
tools

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL