bump deps

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2020-11-06 11:41:23 +03:00
parent 139fea2779
commit 7f0c11006c
4 changed files with 759 additions and 9 deletions

257
api/router/router_test.go Normal file
View File

@ -0,0 +1,257 @@
// +build ignore
package router_test
import (
"context"
"fmt"
"io/ioutil"
"log"
"net/http"
"testing"
"time"
"github.com/unistack-org/micro/v3/api"
"github.com/unistack-org/micro/v3/api/handler"
"github.com/unistack-org/micro/v3/api/handler/rpc"
"github.com/unistack-org/micro/v3/api/router"
rregistry "github.com/unistack-org/micro/v3/api/router/registry"
rstatic "github.com/unistack-org/micro/v3/api/router/static"
"github.com/unistack-org/micro/v3/broker"
bmemory "github.com/unistack-org/micro/v3/broker/memory"
"github.com/unistack-org/micro/v3/client"
gcli "github.com/unistack-org/micro/v3/client/grpc"
rmemory "github.com/unistack-org/micro/v3/registry/memory"
rt "github.com/unistack-org/micro/v3/router"
regRouter "github.com/unistack-org/micro/v3/router/registry"
"github.com/unistack-org/micro/v3/server"
gsrv "github.com/unistack-org/micro/v3/server/grpc"
pb "github.com/unistack-org/micro/v3/server/grpc/proto"
)
// server is used to implement helloworld.GreeterServer.
type testServer struct {
}
// TestHello implements helloworld.GreeterServer
func (s *testServer) Call(ctx context.Context, req *pb.Request, rsp *pb.Response) error {
rsp.Msg = "Hello " + req.Uuid
return nil
}
// TestHello implements helloworld.GreeterServer
func (s *testServer) CallPcre(ctx context.Context, req *pb.Request, rsp *pb.Response) error {
rsp.Msg = "Hello " + req.Uuid
return nil
}
// TestHello implements helloworld.GreeterServer
func (s *testServer) CallPcreInvalid(ctx context.Context, req *pb.Request, rsp *pb.Response) error {
rsp.Msg = "Hello " + req.Uuid
return nil
}
func initial(t *testing.T) (server.Server, client.Client) {
r := rmemory.NewRegistry()
b := bmemory.NewBroker(broker.Registry(r))
// create a new client
s := gsrv.NewServer(
server.Name("foo"),
server.Broker(b),
server.Registry(r),
)
rtr := regRouter.NewRouter(
rt.Registry(r),
)
// create a new server
c := gcli.NewClient(
client.Router(rtr),
client.Broker(b),
)
h := &testServer{}
pb.RegisterTestHandler(s, h)
if err := s.Start(); err != nil {
t.Fatalf("failed to start: %v", err)
}
return s, c
}
func check(t *testing.T, addr string, path string, expected string) {
req, err := http.NewRequest("POST", fmt.Sprintf(path, addr), nil)
if err != nil {
t.Fatalf("Failed to created http.Request: %v", err)
}
req.Header.Set("Content-Type", "application/json")
rsp, err := (&http.Client{}).Do(req)
if err != nil {
t.Fatalf("Failed to created http.Request: %v", err)
}
defer rsp.Body.Close()
buf, err := ioutil.ReadAll(rsp.Body)
if err != nil {
t.Fatal(err)
}
jsonMsg := expected
if string(buf) != jsonMsg {
t.Fatalf("invalid message received, parsing error %s != %s", buf, jsonMsg)
}
}
func TestRouterRegistryPcre(t *testing.T) {
s, c := initial(t)
defer s.Stop()
router := rregistry.NewRouter(
router.WithHandler(rpc.Handler),
router.WithRegistry(s.Options().Registry),
)
hrpc := rpc.NewHandler(
handler.WithClient(c),
handler.WithRouter(router),
)
hsrv := &http.Server{
Handler: hrpc,
Addr: "127.0.0.1:6543",
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
IdleTimeout: 20 * time.Second,
MaxHeaderBytes: 1024 * 1024 * 1, // 1Mb
}
go func() {
log.Println(hsrv.ListenAndServe())
}()
defer hsrv.Close()
time.Sleep(1 * time.Second)
check(t, hsrv.Addr, "http://%s/api/v0/test/call/TEST", `{"msg":"Hello TEST"}`)
}
func TestRouterStaticPcre(t *testing.T) {
s, c := initial(t)
defer s.Stop()
router := rstatic.NewRouter(
router.WithHandler(rpc.Handler),
router.WithRegistry(s.Options().Registry),
)
err := router.Register(&api.Endpoint{
Name: "foo.Test.Call",
Method: []string{"POST"},
Path: []string{"^/api/v0/test/call/?$"},
Handler: "rpc",
})
if err != nil {
t.Fatal(err)
}
hrpc := rpc.NewHandler(
handler.WithClient(c),
handler.WithRouter(router),
)
hsrv := &http.Server{
Handler: hrpc,
Addr: "127.0.0.1:6543",
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
IdleTimeout: 20 * time.Second,
MaxHeaderBytes: 1024 * 1024 * 1, // 1Mb
}
go func() {
log.Println(hsrv.ListenAndServe())
}()
defer hsrv.Close()
time.Sleep(1 * time.Second)
check(t, hsrv.Addr, "http://%s/api/v0/test/call", `{"msg":"Hello "}`)
}
func TestRouterStaticGpath(t *testing.T) {
s, c := initial(t)
defer s.Stop()
router := rstatic.NewRouter(
router.WithHandler(rpc.Handler),
router.WithRegistry(s.Options().Registry),
)
err := router.Register(&api.Endpoint{
Name: "foo.Test.Call",
Method: []string{"POST"},
Path: []string{"/api/v0/test/call/{uuid}"},
Handler: "rpc",
})
if err != nil {
t.Fatal(err)
}
hrpc := rpc.NewHandler(
handler.WithClient(c),
handler.WithRouter(router),
)
hsrv := &http.Server{
Handler: hrpc,
Addr: "127.0.0.1:6543",
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
IdleTimeout: 20 * time.Second,
MaxHeaderBytes: 1024 * 1024 * 1, // 1Mb
}
go func() {
log.Println(hsrv.ListenAndServe())
}()
defer hsrv.Close()
time.Sleep(1 * time.Second)
check(t, hsrv.Addr, "http://%s/api/v0/test/call/TEST", `{"msg":"Hello TEST"}`)
}
func TestRouterStaticPcreInvalid(t *testing.T) {
var ep *api.Endpoint
var err error
s, c := initial(t)
defer s.Stop()
router := rstatic.NewRouter(
router.WithHandler(rpc.Handler),
router.WithRegistry(s.Options().Registry),
)
ep = &api.Endpoint{
Name: "foo.Test.Call",
Method: []string{"POST"},
Path: []string{"^/api/v0/test/call/?"},
Handler: "rpc",
}
err = router.Register(ep)
if err == nil {
t.Fatalf("invalid endpoint %v", ep)
}
ep = &api.Endpoint{
Name: "foo.Test.Call",
Method: []string{"POST"},
Path: []string{"/api/v0/test/call/?$"},
Handler: "rpc",
}
err = router.Register(ep)
if err == nil {
t.Fatalf("invalid endpoint %v", ep)
}
_ = c
}

6
go.mod
View File

@ -7,16 +7,16 @@ require (
github.com/google/uuid v1.1.2 github.com/google/uuid v1.1.2
github.com/opentracing/opentracing-go v1.2.0 github.com/opentracing/opentracing-go v1.2.0
github.com/stretchr/testify v1.5.1 github.com/stretchr/testify v1.5.1
github.com/unistack-org/micro-broker-http v0.0.0-20201102230515-e1a6d448f88f github.com/unistack-org/micro-broker-http v0.0.0-20201106084013-bff50fb8c334
github.com/unistack-org/micro-broker-memory v0.0.2-0.20201105185131-5ff932308afd github.com/unistack-org/micro-broker-memory v0.0.2-0.20201105185131-5ff932308afd
github.com/unistack-org/micro-client-grpc v0.0.2-0.20201028070730-15a5d7d2cde8 github.com/unistack-org/micro-client-grpc v0.0.2-0.20201028070730-15a5d7d2cde8
github.com/unistack-org/micro-registry-memory v0.0.2-0.20201105195351-bd57ee0e4bd6 github.com/unistack-org/micro-registry-memory v0.0.2-0.20201105195351-bd57ee0e4bd6
github.com/unistack-org/micro-router-registry v0.0.2-0.20201105175056-773128885d9e github.com/unistack-org/micro-router-registry v0.0.2-0.20201105175056-773128885d9e
github.com/unistack-org/micro-server-grpc v0.0.2-0.20201104230137-31c35661ae73 github.com/unistack-org/micro-server-grpc v0.0.2-0.20201105204550-241e452ecf38
github.com/unistack-org/micro-server-http v0.0.2-0.20201104225538-7d3dc63ae435 github.com/unistack-org/micro-server-http v0.0.2-0.20201104225538-7d3dc63ae435
github.com/unistack-org/micro-server-tcp v0.0.2-0.20201104231236-b12d45f45cbc github.com/unistack-org/micro-server-tcp v0.0.2-0.20201104231236-b12d45f45cbc
github.com/unistack-org/micro-wrapper-opentracing v0.0.1 github.com/unistack-org/micro-wrapper-opentracing v0.0.1
github.com/unistack-org/micro/v3 v3.0.0-gamma.0.20201105193505-8fa8afdfa4b4 github.com/unistack-org/micro/v3 v3.0.0-gamma.0.20201106081812-be8d09c66352
google.golang.org/grpc v1.31.1 google.golang.org/grpc v1.31.1
google.golang.org/protobuf v1.25.0 google.golang.org/protobuf v1.25.0
) )

12
go.sum
View File

@ -264,8 +264,8 @@ github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5
github.com/timewasted/linode v0.0.0-20160829202747-37e84520dcf7/go.mod h1:imsgLplxEC/etjIhdr3dNzV3JeT27LbVu5pYWm0JCBY= github.com/timewasted/linode v0.0.0-20160829202747-37e84520dcf7/go.mod h1:imsgLplxEC/etjIhdr3dNzV3JeT27LbVu5pYWm0JCBY=
github.com/transip/gotransip v0.0.0-20190812104329-6d8d9179b66f/go.mod h1:i0f4R4o2HM0m3DZYQWsj6/MEowD57VzoH0v3d7igeFY= github.com/transip/gotransip v0.0.0-20190812104329-6d8d9179b66f/go.mod h1:i0f4R4o2HM0m3DZYQWsj6/MEowD57VzoH0v3d7igeFY=
github.com/uber-go/atomic v1.3.2/go.mod h1:/Ct5t2lcmbJ4OSe/waGBoaVvVqtO0bmtfVNex1PFV8g= github.com/uber-go/atomic v1.3.2/go.mod h1:/Ct5t2lcmbJ4OSe/waGBoaVvVqtO0bmtfVNex1PFV8g=
github.com/unistack-org/micro-broker-http v0.0.0-20201102230515-e1a6d448f88f h1:7VEY57ZERb+v5rVTOCwS/sO3NlOamTw2WEv0f1r6Zl8= github.com/unistack-org/micro-broker-http v0.0.0-20201106084013-bff50fb8c334 h1:h90jnIAwqvFlWvDa0k/fLX6fCh94BBJcrjT9vskDMyM=
github.com/unistack-org/micro-broker-http v0.0.0-20201102230515-e1a6d448f88f/go.mod h1:d2NYj/cl/7WBPk5K6Ef3By8Pj2ZN41mFn3LfZFLHYfA= github.com/unistack-org/micro-broker-http v0.0.0-20201106084013-bff50fb8c334/go.mod h1:8/xRK44NNmqBB2e0FEhefIjQPCtyDinQpuMrSocEq9k=
github.com/unistack-org/micro-broker-memory v0.0.2-0.20201105185131-5ff932308afd h1:LNMB1G3yZEI1zpX4SGodLqUfOw4Zan30rcpcvKTDMCI= github.com/unistack-org/micro-broker-memory v0.0.2-0.20201105185131-5ff932308afd h1:LNMB1G3yZEI1zpX4SGodLqUfOw4Zan30rcpcvKTDMCI=
github.com/unistack-org/micro-broker-memory v0.0.2-0.20201105185131-5ff932308afd/go.mod h1:BlX+zLHGZxNngsiGiAaIqt0XjwIk3iurw4cqRFYRaVk= github.com/unistack-org/micro-broker-memory v0.0.2-0.20201105185131-5ff932308afd/go.mod h1:BlX+zLHGZxNngsiGiAaIqt0XjwIk3iurw4cqRFYRaVk=
github.com/unistack-org/micro-client-grpc v0.0.2-0.20201028070730-15a5d7d2cde8 h1:ozSg+KCknYKVYrRAH4j1kssc2TBJhuvrxl6z3xXmXsQ= github.com/unistack-org/micro-client-grpc v0.0.2-0.20201028070730-15a5d7d2cde8 h1:ozSg+KCknYKVYrRAH4j1kssc2TBJhuvrxl6z3xXmXsQ=
@ -291,8 +291,8 @@ github.com/unistack-org/micro-registry-memory v0.0.2-0.20201105195351-bd57ee0e4b
github.com/unistack-org/micro-registry-memory v0.0.2-0.20201105195351-bd57ee0e4bd6/go.mod h1:0f9qV/bM07qO2sNuNnr3qOEozsYpmkTxuEgauWLWDec= github.com/unistack-org/micro-registry-memory v0.0.2-0.20201105195351-bd57ee0e4bd6/go.mod h1:0f9qV/bM07qO2sNuNnr3qOEozsYpmkTxuEgauWLWDec=
github.com/unistack-org/micro-router-registry v0.0.2-0.20201105175056-773128885d9e h1:spzPFROFgxXCoggEv0dapiH3Hfp0x/HqQy4rimQ1rbU= github.com/unistack-org/micro-router-registry v0.0.2-0.20201105175056-773128885d9e h1:spzPFROFgxXCoggEv0dapiH3Hfp0x/HqQy4rimQ1rbU=
github.com/unistack-org/micro-router-registry v0.0.2-0.20201105175056-773128885d9e/go.mod h1:nvJqRLixa2UqbctfnMx1WJ6IJdPJQ9FheJnh+03QsXA= github.com/unistack-org/micro-router-registry v0.0.2-0.20201105175056-773128885d9e/go.mod h1:nvJqRLixa2UqbctfnMx1WJ6IJdPJQ9FheJnh+03QsXA=
github.com/unistack-org/micro-server-grpc v0.0.2-0.20201104230137-31c35661ae73 h1:12YaMyj2b+P9MO9QJ2zu2fjtcASFK7pD3O/dyp55iUQ= github.com/unistack-org/micro-server-grpc v0.0.2-0.20201105204550-241e452ecf38 h1:rsrE+Va3tKhZL6JA/NHzIqFQHkax7Kb6Wu4wvStOU5I=
github.com/unistack-org/micro-server-grpc v0.0.2-0.20201104230137-31c35661ae73/go.mod h1:EzFbU2AkFyDTlCIn4biNtOgLF/rMgxDp72ezWvMl8Is= github.com/unistack-org/micro-server-grpc v0.0.2-0.20201105204550-241e452ecf38/go.mod h1:EzFbU2AkFyDTlCIn4biNtOgLF/rMgxDp72ezWvMl8Is=
github.com/unistack-org/micro-server-http v0.0.2-0.20201104225538-7d3dc63ae435 h1:a6c4WEBNqxTaTOX0+cPtJsIZbb5iAu8BphwoGcGhMaA= github.com/unistack-org/micro-server-http v0.0.2-0.20201104225538-7d3dc63ae435 h1:a6c4WEBNqxTaTOX0+cPtJsIZbb5iAu8BphwoGcGhMaA=
github.com/unistack-org/micro-server-http v0.0.2-0.20201104225538-7d3dc63ae435/go.mod h1:hlRr6JKzG6dswJYiEWT71nCwxqfpR+dK7FVJCZ+RjNE= github.com/unistack-org/micro-server-http v0.0.2-0.20201104225538-7d3dc63ae435/go.mod h1:hlRr6JKzG6dswJYiEWT71nCwxqfpR+dK7FVJCZ+RjNE=
github.com/unistack-org/micro-server-tcp v0.0.2-0.20201104231236-b12d45f45cbc h1:RYSaZEEEbtvE55wqLWPjkZ7MoGS99t8QfQNcBXt8uiM= github.com/unistack-org/micro-server-tcp v0.0.2-0.20201104231236-b12d45f45cbc h1:RYSaZEEEbtvE55wqLWPjkZ7MoGS99t8QfQNcBXt8uiM=
@ -306,12 +306,12 @@ github.com/unistack-org/micro/v3 v3.0.0-gamma.0.20200920135754-1cbd1d2bad83/go.m
github.com/unistack-org/micro/v3 v3.0.0-gamma.0.20200922103357-4c4fa00a5d94/go.mod h1:aL+8VhSXpx0SuEeXPOWUo5BgS7kyvWYobeXFay90UUM= github.com/unistack-org/micro/v3 v3.0.0-gamma.0.20200922103357-4c4fa00a5d94/go.mod h1:aL+8VhSXpx0SuEeXPOWUo5BgS7kyvWYobeXFay90UUM=
github.com/unistack-org/micro/v3 v3.0.0-gamma.0.20200928100853-efd9075d9b4a/go.mod h1:aL+8VhSXpx0SuEeXPOWUo5BgS7kyvWYobeXFay90UUM= github.com/unistack-org/micro/v3 v3.0.0-gamma.0.20200928100853-efd9075d9b4a/go.mod h1:aL+8VhSXpx0SuEeXPOWUo5BgS7kyvWYobeXFay90UUM=
github.com/unistack-org/micro/v3 v3.0.0-gamma.0.20201016063857-14c97d59c15f/go.mod h1:aL+8VhSXpx0SuEeXPOWUo5BgS7kyvWYobeXFay90UUM= github.com/unistack-org/micro/v3 v3.0.0-gamma.0.20201016063857-14c97d59c15f/go.mod h1:aL+8VhSXpx0SuEeXPOWUo5BgS7kyvWYobeXFay90UUM=
github.com/unistack-org/micro/v3 v3.0.0-gamma.0.20201102230232-8a2b12201568 h1:2h+k414Q3ABTRHByIvPJYZbi5s8qlCi9yG7x3wqaFDs=
github.com/unistack-org/micro/v3 v3.0.0-gamma.0.20201102230232-8a2b12201568/go.mod h1:LFvCXGOgcLIj2k/8eL71TpIpcJBN2SXXAUx8U6dz9Rw=
github.com/unistack-org/micro/v3 v3.0.0-gamma.0.20201104214903-1fbf8b2e209e/go.mod h1:LFvCXGOgcLIj2k/8eL71TpIpcJBN2SXXAUx8U6dz9Rw= github.com/unistack-org/micro/v3 v3.0.0-gamma.0.20201104214903-1fbf8b2e209e/go.mod h1:LFvCXGOgcLIj2k/8eL71TpIpcJBN2SXXAUx8U6dz9Rw=
github.com/unistack-org/micro/v3 v3.0.0-gamma.0.20201105181805-e12754779912/go.mod h1:LFvCXGOgcLIj2k/8eL71TpIpcJBN2SXXAUx8U6dz9Rw= github.com/unistack-org/micro/v3 v3.0.0-gamma.0.20201105181805-e12754779912/go.mod h1:LFvCXGOgcLIj2k/8eL71TpIpcJBN2SXXAUx8U6dz9Rw=
github.com/unistack-org/micro/v3 v3.0.0-gamma.0.20201105193505-8fa8afdfa4b4 h1:R4tYb53Y2/mOgCdmdc/o1iFRcLprUj7tWqx8yJv+hVU= github.com/unistack-org/micro/v3 v3.0.0-gamma.0.20201105193505-8fa8afdfa4b4 h1:R4tYb53Y2/mOgCdmdc/o1iFRcLprUj7tWqx8yJv+hVU=
github.com/unistack-org/micro/v3 v3.0.0-gamma.0.20201105193505-8fa8afdfa4b4/go.mod h1:LFvCXGOgcLIj2k/8eL71TpIpcJBN2SXXAUx8U6dz9Rw= github.com/unistack-org/micro/v3 v3.0.0-gamma.0.20201105193505-8fa8afdfa4b4/go.mod h1:LFvCXGOgcLIj2k/8eL71TpIpcJBN2SXXAUx8U6dz9Rw=
github.com/unistack-org/micro/v3 v3.0.0-gamma.0.20201106081812-be8d09c66352 h1:rtZrrpdVoF7zz93N8vsx5evDC4g5AU46tkdPeTGOQMk=
github.com/unistack-org/micro/v3 v3.0.0-gamma.0.20201106081812-be8d09c66352/go.mod h1:LFvCXGOgcLIj2k/8eL71TpIpcJBN2SXXAUx8U6dz9Rw=
github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/vultr/govultr v0.1.4/go.mod h1:9H008Uxr/C4vFNGLqKx232C206GL0PBHzOP0809bGNA= github.com/vultr/govultr v0.1.4/go.mod h1:9H008Uxr/C4vFNGLqKx232C206GL0PBHzOP0809bGNA=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU= github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=

493
store/store_test.go Normal file
View File

@ -0,0 +1,493 @@
// +build ignore
// Package test provides a way to run tests against all the various implementations of the Store interface.
// It can't live in the store package itself because of circular import issues
package test
import (
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/unistack-org/micro/v3/store/cache"
"github.com/unistack-org/micro/v3/store/memory"
"github.com/kr/pretty"
"github.com/unistack-org/micro/v3/store/cockroach"
"github.com/unistack-org/micro/v3/store"
"github.com/unistack-org/micro/v3/store/file"
"github.com/davecgh/go-spew/spew"
)
func fileStoreCleanup(db string, s store.Store) {
s.Close()
dir := filepath.Join(file.DefaultDir, db+"/")
os.RemoveAll(dir)
}
func cockroachStoreCleanup(db string, s store.Store) {
keys, _ := s.List()
for _, k := range keys {
s.Delete(k)
}
s.Close()
}
func memoryCleanup(db string, s store.Store) {
s.Close()
}
func cacheCleanup(db string, s store.Store) {
s.Close()
}
func TestStoreReInit(t *testing.T) {
if len(os.Getenv("INTEGRATION_TESTS")) > 0 {
t.Skip()
}
tcs := []struct {
name string
s store.Store
cleanup func(db string, s store.Store)
}{
{name: "file", s: file.NewStore(store.Table("aaa")), cleanup: fileStoreCleanup},
{name: "cockroach", s: cockroach.NewStore(store.Table("aaa")), cleanup: cockroachStoreCleanup},
{name: "memory", s: memory.NewStore(store.Table("aaa")), cleanup: memoryCleanup},
{name: "cache", s: cache.NewStore(memory.NewStore(store.Table("aaa"))), cleanup: cacheCleanup},
}
for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
defer tc.cleanup(file.DefaultDatabase, tc.s)
tc.s.Init(store.Table("bbb"))
if tc.s.Options().Table != "bbb" {
t.Error("Init didn't reinitialise the store")
}
})
}
}
func TestStoreBasic(t *testing.T) {
if len(os.Getenv("INTEGRATION_TESTS")) > 0 {
t.Skip()
}
tcs := []struct {
name string
s store.Store
cleanup func(db string, s store.Store)
}{
{name: "file", s: file.NewStore(), cleanup: fileStoreCleanup},
{name: "cockroach", s: cockroach.NewStore(), cleanup: cockroachStoreCleanup},
{name: "memory", s: memory.NewStore(), cleanup: memoryCleanup},
{name: "cache", s: cache.NewStore(memory.NewStore()), cleanup: cacheCleanup},
}
for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
defer tc.cleanup(file.DefaultDatabase, tc.s)
runStoreTest(tc.s, t)
})
}
}
func TestStoreTable(t *testing.T) {
if len(os.Getenv("INTEGRATION_TESTS")) > 0 {
t.Skip()
}
tcs := []struct {
name string
s store.Store
cleanup func(db string, s store.Store)
}{
{name: "file", s: file.NewStore(store.Table("testTable")), cleanup: fileStoreCleanup},
{name: "cockroach", s: cockroach.NewStore(store.Table("testTable")), cleanup: cockroachStoreCleanup},
{name: "memory", s: memory.NewStore(store.Table("testTable")), cleanup: memoryCleanup},
{name: "cache", s: cache.NewStore(memory.NewStore(store.Table("testTable"))), cleanup: cacheCleanup},
}
for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
defer tc.cleanup(file.DefaultDatabase, tc.s)
runStoreTest(tc.s, t)
})
}
}
func TestStoreDatabase(t *testing.T) {
if len(os.Getenv("INTEGRATION_TESTS")) > 0 {
t.Skip()
}
tcs := []struct {
name string
s store.Store
cleanup func(db string, s store.Store)
}{
{name: "file", s: file.NewStore(store.Database("testdb")), cleanup: fileStoreCleanup},
{name: "cockroach", s: cockroach.NewStore(store.Database("testdb")), cleanup: cockroachStoreCleanup},
{name: "memory", s: memory.NewStore(store.Database("testdb")), cleanup: memoryCleanup},
{name: "cache", s: cache.NewStore(memory.NewStore(store.Database("testdb"))), cleanup: cacheCleanup},
}
for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
defer tc.cleanup("testdb", tc.s)
runStoreTest(tc.s, t)
})
}
}
func TestStoreDatabaseTable(t *testing.T) {
if len(os.Getenv("INTEGRATION_TESTS")) > 0 {
t.Skip()
}
tcs := []struct {
name string
s store.Store
cleanup func(db string, s store.Store)
}{
{name: "file", s: file.NewStore(store.Database("testdb"), store.Table("testTable")), cleanup: fileStoreCleanup},
{name: "cockroach", s: cockroach.NewStore(store.Database("testdb"), store.Table("testTable")), cleanup: cockroachStoreCleanup},
{name: "memory", s: memory.NewStore(store.Database("testdb"), store.Table("testTable")), cleanup: memoryCleanup},
{name: "cache", s: cache.NewStore(memory.NewStore(store.Database("testdb"), store.Table("testTable"))), cleanup: cacheCleanup},
}
for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
defer tc.cleanup("testdb", tc.s)
runStoreTest(tc.s, t)
})
}
}
func runStoreTest(s store.Store, t *testing.T) {
if len(os.Getenv("INTEGRATION_TESTS")) == 0 {
t.Logf("Options %s %v\n", s.String(), s.Options())
}
expiryTests(s, t)
suffixPrefixExpiryTests(s, t)
readTests(s, t)
listTests(s, t)
}
func readTests(s store.Store, t *testing.T) {
// Test Table, Suffix and WriteOptions
if err := s.Write(&store.Record{
Key: "foofoobarbar",
Value: []byte("something"),
}, store.WriteTTL(time.Millisecond*100)); err != nil {
t.Error(err)
}
if err := s.Write(&store.Record{
Key: "foofoo",
Value: []byte("something"),
}, store.WriteExpiry(time.Now().Add(time.Millisecond*100))); err != nil {
t.Error(err)
}
if err := s.Write(&store.Record{
Key: "barbar",
Value: []byte("something"),
// TTL has higher precedence than expiry
}, store.WriteExpiry(time.Now().Add(time.Hour)), store.WriteTTL(time.Millisecond*100)); err != nil {
t.Error(err)
}
if results, err := s.Read("foo", store.ReadPrefix(), store.ReadSuffix()); err != nil {
t.Error(err)
} else {
if len(results) != 1 {
t.Errorf("Expected 1 results, got %d: %# v", len(results), spew.Sdump(results))
}
}
time.Sleep(time.Millisecond * 100)
if results, err := s.List(); err != nil {
t.Fatalf("List failed: %s", err)
} else {
if len(results) != 0 {
t.Fatalf("Expiry options were not effective, results :%v", spew.Sdump(results))
}
}
// write the following records
for i := 0; i < 10; i++ {
s.Write(&store.Record{
Key: fmt.Sprintf("a%d", i),
Value: []byte{},
})
}
// read back a few records
if results, err := s.Read("a", store.ReadLimit(5), store.ReadPrefix()); err != nil {
t.Error(err)
} else {
if len(results) != 5 {
t.Fatal("Expected 5 results, got ", len(results))
}
if !strings.HasPrefix(results[0].Key, "a") {
t.Fatalf("Expected a prefix, got %s", results[0].Key)
}
}
// read the rest back
if results, err := s.Read("a", store.ReadLimit(30), store.ReadOffset(5), store.ReadPrefix()); err != nil {
t.Fatal(err)
} else {
if len(results) != 5 {
t.Fatal("Expected 5 results, got ", len(results))
}
}
}
func listTests(s store.Store, t *testing.T) {
for i := 0; i < 10; i++ {
s.Write(&store.Record{Key: fmt.Sprintf("List%d", i), Value: []byte("bar")})
}
recs, err := s.List(store.ListPrefix("List"))
if err != nil {
t.Fatalf("Error listing records %s", err)
}
if len(recs) != 10 {
t.Fatalf("Expected 10 records, received %d", len(recs))
}
recs, err = s.List(store.ListPrefix("List"), store.ListLimit(5))
if err != nil {
t.Fatalf("Error listing records %s", err)
}
if len(recs) != 5 {
t.Fatalf("Expected 5 records, received %d", len(recs))
}
recs, err = s.List(store.ListPrefix("List"), store.ListOffset(6))
if err != nil {
t.Fatalf("Error listing records %s", err)
}
if len(recs) != 4 {
t.Fatalf("Expected 4 records, received %d %+v", len(recs), recs)
}
recs, err = s.List(store.ListPrefix("List"), store.ListOffset(6), store.ListLimit(2))
if err != nil {
t.Fatalf("Error listing records %s", err)
}
if len(recs) != 2 {
t.Fatalf("Expected 2 records, received %d %+v", len(recs), recs)
}
}
func expiryTests(s store.Store, t *testing.T) {
// Read and Write an expiring Record
if err := s.Write(&store.Record{
Key: "Hello",
Value: []byte("World"),
Expiry: time.Millisecond * 150,
}); err != nil {
t.Error(err)
}
if r, err := s.Read("Hello"); err != nil {
t.Fatal(err)
} else {
if len(r) != 1 {
t.Error("Read returned multiple records")
}
if r[0].Key != "Hello" {
t.Errorf("Expected %s, got %s", "Hello", r[0].Key)
}
if string(r[0].Value) != "World" {
t.Errorf("Expected %s, got %s", "World", r[0].Value)
}
}
// wait for expiry
time.Sleep(time.Millisecond * 200)
if _, err := s.Read("Hello"); err != store.ErrNotFound {
t.Errorf("Expected %# v, got %# v", store.ErrNotFound, err)
}
// exercise the different ways to write an expiry, record.expiry
s.Write(&store.Record{Key: "aaa", Value: []byte("bbb"), Expiry: 1 * time.Second})
s.Write(&store.Record{Key: "aaaa", Value: []byte("bbb"), Expiry: 1 * time.Second})
s.Write(&store.Record{Key: "aaaaa", Value: []byte("bbb"), Expiry: 1 * time.Second})
results, err := s.Read("a", store.ReadPrefix())
if err != nil {
t.Error(err)
}
if len(results) != 3 {
t.Fatal("Results should have returned 3 records")
}
time.Sleep(1 * time.Second)
results, err = s.Read("a", store.ReadPrefix())
if err != nil {
t.Error(err)
}
if len(results) != 0 {
t.Fatal("Results should have returned 0 records")
}
// exercise the different ways to write an expiry, WriteExpiry
s.Write(&store.Record{Key: "bbb", Value: []byte("bbb")}, store.WriteExpiry(time.Now().Add(1*time.Second)))
s.Write(&store.Record{Key: "bbbb", Value: []byte("bbb")}, store.WriteExpiry(time.Now().Add(1*time.Second)))
s.Write(&store.Record{Key: "bbbbb", Value: []byte("bbb")}, store.WriteExpiry(time.Now().Add(1*time.Second)))
results, err = s.Read("b", store.ReadPrefix())
if err != nil {
t.Error(err)
}
if len(results) != 3 {
t.Fatalf("Results should have returned 3 records. Received %d", len(results))
}
time.Sleep(1 * time.Second)
results, err = s.Read("b", store.ReadPrefix())
if err != nil {
t.Error(err)
}
if len(results) != 0 {
t.Fatalf("Results should have returned 0 records. Received %d", len(results))
}
// exercise the different ways to write an expiry, WriteTTL
s.Write(&store.Record{Key: "ccc", Value: []byte("bbb")}, store.WriteTTL(1*time.Second))
s.Write(&store.Record{Key: "cccc", Value: []byte("bbb")}, store.WriteTTL(1*time.Second))
s.Write(&store.Record{Key: "ccccc", Value: []byte("bbb")}, store.WriteTTL(1*time.Second))
results, err = s.Read("c", store.ReadPrefix())
if err != nil {
t.Error(err)
}
if len(results) != 3 {
t.Fatalf("Results should have returned 3 records. Received %d", len(results))
}
time.Sleep(1 * time.Second)
results, err = s.Read("c", store.ReadPrefix())
if err != nil {
t.Error(err)
}
if len(results) != 0 {
t.Fatalf("Results should have returned 0 records. Received %d", len(results))
}
}
func suffixPrefixExpiryTests(s store.Store, t *testing.T) {
// Write 3 records with various expiry and get with Prefix
records := []*store.Record{
{
Key: "foo",
Value: []byte("foofoo"),
},
{
Key: "foobar",
Value: []byte("foobarfoobar"),
Expiry: time.Millisecond * 100,
},
}
for _, r := range records {
if err := s.Write(r); err != nil {
t.Errorf("Couldn't write k: %s, v: %# v (%s)", r.Key, pretty.Formatter(r.Value), err)
}
}
if results, err := s.Read("foo", store.ReadPrefix()); err != nil {
t.Errorf("Couldn't read all \"foo\" keys, got %#v (%s)", spew.Sdump(results), err)
} else {
if len(results) != 2 {
t.Errorf("Expected 2 items, got %d", len(results))
}
}
// wait for the expiry
time.Sleep(time.Millisecond * 200)
if results, err := s.Read("foo", store.ReadPrefix()); err != nil {
t.Errorf("Couldn't read all \"foo\" keys, got %# v (%s)", spew.Sdump(results), err)
} else if len(results) != 1 {
t.Errorf("Expected 1 item, got %d", len(results))
}
if err := s.Delete("foo"); err != nil {
t.Errorf("Delete failed (%v)", err)
}
if results, err := s.Read("foo"); err != store.ErrNotFound {
t.Errorf("Expected read failure read all \"foo\" keys, got %# v (%s)", spew.Sdump(results), err)
} else {
if len(results) != 0 {
t.Errorf("Expected 0 items, got %d (%# v)", len(results), spew.Sdump(results))
}
}
// Write 3 records with various expiry and get with Suffix
records = []*store.Record{
{
Key: "foo",
Value: []byte("foofoo"),
},
{
Key: "barfoo",
Value: []byte("barfoobarfoo"),
Expiry: time.Millisecond * 100,
},
{
Key: "bazbarfoo",
Value: []byte("bazbarfoobazbarfoo"),
Expiry: 2 * time.Millisecond * 100,
},
}
for _, r := range records {
if err := s.Write(r); err != nil {
t.Errorf("Couldn't write k: %s, v: %# v (%s)", r.Key, pretty.Formatter(r.Value), err)
}
}
if results, err := s.Read("foo", store.ReadSuffix()); err != nil {
t.Errorf("Couldn't read all \"foo\" keys, got %# v (%s)", spew.Sdump(results), err)
} else {
if len(results) != 3 {
t.Errorf("Expected 3 items, got %d", len(results))
//t.Logf("Table test: %v\n", spew.Sdump(results))
}
}
time.Sleep(time.Millisecond * 100)
if results, err := s.Read("foo", store.ReadSuffix()); err != nil {
t.Errorf("Couldn't read all \"foo\" keys, got %# v (%s)", spew.Sdump(results), err)
} else {
if len(results) != 2 {
t.Errorf("Expected 2 items, got %d", len(results))
//t.Logf("Table test: %v\n", spew.Sdump(results))
}
}
time.Sleep(time.Millisecond * 100)
if results, err := s.Read("foo", store.ReadSuffix()); err != nil {
t.Errorf("Couldn't read all \"foo\" keys, got %# v (%s)", spew.Sdump(results), err)
} else {
if len(results) != 1 {
t.Errorf("Expected 1 item, got %d", len(results))
// t.Logf("Table test: %# v\n", spew.Sdump(results))
}
}
if err := s.Delete("foo"); err != nil {
t.Errorf("Delete failed (%v)", err)
}
if results, err := s.Read("foo", store.ReadSuffix()); err != nil {
t.Errorf("Couldn't read all \"foo\" keys, got %# v (%s)", spew.Sdump(results), err)
} else {
if len(results) != 0 {
t.Errorf("Expected 0 items, got %d (%# v)", len(results), spew.Sdump(results))
}
}
}