refactor: flatten and separate (#353)

* refactor(atomicx): move outside the engine package

After merging probe-engine into probe-cli, my impression is that we have
too much unnecessary nesting of packages in this repository.

The idea of this commit and of a bunch of following commits will instead
be to reduce the nesting and simplify the structure.

While there, improve the documentation.

* fix: always use the atomicx package

For consistency, never use sync/atomic and always use ./internal/atomicx
so we can just grep and make sure we're not risking to crash if we make
a subtle mistake on a 32 bit platform.

While there, mention in the contributing guidelines that we want to
always prefer the ./internal/atomicx package over sync/atomic.

* fix(atomicx): remove unnecessary constructor

We don't need a constructor here. The default constructed `&Int64{}`
instance is already usable and the constructor does not add anything to
what we are doing, rather it just creates extra confusion.

* cleanup(atomicx): we are not using Float64

Because atomicx.Float64 is unused, we can safely zap it.

* cleanup(atomicx): simplify impl and improve tests

We can simplify the implementation by using defer and by letting
the Load() method call Add(0).

We can improve tests by making many goroutines updated the
atomic int64 value concurrently.

* refactor(fsx): can live in the ./internal pkg

Let us reduce the amount of nesting. While there, ensure that the
package only exports the bare minimum, and improve the documentation
of the tests, to ease reading the code.

* refactor: move runtimex to ./internal

* refactor: move shellx into the ./internal package

While there, remove unnecessary dependency between packages.

While there, specify in the contributing guidelines that
one should use x/sys/execabs instead of os/exec.

* refactor: move ooapi into the ./internal pkg

* refactor(humanize): move to ./internal and better docs

* refactor: move platform to ./internal

* refactor(randx): move to ./internal

* refactor(multierror): move into the ./internal pkg

* refactor(kvstore): all kvstores in ./internal

Rather than having part of the kvstore inside ./internal/engine/kvstore
and part in ./internal/engine/kvstore.go, let us put every piece of code
that is kvstore related into the ./internal/kvstore package.

* fix(kvstore): always return ErrNoSuchKey on Get() error

It should help to use the kvstore everywhere removing all the
copies that are lingering around the tree.

* sessionresolver: make KVStore mandatory

Simplifies implementation. While there, use the ./internal/kvstore
package rather than having our private implementation.

* fix(ooapi): use the ./internal/kvstore package

* fix(platform): better documentation
This commit is contained in:
Simone Basso 2021-06-04 10:34:18 +02:00 committed by GitHub
commit 33de701263
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
169 changed files with 1136 additions and 1003 deletions

View file

@ -1,41 +0,0 @@
// Package fsx contains file system extension
package fsx
import (
"fmt"
"io/fs"
"os"
"syscall"
)
// Open is a wrapper for os.Open that ensures that we're opening a file.
func Open(pathname string) (fs.File, error) {
return OpenWithFS(filesystem{}, pathname)
}
// OpenWithFS is like Open but with explicit file system argument.
func OpenWithFS(fs fs.FS, pathname string) (fs.File, error) {
file, err := fs.Open(pathname)
if err != nil {
return nil, err
}
info, err := file.Stat()
if err != nil {
file.Close()
return nil, err
}
if info.IsDir() {
file.Close()
return nil, fmt.Errorf(
"input path points to a directory: %w", syscall.EISDIR)
}
return file, nil
}
// filesystem is a private implementation of fs.FS.
type filesystem struct{}
// Open implements fs.FS.Open.
func (filesystem) Open(pathname string) (fs.File, error) {
return os.Open(pathname)
}

View file

@ -1,76 +0,0 @@
package fsx_test
import (
"errors"
"io/fs"
"os"
"sync/atomic"
"syscall"
"testing"
"github.com/ooni/probe-cli/v3/internal/engine/internal/fsx"
)
var StateBaseDir = "./testdata/"
type FailingStatFS struct {
CloseCount *int32
}
type FailingStatFile struct {
CloseCount *int32
}
var errStatFailed = errors.New("stat failed")
func (FailingStatFile) Stat() (os.FileInfo, error) {
return nil, errStatFailed
}
func (f FailingStatFS) Open(pathname string) (fs.File, error) {
return FailingStatFile(f), nil
}
func (fs FailingStatFile) Close() error {
if fs.CloseCount != nil {
atomic.AddInt32(fs.CloseCount, 1)
}
return nil
}
func (FailingStatFile) Read([]byte) (int, error) {
return 0, nil
}
func TestOpenWithFailingStat(t *testing.T) {
var count int32
_, err := fsx.OpenWithFS(FailingStatFS{CloseCount: &count}, StateBaseDir+"testfile.txt")
if !errors.Is(err, errStatFailed) {
t.Errorf("expected error with invalid FS: %+v", err)
}
if count != 1 {
t.Error("expected counter to be equal to 1")
}
}
func TestOpenNonexistentFile(t *testing.T) {
_, err := fsx.Open(StateBaseDir + "invalidtestfile.txt")
if !errors.Is(err, syscall.ENOENT) {
t.Errorf("not the error we expected")
}
}
func TestOpenDirectoryShouldFail(t *testing.T) {
_, err := fsx.Open(StateBaseDir)
if !errors.Is(err, syscall.EISDIR) {
t.Fatalf("not the error we expected: %+v", err)
}
}
func TestOpeningExistingFileShouldWork(t *testing.T) {
file, err := fsx.Open(StateBaseDir + "testfile.txt")
if err != nil {
t.Fatal(err)
}
defer file.Close()
}

View file

@ -1 +0,0 @@

View file

@ -1 +0,0 @@
my test input

View file

@ -6,9 +6,9 @@ import (
"net/http"
"net/url"
"github.com/ooni/probe-cli/v3/internal/engine/kvstore"
"github.com/ooni/probe-cli/v3/internal/engine/model"
"github.com/ooni/probe-cli/v3/internal/engine/probeservices"
"github.com/ooni/probe-cli/v3/internal/kvstore"
)
// Session allows to mock sessions.
@ -67,7 +67,7 @@ func (sess *Session) FetchURLList(
// KeyValueStore returns the configured key-value store.
func (sess *Session) KeyValueStore() model.KeyValueStore {
return kvstore.NewMemoryKeyValueStore()
return &kvstore.Memory{}
}
// Logger implements ExperimentSession.Logger

View file

@ -1,66 +0,0 @@
// Package multierror contains code to manage multiple errors.
package multierror
import (
"errors"
"fmt"
"strings"
)
// Union is the logical union of several errors. The Union will
// appear to be the Root error, except that it will actually
// be possible to look deeper and see specific sub errors that
// occurred using errors.As and errors.Is.
type Union struct {
Children []error
Root error
}
// New creates a new Union error instance.
func New(root error) *Union {
return &Union{Root: root}
}
// Unwrap returns the Root error of the Union error.
func (err Union) Unwrap() error {
return err.Root
}
// Add adds the specified child error to the Union error.
func (err *Union) Add(child error) {
err.Children = append(err.Children, child)
}
// AddWithPrefix adds the specified child error to the Union error
// with the specified prefix before the child error.
func (err *Union) AddWithPrefix(prefix string, child error) {
err.Add(fmt.Errorf("%s: %w", prefix, child))
}
// Is returns whether the Union error contains at least one child
// error that is exactly the specified target error.
func (err Union) Is(target error) bool {
if errors.Is(err.Root, target) {
return true
}
for _, c := range err.Children {
if errors.Is(c, target) {
return true
}
}
return false
}
// Error returns a string representation of the Union error.
func (err Union) Error() string {
var sb strings.Builder
sb.WriteString(err.Root.Error())
sb.WriteString(": [")
for _, c := range err.Children {
sb.WriteString(" ")
sb.WriteString(c.Error())
sb.WriteString(";")
}
sb.WriteString("]")
return sb.String()
}

View file

@ -1,85 +0,0 @@
package multierror_test
import (
"context"
"errors"
"fmt"
"io"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/ooni/probe-cli/v3/internal/engine/internal/multierror"
)
func TestEmpty(t *testing.T) {
root := errors.New("antani")
var err error = multierror.New(root)
if err.Error() != "antani: []" {
t.Fatal("unexpected Error value")
}
if !errors.Is(err, root) {
t.Fatal("error should be root")
}
if !errors.Is(errors.Unwrap(err), root) {
t.Fatal("unwrapping did not return root")
}
if errors.Is(err, io.EOF) {
t.Fatal("error should not be EOF")
}
}
func TestNonEmpty(t *testing.T) {
root := errors.New("antani")
container := multierror.New(root)
container.AddWithPrefix("first operation failed", io.EOF)
container.AddWithPrefix("second operation failed", context.Canceled)
var err error = container
expect := "antani: [ first operation failed: EOF; second operation failed: context canceled;]"
if diff := cmp.Diff(err.Error(), expect); diff != "" {
t.Fatal(diff)
}
if !errors.Is(err, root) {
t.Fatal("error should be root")
}
if !errors.Is(errors.Unwrap(err), root) {
t.Fatal("unwrapping did not return root")
}
if !errors.Is(err, io.EOF) {
t.Fatal("error should be EOF")
}
if !errors.Is(err, context.Canceled) {
t.Fatal("error should be context.Canceled")
}
var as *multierror.Union
if !errors.As(err, &as) {
t.Fatal("cannot cast error to multierror.Union")
}
if !errors.Is(as.Root, root) {
t.Fatal("unexpected root")
}
if len(as.Children) != 2 {
t.Fatal("unexpected number of children")
}
}
type SpecificRootError struct {
Value int
}
func (sre SpecificRootError) Error() string {
return fmt.Sprintf("%d", sre.Value)
}
func TestAsWorksForRoot(t *testing.T) {
const expected = 144
var (
err error = multierror.New(&SpecificRootError{Value: expected})
sre *SpecificRootError
)
if !errors.As(err, &sre) {
t.Fatal("cannot cast error to original type")
}
if sre.Value != expected {
t.Fatal("unexpected sre.Value")
}
}

View file

@ -1,46 +0,0 @@
// Package platform returns the platform name. The name returned here
// is compatible with the names returned by Measurement Kit.
package platform
import "runtime"
// Name returns the platform name. The returned value is one of:
//
// 1. "android"
// 2. "ios"
// 3. "linux"
// 5. "macos"
// 4. "windows"
// 5. "unknown"
//
// The android, ios, linux, macos, windows, and unknown strings are
// also returned by Measurement Kit. As a known bug, the detection of
// darwin-based systems relies on the architecture, when CGO support
// has been disabled. In such case, the code will return "ios" when
// using arm{,64} and "macos" when using x86{,_64}.
func Name() string {
if name := cgoname(); name != "unknown" {
return name
}
return puregoname(runtime.GOOS, runtime.GOARCH)
}
func puregoname(goos, goarch string) string {
switch goos {
case "android", "linux", "windows":
return goos
case "darwin":
return detectDarwin(goarch)
}
return "unknown"
}
func detectDarwin(goarch string) string {
switch goarch {
case "386", "amd64":
return "macos"
case "arm", "arm64":
return "ios"
}
return "unknown"
}

View file

@ -1,31 +0,0 @@
// +build cgo
package platform
//
// /* Guess the platform in which we are.
//
// See: <https://sourceforge.net/p/predef/wiki/OperatingSystems/>
// <http://stackoverflow.com/a/18729350> */
//
//#if defined __ANDROID__
//# define OONI_PLATFORM "android"
//#elif defined __linux__
//# define OONI_PLATFORM "linux"
//#elif defined _WIN32
//# define OONI_PLATFORM "windows"
//#elif defined __APPLE__
//# include <TargetConditionals.h>
//# if TARGET_OS_IPHONE
//# define OONI_PLATFORM "ios"
//# else
//# define OONI_PLATFORM "macos"
//# endif
//#else
//# define OONI_PLATFORM "unknown"
//#endif
import "C"
func cgoname() string {
return C.OONI_PLATFORM
}

View file

@ -1,7 +0,0 @@
// +build !cgo
package platform
func cgoname() string {
return "unknown"
}

View file

@ -1,68 +0,0 @@
package platform
import (
"fmt"
"testing"
)
func TestGood(t *testing.T) {
var expected bool
switch Name() {
case "android", "ios", "linux", "macos", "windows":
expected = true
}
if !expected {
t.Fatal("unexpected platform name")
}
}
func TestPuregoname(t *testing.T) {
var runtimevariables = []struct {
expected string
goarch string
goos string
}{{
expected: "android",
goarch: "*",
goos: "android",
}, {
expected: "ios",
goarch: "arm64",
goos: "darwin",
}, {
expected: "ios",
goarch: "arm",
goos: "darwin",
}, {
expected: "linux",
goarch: "*",
goos: "linux",
}, {
expected: "macos",
goarch: "amd64",
goos: "darwin",
}, {
expected: "macos",
goarch: "386",
goos: "darwin",
}, {
expected: "unknown",
goarch: "*",
goos: "solaris",
}, {
expected: "unknown",
goarch: "mips",
goos: "darwin",
}, {
expected: "windows",
goarch: "*",
goos: "windows",
}}
for _, v := range runtimevariables {
t.Run(fmt.Sprintf("with %s/%s", v.goos, v.goarch), func(t *testing.T) {
if puregoname(v.goos, v.goarch) != v.expected {
t.Fatal("unexpected results")
}
})
}
}

View file

@ -1,50 +0,0 @@
// Package randx contains math/rand extensions
package randx
import (
"math/rand"
"time"
"unicode"
)
const (
uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
lowercase = "abcdefghijklmnopqrstuvwxyz"
letters = uppercase + lowercase
)
func lettersWithString(n int, letterBytes string) string {
// See https://stackoverflow.com/questions/22892120
rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
b := make([]byte, n)
for i := range b {
b[i] = letterBytes[rnd.Intn(len(letterBytes))]
}
return string(b)
}
// Letters return a string composed of random letters
func Letters(n int) string {
return lettersWithString(n, letters)
}
// LettersUppercase return a string composed of random uppercase letters
func LettersUppercase(n int) string {
return lettersWithString(n, uppercase)
}
// ChangeCapitalization returns a new string where the capitalization
// of each character is changed at random.
func ChangeCapitalization(source string) (dest string) {
rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
for _, chr := range source {
if unicode.IsLower(chr) && rnd.Float64() <= 0.5 {
dest += string(unicode.ToUpper(chr))
} else if unicode.IsUpper(chr) && rnd.Float64() <= 0.5 {
dest += string(unicode.ToLower(chr))
} else {
dest += string(chr)
}
}
return
}

View file

@ -1,34 +0,0 @@
package randx_test
import (
"testing"
"github.com/ooni/probe-cli/v3/internal/engine/internal/randx"
)
func TestLetters(t *testing.T) {
str := randx.Letters(1024)
for _, chr := range str {
if (chr >= 'A' && chr <= 'Z') || (chr >= 'a' && chr <= 'z') {
continue
}
t.Fatal("invalid input char")
}
}
func TestLettersUppercase(t *testing.T) {
str := randx.LettersUppercase(1024)
for _, chr := range str {
if chr >= 'A' && chr <= 'Z' {
continue
}
t.Fatal("invalid input char")
}
}
func TestChangeCapitalization(t *testing.T) {
str := randx.Letters(2048)
if randx.ChangeCapitalization(str) == str {
t.Fatal("capitalization not changed")
}
}

View file

@ -5,13 +5,16 @@ import (
"testing"
"github.com/ooni/probe-cli/v3/internal/engine/internal/sessionresolver"
"github.com/ooni/probe-cli/v3/internal/kvstore"
)
func TestSessionResolverGood(t *testing.T) {
if testing.Short() {
t.Skip("skip test in short mode")
}
reso := &sessionresolver.Resolver{}
reso := &sessionresolver.Resolver{
KVStore: &kvstore.Memory{},
}
defer reso.CloseIdleConnections()
if reso.Network() != "sessionresolver" {
t.Fatal("unexpected Network")

View file

@ -1,43 +0,0 @@
package sessionresolver
import (
"errors"
"fmt"
"sync"
)
func (r *Resolver) kvstore() KVStore {
defer r.mu.Unlock()
r.mu.Lock()
if r.KVStore == nil {
r.KVStore = &memkvstore{}
}
return r.KVStore
}
var errMemkvstoreNotFound = errors.New("memkvstore: not found")
type memkvstore struct {
m map[string][]byte
mu sync.Mutex
}
func (kvs *memkvstore) Get(key string) ([]byte, error) {
defer kvs.mu.Unlock()
kvs.mu.Lock()
out, good := kvs.m[key]
if !good {
return nil, fmt.Errorf("%w: %s", errMemkvstoreNotFound, key)
}
return out, nil
}
func (kvs *memkvstore) Set(key string, value []byte) error {
defer kvs.mu.Unlock()
kvs.mu.Lock()
if kvs.m == nil {
kvs.m = make(map[string][]byte)
}
kvs.m[key] = value
return nil
}

View file

@ -1,47 +0,0 @@
package sessionresolver
import (
"errors"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestKVStoreCustom(t *testing.T) {
kvs := &memkvstore{}
reso := &Resolver{KVStore: kvs}
o := reso.kvstore()
if o != kvs {
t.Fatal("not the kvstore we expected")
}
}
func TestMemkvstoreGetNotFound(t *testing.T) {
reso := &Resolver{}
key := "antani"
out, err := reso.kvstore().Get(key)
if !errors.Is(err, errMemkvstoreNotFound) {
t.Fatal("not the error we expected", err)
}
if out != nil {
t.Fatal("expected nil here")
}
}
func TestMemkvstoreRoundTrip(t *testing.T) {
reso := &Resolver{}
key := []string{"antani", "mascetti"}
value := [][]byte{[]byte(`mascetti`), []byte(`antani`)}
for idx := 0; idx < 2; idx++ {
if err := reso.kvstore().Set(key[idx], value[idx]); err != nil {
t.Fatal(err)
}
out, err := reso.kvstore().Get(key[idx])
if err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(value[idx], out); diff != "" {
t.Fatal(diff)
}
}
}

View file

@ -33,9 +33,9 @@ import (
"sync"
"time"
"github.com/ooni/probe-cli/v3/internal/engine/internal/multierror"
"github.com/ooni/probe-cli/v3/internal/engine/netx/bytecounter"
"github.com/ooni/probe-cli/v3/internal/engine/runtimex"
"github.com/ooni/probe-cli/v3/internal/multierror"
"github.com/ooni/probe-cli/v3/internal/runtimex"
)
// Resolver is the session resolver. Resolver will try to use
@ -45,6 +45,9 @@ import (
// and therefore we can generally give preference to underlying
// DoT/DoH resolvers that work better.
//
// Make sure you fill the mandatory fields (indicated below)
// before using this data structure.
//
// You MUST NOT modify public fields of this structure once it
// has been created, because that MAY lead to data races.
//
@ -57,10 +60,9 @@ type Resolver struct {
// field is not set, then we won't count the bytes.
ByteCounter *bytecounter.Counter
// KVStore is the optional key-value store where you
// KVStore is the MANDATORY key-value store where you
// want us to write statistics about which resolver is
// working better in your network. If this field is
// not set, then we'll use a in-memory store.
// working better in your network.
KVStore KVStore
// Logger is the optional logger you want us to use

View file

@ -6,11 +6,12 @@ import (
"net"
"net/url"
"strings"
"sync/atomic"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/ooni/probe-cli/v3/internal/engine/internal/multierror"
"github.com/ooni/probe-cli/v3/internal/atomicx"
"github.com/ooni/probe-cli/v3/internal/kvstore"
"github.com/ooni/probe-cli/v3/internal/multierror"
)
func TestNetworkWorks(t *testing.T) {
@ -30,7 +31,7 @@ func TestAddressWorks(t *testing.T) {
func TestTypicalUsageWithFailure(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel() // fail immediately
reso := &Resolver{}
reso := &Resolver{KVStore: &kvstore.Memory{}}
addrs, err := reso.LookupHost(ctx, "ooni.org")
if !errors.Is(err, ErrLookupHost) {
t.Fatal("not the error we expected", err)
@ -82,6 +83,7 @@ func TestTypicalUsageWithSuccess(t *testing.T) {
expected := []string{"8.8.8.8", "8.8.4.4"}
ctx := context.Background()
reso := &Resolver{
KVStore: &kvstore.Memory{},
dnsClientMaker: &fakeDNSClientMaker{
reso: &FakeResolver{Data: expected},
},
@ -252,7 +254,7 @@ func TestMaybeConfusionManyEntries(t *testing.T) {
func TestResolverWorksWithProxy(t *testing.T) {
var (
works int32
works = &atomicx.Int64{}
startuperr = make(chan error)
listench = make(chan net.Listener)
done = make(chan interface{})
@ -273,7 +275,7 @@ func TestResolverWorksWithProxy(t *testing.T) {
// shutdown by the main goroutine.
return
}
atomic.AddInt32(&works, 1)
works.Add(1)
conn.Close()
}
}()
@ -283,10 +285,13 @@ func TestResolverWorksWithProxy(t *testing.T) {
}
listener := <-listench
// use the proxy
reso := &Resolver{ProxyURL: &url.URL{
Scheme: "socks5",
Host: listener.Addr().String(),
}}
reso := &Resolver{
ProxyURL: &url.URL{
Scheme: "socks5",
Host: listener.Addr().String(),
},
KVStore: &kvstore.Memory{},
}
ctx := context.Background()
addrs, err := reso.LookupHost(ctx, "ooni.org")
// cleanly shutdown the listener
@ -299,7 +304,7 @@ func TestResolverWorksWithProxy(t *testing.T) {
if addrs != nil {
t.Fatal("expected nil addrs")
}
if works < 1 {
if works.Load() < 1 {
t.Fatal("expected to see a positive number of entries here")
}
}

View file

@ -18,9 +18,15 @@ type resolverinfo struct {
Score float64
}
// ErrNilKVStore indicates that the KVStore is nil.
var ErrNilKVStore = errors.New("sessionresolver: kvstore is nil")
// readstate reads the resolver state from disk
func (r *Resolver) readstate() ([]*resolverinfo, error) {
data, err := r.kvstore().Get(storekey)
if r.KVStore == nil {
return nil, ErrNilKVStore
}
data, err := r.KVStore.Get(storekey)
if err != nil {
return nil, err
}
@ -85,9 +91,12 @@ func (r *Resolver) readstatedefault() []*resolverinfo {
// writestate writes the state on the kvstore.
func (r *Resolver) writestate(ri []*resolverinfo) error {
if r.KVStore == nil {
return ErrNilKVStore
}
data, err := r.getCodec().Encode(ri)
if err != nil {
return err
}
return r.kvstore().Set(storekey, data)
return r.KVStore.Set(storekey, data)
}

View file

@ -3,12 +3,25 @@ package sessionresolver
import (
"errors"
"testing"
"github.com/ooni/probe-cli/v3/internal/kvstore"
)
func TestReadStateNothingInKVStore(t *testing.T) {
reso := &Resolver{KVStore: &memkvstore{}}
func TestReadStateNoKVStore(t *testing.T) {
reso := &Resolver{}
out, err := reso.readstate()
if !errors.Is(err, errMemkvstoreNotFound) {
if !errors.Is(err, ErrNilKVStore) {
t.Fatal("not the error we expected", err)
}
if out != nil {
t.Fatal("expected nil here")
}
}
func TestReadStateNothingInKVStore(t *testing.T) {
reso := &Resolver{KVStore: &kvstore.Memory{}}
out, err := reso.readstate()
if !errors.Is(err, kvstore.ErrNoSuchKey) {
t.Fatal("not the error we expected", err)
}
if out != nil {
@ -19,7 +32,7 @@ func TestReadStateNothingInKVStore(t *testing.T) {
func TestReadStateDecodeError(t *testing.T) {
errMocked := errors.New("mocked error")
reso := &Resolver{
KVStore: &memkvstore{},
KVStore: &kvstore.Memory{},
codec: &FakeCodec{DecodeErr: errMocked},
}
if err := reso.KVStore.Set(storekey, []byte(`[]`)); err != nil {
@ -35,9 +48,9 @@ func TestReadStateDecodeError(t *testing.T) {
}
func TestReadStateAndPruneReadStateError(t *testing.T) {
reso := &Resolver{KVStore: &memkvstore{}}
reso := &Resolver{KVStore: &kvstore.Memory{}}
out, err := reso.readstateandprune()
if !errors.Is(err, errMemkvstoreNotFound) {
if !errors.Is(err, kvstore.ErrNoSuchKey) {
t.Fatal("not the error we expected", err)
}
if out != nil {
@ -46,7 +59,7 @@ func TestReadStateAndPruneReadStateError(t *testing.T) {
}
func TestReadStateAndPruneWithUnsupportedEntries(t *testing.T) {
reso := &Resolver{KVStore: &memkvstore{}}
reso := &Resolver{KVStore: &kvstore.Memory{}}
var in []*resolverinfo
in = append(in, &resolverinfo{})
if err := reso.writestate(in); err != nil {
@ -62,7 +75,7 @@ func TestReadStateAndPruneWithUnsupportedEntries(t *testing.T) {
}
func TestReadStateDefaultWithMissingEntries(t *testing.T) {
reso := &Resolver{KVStore: &memkvstore{}}
reso := &Resolver{KVStore: &kvstore.Memory{}}
// let us simulate that we have just one entry here
existingURL := "https://dns.google/dns-query"
existingScore := 0.88
@ -100,12 +113,27 @@ func TestReadStateDefaultWithMissingEntries(t *testing.T) {
}
}
func TestWriteStateNoKVStore(t *testing.T) {
reso := &Resolver{}
existingURL := "https://dns.google/dns-query"
existingScore := 0.88
var in []*resolverinfo
in = append(in, &resolverinfo{
URL: existingURL,
Score: existingScore,
})
if err := reso.writestate(in); !errors.Is(err, ErrNilKVStore) {
t.Fatal("not the error we expected", err)
}
}
func TestWriteStateCannotSerialize(t *testing.T) {
errMocked := errors.New("mocked error")
reso := &Resolver{
codec: &FakeCodec{
EncodeErr: errMocked,
},
KVStore: &kvstore.Memory{},
}
existingURL := "https://dns.google/dns-query"
existingScore := 0.88