rulelist: imp docs, tests

This commit is contained in:
Ainar Garipov 2024-11-01 15:54:18 +03:00
parent 7a759c4699
commit 5efcfda937
6 changed files with 23 additions and 15 deletions

View file

@ -19,6 +19,7 @@ import (
//
// TODO(a.garipov): Merge with [TextEngine] in some way?
type Engine struct {
// logger is used to log the operation of the engine and its refreshes.
logger *slog.Logger
// mu protects engine and storage.
@ -32,8 +33,7 @@ type Engine struct {
// storage is the filtering-rule storage. It is saved here to close it.
storage *filterlist.RuleStorage
// name is the human-readable name of the engine, like "allowed", "blocked",
// or "custom", used to report errors.
// name is the human-readable name of the engine.
name string
// filters is the data about rule filters in this engine.
@ -46,8 +46,8 @@ type EngineConfig struct {
// Logger is used to log the operation of the engine. It must not be nil.
Logger *slog.Logger
// name is the human-readable name of the engine, like "allowed", "blocked",
// or "custom", used to report errors.
// name is the human-readable name of the engine; see [EngineNameAllow] and
// similar constants.
Name string
// Filters is the data about rule lists in this engine. There must be no
@ -92,7 +92,7 @@ func (e *Engine) FilterRequest(
}
// currentEngine returns the current filtering engine.
func (e *Engine) currentEngine() (enging *urlfilter.DNSEngine) {
func (e *Engine) currentEngine() (engine *urlfilter.DNSEngine) {
e.mu.RLock()
defer e.mu.RUnlock()
@ -103,7 +103,7 @@ func (e *Engine) currentEngine() (enging *urlfilter.DNSEngine) {
// parseBuf, cli, cacheDir, and maxSize are used for updates of rule-list
// filters; see [Filter.Refresh].
//
// TODO(a.garipov): Unexport and test in an internal test or through enigne
// TODO(a.garipov): Unexport and test in an internal test or through engine
// tests.
func (e *Engine) Refresh(
ctx context.Context,

View file

@ -105,7 +105,7 @@ func NewFilter(c *FilterConfig) (f *Filter, err error) {
// buffer used to parse information from the data. cli and maxSize are only
// used when f is a URL-based list.
//
// TODO(a.garipov): Unexport and test in an internal test or through enigne
// TODO(a.garipov): Unexport and test in an internal test or through engine
// tests.
//
// TODO(a.garipov): Consider not returning parseRes.

View file

@ -71,3 +71,10 @@ var _ fmt.Stringer = UID{}
func (id UID) String() (s string) {
return uuid.UUID(id).String()
}
// Common engine names.
const (
EngineNameAllow = "allow"
EngineNameBlock = "block"
EngineNameCustom = "custom"
)

View file

@ -60,7 +60,7 @@ type StorageConfig struct {
// refreshed, so a refresh should be performed before use.
func NewStorage(c *StorageConfig) (s *Storage, err error) {
custom, err := NewTextEngine(&TextEngineConfig{
Name: "custom",
Name: EngineNameCustom,
Rules: c.CustomRules,
ID: URLFilterIDCustom,
})
@ -71,13 +71,13 @@ func NewStorage(c *StorageConfig) (s *Storage, err error) {
return &Storage{
refreshMu: &sync.Mutex{},
allow: NewEngine(&EngineConfig{
Logger: c.Logger.With("engine", "allow"),
Name: "allow",
Logger: c.Logger.With("engine", EngineNameAllow),
Name: EngineNameAllow,
Filters: c.AllowFilters,
}),
block: NewEngine(&EngineConfig{
Logger: c.Logger.With("engine", "block"),
Name: "block",
Logger: c.Logger.With("engine", EngineNameBlock),
Name: EngineNameBlock,
Filters: c.BlockFilters,
}),
custom: custom,

View file

@ -41,6 +41,7 @@ func TestStorage_Refresh(t *testing.T) {
MaxRuleListTextSize: 1 * datasize.KB,
})
require.NoError(t, err)
testutil.CleanupAndRequireSuccess(t, strg.Close)
ctx := testutil.ContextWithTimeout(t, testTimeout)
err = strg.Refresh(ctx)

View file

@ -20,15 +20,15 @@ type TextEngine struct {
// storage is the filtering-rule storage. It is saved here to close it.
storage *filterlist.RuleStorage
// name is the human-readable name of the engine, like "custom".
// name is the human-readable name of the engine.
name string
}
// TextEngineConfig is the configuration for a rule-list filtering engine
// created from a filtering rule text.
type TextEngineConfig struct {
// Name is the human-readable name of this engine, like "allowed",
// "blocked", or "custom".
// name is the human-readable name of the engine; see [EngineNameAllow] and
// similar constants.
Name string
// Rules is the text of the filtering rules for this engine.