Package bttest contains test helpers for working with the bigtable package.
To use a Server, create it, and then connect to it with no security: (The project/instance values are ignored.)
srv, err := bttest.NewServer("localhost:0") ... conn, err := grpc.Dial( srv.Addr, grpc.WithTransportCredentials(insecure.NewCredentials())) ... client, err := bigtable.NewClient(ctx, proj, instance, option.WithGRPCConn(conn)) ...
To use a Server with an in-memory connection, provide a bufconn listener:
l := bufconn.Listen(1024 * 1024) srv, err := bttest.NewServerWithListener(l) ... conn, err := grpc.Dial( "bufnet", grpc.WithContextDialer(func(context.Context, string) (net.Conn, error) { return l.Dial() }), grpc.WithTransportCredentials(insecure.NewCredentials())) ...
Server
type Server struct {
Addr string
// contains filtered or unexported fields
}Server is an in-memory Cloud Bigtable fake. It is unauthenticated, and only a rough approximation.
func NewServer
func NewServer(laddr string, opt ...grpc.ServerOption) (*Server, error)NewServer creates a new Server. The Server will be listening for gRPC connections, without TLS, on the provided address. The resolved address is named by the Addr field.
Example
package main
import (
"context"
"fmt"
"log"
"cloud.google.com/go/bigtable"
"cloud.google.com/go/bigtable/bttest"
"google.golang.org/api/option"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
func main() {
srv, err := bttest.NewServer("localhost:0")
if err != nil {
log.Fatalln(err)
}
ctx := context.Background()
conn, err := grpc.Dial(srv.Addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
log.Fatalln(err)
}
proj, instance := "proj", "instance"
adminClient, err := bigtable.NewAdminClient(ctx, proj, instance, option.WithGRPCConn(conn))
if err != nil {
log.Fatalln(err)
}
if err = adminClient.CreateTable(ctx, "example"); err != nil {
log.Fatalln(err)
}
if err = adminClient.CreateColumnFamily(ctx, "example", "links"); err != nil {
log.Fatalln(err)
}
client, err := bigtable.NewClientWithConfig(ctx, proj, instance, bigtable.ClientConfig{MetricsProvider: bigtable.NoopMetricsProvider{}}, option.WithGRPCConn(conn))
if err != nil {
log.Fatalln(err)
}
tbl := client.Open("example")
mut := bigtable.NewMutation()
mut.Set("links", "golang.org", bigtable.Now(), []byte("Gophers!"))
if err = tbl.Apply(ctx, "com.google.cloud", mut); err != nil {
log.Fatalln(err)
}
if row, err := tbl.ReadRow(ctx, "com.google.cloud"); err != nil {
log.Fatalln(err)
} else {
for _, column := range row["links"] {
fmt.Println(column.Column)
fmt.Println(string(column.Value))
}
}
}
func NewServerWithListener
NewServerWithListener creates a new Server using the provided listener. The Addr field of the returned Server will be the listener's address.
The caller is responsible for closing the listener. The server's Close method will not close the provided listener, nor will it clean up any underlying resources like unix socket files.
func (*Server) Close
func (s *Server) Close()Close shuts down the server.