feat: Add rpc store

Add test for rpc services
This commit is contained in:
Anthony Laibe 2021-11-09 15:15:45 +01:00
parent 0c873e3c2d
commit b81bd6ff30
8 changed files with 216 additions and 2 deletions

View File

@ -1,7 +1,5 @@
package rpc
//
import (
"testing"

22
waku/v2/rpc/debug_test.go Normal file
View File

@ -0,0 +1,22 @@
package rpc
import (
"bytes"
"net/http"
"testing"
"github.com/stretchr/testify/require"
)
func TestGetV1Info(t *testing.T) {
var reply InfoReply
request, err := http.NewRequest(http.MethodPost, "url", bytes.NewReader([]byte("")))
require.NoError(t, err)
d := &DebugService{nil}
err = d.GetV1Info(request, &InfoArgs{}, &reply)
require.NoError(t, err)
require.Equal(t, "2.0", reply.Version)
}

55
waku/v2/rpc/relay_test.go Normal file
View File

@ -0,0 +1,55 @@
package rpc
import (
"context"
"testing"
"github.com/status-im/go-waku/waku/v2/node"
"github.com/stretchr/testify/require"
)
func makeRelayService(t *testing.T) *RelayService {
options := node.WithWakuRelay()
n, err := node.New(context.Background(), options)
require.NoError(t, err)
err = n.Start()
require.NoError(t, err)
return &RelayService{n}
}
func TestPostV1Message(t *testing.T) {
var reply SuccessReply
d := makeRelayService(t)
err := d.PostV1Message(
makeRequest(t),
&RelayMessageArgs{},
&reply,
)
require.NoError(t, err)
require.True(t, reply.Success)
}
func TestSubscription(t *testing.T) {
var reply SuccessReply
d := makeRelayService(t)
args := &TopicsArgs{Topics: []string{"test"}}
err := d.PostV1Subscription(
makeRequest(t),
args,
&reply,
)
require.NoError(t, err)
require.True(t, reply.Success)
err = d.DeleteV1Subscription(
makeRequest(t),
args,
&reply,
)
require.NoError(t, err)
require.True(t, reply.Success)
}

68
waku/v2/rpc/store.go Normal file
View File

@ -0,0 +1,68 @@
package rpc
import (
"net/http"
"github.com/status-im/go-waku/waku/v2/node"
"github.com/status-im/go-waku/waku/v2/protocol/pb"
"github.com/status-im/go-waku/waku/v2/protocol/store"
)
type StoreService struct {
node *node.WakuNode
}
// cursor *pb.Index
// pageSize uint64
// asc bool
type StorePagingOptions struct {
PageSize uint64 `json:"pageSize,omitempty"`
Cursor *pb.Index `json:"cursor,omitempty"`
Forward bool `json:"forward,omitempty"`
}
type StoreMessagesArgs struct {
Topic string `json:"pubsubTopic,omitempty"`
ContentFilters []string `json:"contentFilters,omitempty"`
StartTime float64 `json:"startTime,omitempty"`
EndTime float64 `json:"endTime,omitempty"`
PagingOptions StorePagingOptions `json:"pagingOptions,omitempty"`
}
type StoreMessagesReply struct {
Messages []*pb.WakuMessage `json:"messages,omitempty"`
PagingInfo StorePagingOptions `json:"pagingInfo,omitempty"`
Error string `json:"error,omitempty"`
}
func (s *StoreService) GetV1Messages(req *http.Request, args *StoreMessagesArgs, reply *StoreMessagesReply) error {
options := []store.HistoryRequestOption{
store.WithAutomaticRequestId(),
store.WithAutomaticPeerSelection(),
store.WithPaging(args.PagingOptions.Forward, args.PagingOptions.PageSize),
store.WithCursor(args.PagingOptions.Cursor),
}
res, err := s.node.Store().Query(
req.Context(),
store.Query{
Topic: args.Topic,
ContentTopics: args.ContentFilters,
StartTime: args.StartTime,
EndTime: args.EndTime,
},
options...,
)
if err != nil {
log.Error("Error querying messages:", err)
reply.Error = err.Error()
return nil
}
reply.Messages = res.Messages
reply.PagingInfo = StorePagingOptions{
PageSize: args.PagingOptions.PageSize,
Cursor: res.Cursor(),
Forward: args.PagingOptions.Forward,
}
return nil
}

32
waku/v2/rpc/store_test.go Normal file
View File

@ -0,0 +1,32 @@
package rpc
import (
"context"
"testing"
"github.com/status-im/go-waku/waku/v2/node"
"github.com/stretchr/testify/require"
)
func makeStoreService(t *testing.T) *StoreService {
options := node.WithWakuStore(false, false)
n, err := node.New(context.Background(), options)
require.NoError(t, err)
err = n.Start()
require.NoError(t, err)
return &StoreService{n}
}
func TestGetV1Message(t *testing.T) {
var reply StoreMessagesReply
s := makeStoreService(t)
err := s.GetV1Messages(
makeRequest(t),
&StoreMessagesArgs{},
&reply,
)
require.NoError(t, err)
require.NotEmpty(t, reply.Error)
}

15
waku/v2/rpc/util_test.go Normal file
View File

@ -0,0 +1,15 @@
package rpc
import (
"bytes"
"net/http"
"testing"
"github.com/stretchr/testify/require"
)
func makeRequest(t *testing.T) *http.Request {
request, err := http.NewRequest(http.MethodPost, "url", bytes.NewReader([]byte("")))
require.NoError(t, err)
return request
}

View File

@ -33,6 +33,11 @@ func NewWakuRpc(node *node.WakuNode, address string, port int) *WakuRpc {
log.Error(err)
}
err = s.RegisterService(&StoreService{node}, "Store")
if err != nil {
log.Error(err)
}
mux := http.NewServeMux()
mux.HandleFunc("/jsonrpc", func(w http.ResponseWriter, r *http.Request) {
t := time.Now()

View File

@ -0,0 +1,19 @@
package rpc
import (
"context"
"testing"
"github.com/status-im/go-waku/waku/v2/node"
"github.com/stretchr/testify/require"
)
func TestWakuRpc(t *testing.T) {
options := node.WithWakuStore(false, false)
n, err := node.New(context.Background(), options)
require.NoError(t, err)
rpc := NewWakuRpc(n, "127.0.0.1", 8080)
require.NotNil(t, rpc.server)
require.Equal(t, rpc.server.Addr, "127.0.0.1:8080")
}