Add tests for topic flags
This commit is contained in:
parent
e34af8d6eb
commit
fc3decf661
2
Makefile
2
Makefile
|
@ -28,7 +28,7 @@ DOCKER_IMAGE_NAME ?= statusteam/status-go
|
|||
DOCKER_TEST_WORKDIR = /go/src/github.com/status-im/status-go/
|
||||
DOCKER_TEST_IMAGE = golang:1.9
|
||||
|
||||
UNIT_TEST_PACKAGES := $(shell go list ./... | grep -v /vendor | grep -v /t/e2e | grep -v /t/destructive | grep -v /cmd | grep -v /lib)
|
||||
UNIT_TEST_PACKAGES := $(shell go list ./... | grep -v /vendor | grep -v /t/e2e | grep -v /t/destructive | grep -v /lib)
|
||||
|
||||
# This is a code for automatic help generator.
|
||||
# It supports ANSI colors and categories.
|
||||
|
|
|
@ -14,6 +14,7 @@ import (
|
|||
"github.com/ethereum/go-ethereum/log"
|
||||
"github.com/ethereum/go-ethereum/p2p/discv5"
|
||||
"github.com/status-im/status-go/cmd/statusd/debug"
|
||||
"github.com/status-im/status-go/cmd/statusd/topics"
|
||||
"github.com/status-im/status-go/geth/api"
|
||||
"github.com/status-im/status-go/geth/node"
|
||||
"github.com/status-im/status-go/geth/params"
|
||||
|
@ -75,8 +76,8 @@ var (
|
|||
syncAndExit = flag.Int("sync-and-exit", -1, "Timeout in minutes for blockchain sync and exit, zero means no timeout unless sync is finished")
|
||||
|
||||
// Topics that will be search and registered by discovery v5.
|
||||
searchTopics = topicLimitsFlag{}
|
||||
registerTopics = topicsFlag{}
|
||||
searchTopics = topics.TopicLimitsFlag{}
|
||||
registerTopics = topics.TopicFlag{}
|
||||
)
|
||||
|
||||
// All general log messages in this package should be routed through this logger.
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
Topics flags
|
||||
============
|
||||
|
||||
This module provides 2 helpers to parse collections of topics.
|
||||
|
||||
1. List of topics, such as:
|
||||
|
||||
```
|
||||
statusd -topic.register=whisper -topic.register=les
|
||||
```
|
||||
|
||||
2. List of topics with limits per topic. Main use case is to define per-protocol
|
||||
peer limits:
|
||||
|
||||
```
|
||||
statusd -topic.search=whisper=7,9 -topic.search=mailserver=1,1 -topic.search=les=1,2
|
||||
```
|
|
@ -1,4 +1,4 @@
|
|||
package main
|
||||
package topics
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
@ -9,24 +9,28 @@ import (
|
|||
"github.com/status-im/status-go/geth/params"
|
||||
)
|
||||
|
||||
type topicsFlag []discv5.Topic
|
||||
// TopicFlag used to parse discv5 topics.
|
||||
type TopicFlag []discv5.Topic
|
||||
|
||||
func (f *topicsFlag) String() string {
|
||||
func (f *TopicFlag) String() string {
|
||||
return "discv5 topics"
|
||||
}
|
||||
|
||||
func (f *topicsFlag) Set(value string) error {
|
||||
// Set parses single topic.
|
||||
func (f *TopicFlag) Set(value string) error {
|
||||
*f = append(*f, discv5.Topic(strings.TrimSpace(value)))
|
||||
return nil
|
||||
}
|
||||
|
||||
type topicLimitsFlag map[discv5.Topic]params.Limits
|
||||
// TopicLimitsFlag used to parse limits for discv5 topics.
|
||||
type TopicLimitsFlag map[discv5.Topic]params.Limits
|
||||
|
||||
func (f *topicLimitsFlag) String() string {
|
||||
func (f *TopicLimitsFlag) String() string {
|
||||
return "disv5 topics to limits map"
|
||||
}
|
||||
|
||||
func (f *topicLimitsFlag) Set(value string) error {
|
||||
// Set parses single limit for a topic.
|
||||
func (f *TopicLimitsFlag) Set(value string) error {
|
||||
parts := strings.Split(strings.TrimSpace(value), "=")
|
||||
if len(parts) != 2 {
|
||||
return errors.New("topic must be separated by '=' from limits, e.g. 'topic1=1,1'")
|
|
@ -0,0 +1,105 @@
|
|||
package topics
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/status-im/status-go/geth/params"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestTopicFlags(t *testing.T) {
|
||||
type testCase struct {
|
||||
shortcut string
|
||||
flags []string
|
||||
expected TopicFlag
|
||||
}
|
||||
|
||||
for _, tc := range []testCase{
|
||||
{
|
||||
shortcut: "single",
|
||||
flags: []string{"whisper"},
|
||||
expected: TopicFlag{"whisper"},
|
||||
},
|
||||
{
|
||||
shortcut: "multiple",
|
||||
flags: []string{"whisper", "les"},
|
||||
expected: TopicFlag{"whisper", "les"},
|
||||
},
|
||||
{
|
||||
shortcut: "corrupted",
|
||||
flags: []string{" whisper ", "les "},
|
||||
expected: TopicFlag{"whisper", "les"},
|
||||
},
|
||||
} {
|
||||
t.Run(tc.shortcut, func(t *testing.T) {
|
||||
result := TopicFlag{}
|
||||
for _, flag := range tc.flags {
|
||||
assert.NoError(t, result.Set(flag))
|
||||
}
|
||||
assert.Equal(t, tc.expected, result)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestTopicLimitsFlag(t *testing.T) {
|
||||
type testCase struct {
|
||||
shortcut string
|
||||
flags []string
|
||||
expected TopicLimitsFlag
|
||||
expectErr bool
|
||||
}
|
||||
for _, tc := range []testCase{
|
||||
{
|
||||
shortcut: "single",
|
||||
flags: []string{"whisper=1,1"},
|
||||
expected: TopicLimitsFlag{"whisper": params.Limits{1, 1}},
|
||||
},
|
||||
{
|
||||
shortcut: "multiple",
|
||||
flags: []string{"whisper=1,1", "les=2,3"},
|
||||
expected: TopicLimitsFlag{"whisper": params.Limits{1, 1}, "les": params.Limits{2, 3}},
|
||||
},
|
||||
{
|
||||
shortcut: "corrupted",
|
||||
flags: []string{" whisper=1,1 ", " les=2,3"},
|
||||
expected: TopicLimitsFlag{"whisper": params.Limits{1, 1}, "les": params.Limits{2, 3}},
|
||||
},
|
||||
{
|
||||
shortcut: "badseparator",
|
||||
flags: []string{"whisper==1,1"},
|
||||
expected: TopicLimitsFlag{},
|
||||
expectErr: true,
|
||||
},
|
||||
{
|
||||
shortcut: "singlelimit",
|
||||
flags: []string{"whisper=1"},
|
||||
expected: TopicLimitsFlag{},
|
||||
expectErr: true,
|
||||
},
|
||||
{
|
||||
shortcut: "minnotanumber",
|
||||
flags: []string{"whisper=a,1"},
|
||||
expected: TopicLimitsFlag{},
|
||||
expectErr: true,
|
||||
},
|
||||
{
|
||||
shortcut: "maxnotanumber",
|
||||
flags: []string{"whisper=1,a"},
|
||||
expected: TopicLimitsFlag{},
|
||||
expectErr: true,
|
||||
},
|
||||
} {
|
||||
t.Run(tc.shortcut, func(t *testing.T) {
|
||||
result := TopicLimitsFlag{}
|
||||
for _, flag := range tc.flags {
|
||||
err := result.Set(flag)
|
||||
if tc.expectErr {
|
||||
assert.Error(t, err)
|
||||
} else {
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
}
|
||||
assert.Equal(t, tc.expected, result)
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue