Fix utests

This commit is contained in:
Širhoe Biazhkovič
2021-09-05 17:32:31 +03:00
parent cb4ac3c7f8
commit 20f1a99ec2
6 changed files with 43 additions and 40 deletions

View File

@@ -65,7 +65,7 @@ func generateInfohash() [20]byte {
panic(fmt.Errorf("not enough randomness? Got %d bytes", n)) panic(fmt.Errorf("not enough randomness? Got %d bytes", n))
} }
return [20]byte(bittorrent.InfoHashFromBytes(b)) return bittorrent.InfoHashFromBytes(b)
} }
func test(addr string, delay time.Duration) error { func test(addr string, delay time.Duration) error {

View File

@@ -3,25 +3,21 @@ package container
import ( import (
"errors" "errors"
"github.com/chihaya/chihaya/bittorrent" "github.com/chihaya/chihaya/bittorrent"
"github.com/chihaya/chihaya/pkg/stop"
"gopkg.in/yaml.v2"
"sync" "sync"
) )
type Constructor func () Configuration type Builder interface {
Build([]byte) (Container, error)
type Configuration interface {
Build() (Container, error)
} }
var ( var (
constructorsMU sync.Mutex buildersMU sync.Mutex
constructors = make(map[string]Constructor) builders = make(map[string]Builder)
ErrContainerDoesNotExist = errors.New("torrent hash container with that name does not exist") ErrContainerDoesNotExist = errors.New("torrent hash container with that name does not exist")
) )
func Register(n string, c Constructor) { func Register(n string, c Builder) {
if len(n) == 0 { if len(n) == 0 {
panic("middleware: could not register a Container with an empty name") panic("middleware: could not register a Container with an empty name")
} }
@@ -29,28 +25,25 @@ func Register(n string, c Constructor) {
panic("middleware: could not register a Container with nil builder constructor") panic("middleware: could not register a Container with nil builder constructor")
} }
constructorsMU.Lock() buildersMU.Lock()
defer constructorsMU.Unlock() defer buildersMU.Unlock()
constructors[n] = c builders[n] = c
} }
type Container interface { type Container interface {
stop.Stopper
Contains(bittorrent.InfoHash) bool Contains(bittorrent.InfoHash) bool
} }
func GetContainer(name string, confBytes []byte) (Container, error) { func GetContainer(name string, confBytes []byte) (Container, error) {
constructorsMU.Lock()
defer constructorsMU.Unlock() buildersMU.Lock()
defer buildersMU.Unlock()
var err error var err error
var cn Container var cn Container
if getConfig, exist := constructors[name]; !exist { if builder, exist := builders[name]; !exist {
err = ErrContainerDoesNotExist err = ErrContainerDoesNotExist
} else { } else {
conf := getConfig() cn, err = builder.Build(confBytes)
if err = yaml.Unmarshal(confBytes, &conf); err == nil {
cn, err = conf.Build()
}
} }
return cn, err return cn, err
} }

View File

@@ -6,35 +6,40 @@ import (
"github.com/chihaya/chihaya/middleware/torrentapproval/container" "github.com/chihaya/chihaya/middleware/torrentapproval/container"
"github.com/chihaya/chihaya/middleware/torrentapproval/container/list" "github.com/chihaya/chihaya/middleware/torrentapproval/container/list"
"github.com/chihaya/chihaya/pkg/stop" "github.com/chihaya/chihaya/pkg/stop"
"gopkg.in/yaml.v2"
"sync" "sync"
) )
func init() { func init() {
container.Register("list", func() container.Configuration { container.Register("directory", builder{})
return Config{}
})
} }
type builder struct {}
type Config struct { type Config struct {
WhitelistPath string `yaml:"whitelist_path"` WhitelistPath string `yaml:"whitelist_path"`
BlacklistPath string `yaml:"blacklist_path"` BlacklistPath string `yaml:"blacklist_path"`
} }
func (b Config) Build() (container.Container, error) { func (b builder) Build(confBytes []byte) (container.Container, error) {
if len(b.WhitelistPath) > 0 && len(b.BlacklistPath) > 0 { c := new(Config)
if err := yaml.Unmarshal(confBytes, c); err != nil {
return nil, fmt.Errorf("unable to deserialise configuration: %v", err)
}
if len(c.WhitelistPath) > 0 && len(c.BlacklistPath) > 0 {
return nil, fmt.Errorf("using both whitelist and blacklist is invalid") return nil, fmt.Errorf("using both whitelist and blacklist is invalid")
} }
var err error var err error
lst := &directory{ lst := &directory{
List: list.List{ List: list.List{
Hashes: sync.Map{}, Hashes: sync.Map{},
Invert: len(b.WhitelistPath) == 0, Invert: len(c.WhitelistPath) == 0,
}, },
watcher: nil, watcher: nil,
} }
dir := b.WhitelistPath dir := c.WhitelistPath
if lst.Invert { if lst.Invert {
dir = b.BlacklistPath dir = c.BlacklistPath
} }
var w *dirwatch.Instance var w *dirwatch.Instance
w, err = dirwatch.New(dir) w, err = dirwatch.New(dir)

View File

@@ -5,16 +5,16 @@ import (
"fmt" "fmt"
"github.com/chihaya/chihaya/bittorrent" "github.com/chihaya/chihaya/bittorrent"
"github.com/chihaya/chihaya/middleware/torrentapproval/container" "github.com/chihaya/chihaya/middleware/torrentapproval/container"
"github.com/chihaya/chihaya/pkg/stop" "gopkg.in/yaml.v2"
"sync" "sync"
) )
func init() { func init() {
container.Register("list", func() container.Configuration { container.Register("list", builder{})
return Config{}
})
} }
type builder struct {}
type Config struct { type Config struct {
Whitelist []string `yaml:"whitelist"` Whitelist []string `yaml:"whitelist"`
Blacklist []string `yaml:"blacklist"` Blacklist []string `yaml:"blacklist"`
@@ -22,7 +22,11 @@ type Config struct {
var DUMMY struct{} var DUMMY struct{}
func (c Config) Build() (container.Container, error) { func (b builder) Build(confBytes []byte) (container.Container, error) {
c := new(Config)
if err := yaml.Unmarshal(confBytes, c); err != nil {
return nil, fmt.Errorf("unable to deserialise configuration: %v", err)
}
if len(c.Whitelist) > 0 && len(c.Blacklist) > 0 { if len(c.Whitelist) > 0 && len(c.Blacklist) > 0 {
return nil, fmt.Errorf("using both whitelist and blacklist is invalid") return nil, fmt.Errorf("using both whitelist and blacklist is invalid")
} }
@@ -55,10 +59,6 @@ type List struct {
Hashes sync.Map Hashes sync.Map
} }
func (l *List) Stop() stop.Result {
return stop.AlreadyStopped
}
func (l *List) Contains(hash bittorrent.InfoHash) bool { func (l *List) Contains(hash bittorrent.InfoHash) bool {
_, result := l.Hashes.Load(hash) _, result := l.Hashes.Load(hash)
return result != l.Invert return result != l.Invert

View File

@@ -11,6 +11,8 @@ import (
"github.com/chihaya/chihaya/bittorrent" "github.com/chihaya/chihaya/bittorrent"
"github.com/chihaya/chihaya/middleware" "github.com/chihaya/chihaya/middleware"
_ "github.com/chihaya/chihaya/middleware/torrentapproval/container/directory"
_ "github.com/chihaya/chihaya/middleware/torrentapproval/container/list"
) )
// Name is the name by which this middleware is registered with Chihaya. // Name is the name by which this middleware is registered with Chihaya.
@@ -73,5 +75,8 @@ func (h *hook) HandleScrape(ctx context.Context, req *bittorrent.ScrapeRequest,
} }
func (h *hook) Stop() stop.Result { func (h *hook) Stop() stop.Result {
return h.hashContainer.Stop() if st, isOk := h.hashContainer.(stop.Stopper); isOk{
return st.Stop()
}
return stop.AlreadyStopped
} }

View File

@@ -68,7 +68,7 @@ func TestHandleAnnounce(t *testing.T) {
for _, tt := range cases { for _, tt := range cases {
t.Run(fmt.Sprintf("testing hash %s", tt.ih), func(t *testing.T) { t.Run(fmt.Sprintf("testing hash %s", tt.ih), func(t *testing.T) {
d := driver{} d := driver{}
cfg, err := yaml.Marshal(tt) cfg, err := yaml.Marshal(tt.cfg)
require.Nil(t, err) require.Nil(t, err)
h, err := d.NewHook(cfg) h, err := d.NewHook(cfg)
require.Nil(t, err) require.Nil(t, err)