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
parent 2a7fdcd810
commit 33de701263
169 changed files with 1137 additions and 1004 deletions
+9 -5
View File
@@ -3,8 +3,8 @@ package oonimkall
import (
"context"
"sync"
"sync/atomic"
"github.com/ooni/probe-cli/v3/internal/atomicx"
"github.com/ooni/probe-cli/v3/internal/engine/model"
)
@@ -17,16 +17,18 @@ func (cb *FakeExperimentCallbacks) OnProgress(percentage float64, message string
// FakeExperimentSession is a fake experimentSession
type FakeExperimentSession struct {
ExperimentBuilder experimentBuilder
LockCount int32
LockCount *atomicx.Int64
LookupBackendsErr error
LookupLocationErr error
NewExperimentBuilderErr error
UnlockCount int32
UnlockCount *atomicx.Int64
}
// lock implements experimentSession.lock
func (sess *FakeExperimentSession) lock() {
atomic.AddInt32(&sess.LockCount, 1)
if sess.LockCount != nil {
sess.LockCount.Add(1)
}
}
// maybeLookupBackends implements experimentSession.maybeLookupBackends
@@ -46,7 +48,9 @@ func (sess *FakeExperimentSession) newExperimentBuilder(name string) (experiment
// unlock implements experimentSession.unlock
func (sess *FakeExperimentSession) unlock() {
atomic.AddInt32(&sess.UnlockCount, 1)
if sess.UnlockCount != nil {
sess.UnlockCount.Add(1)
}
}
// FakeExperimentBuilder is a fake experimentBuilder
+4 -3
View File
@@ -8,9 +8,10 @@ import (
"net/url"
"time"
engine "github.com/ooni/probe-cli/v3/internal/engine"
"github.com/ooni/probe-cli/v3/internal/engine"
"github.com/ooni/probe-cli/v3/internal/engine/model"
"github.com/ooni/probe-cli/v3/internal/engine/runtimex"
"github.com/ooni/probe-cli/v3/internal/kvstore"
"github.com/ooni/probe-cli/v3/internal/runtimex"
)
const (
@@ -71,7 +72,7 @@ func (r *Runner) hasUnsupportedSettings(logger *ChanLogger) bool {
}
func (r *Runner) newsession(ctx context.Context, logger *ChanLogger) (*engine.Session, error) {
kvstore, err := engine.NewFileSystemKVStore(r.settings.StateDir)
kvstore, err := kvstore.NewFS(r.settings.StateDir)
if err != nil {
return nil, err
}
+6 -5
View File
@@ -8,12 +8,13 @@ import (
"runtime"
"sync"
"github.com/ooni/probe-cli/v3/internal/atomicx"
"github.com/ooni/probe-cli/v3/internal/engine"
"github.com/ooni/probe-cli/v3/internal/engine/atomicx"
"github.com/ooni/probe-cli/v3/internal/engine/legacy/assetsdir"
"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/engine/runtimex"
"github.com/ooni/probe-cli/v3/internal/kvstore"
"github.com/ooni/probe-cli/v3/internal/runtimex"
)
// AtomicInt64 allows us to export atomicx.Int64 variables to
@@ -25,8 +26,8 @@ type AtomicInt64 struct {
// These two variables contain metrics pertaining to the number
// of Sessions and Contexts that are currently being used.
var (
ActiveSessions = &AtomicInt64{atomicx.NewInt64()}
ActiveContexts = &AtomicInt64{atomicx.NewInt64()}
ActiveSessions = &AtomicInt64{&atomicx.Int64{}}
ActiveContexts = &AtomicInt64{&atomicx.Int64{}}
)
// Logger is the logger used by a Session. You should implement a class
@@ -147,7 +148,7 @@ func NewSessionWithContext(ctx *Context, config *SessionConfig) (*Session, error
// newSessionWithContext implements NewSessionWithContext.
func newSessionWithContext(ctx context.Context, config *SessionConfig) (*Session, error) {
kvstore, err := engine.NewFileSystemKVStore(config.StateDir)
kvstore, err := kvstore.NewFS(config.StateDir)
if err != nil {
return nil, err
}
+6 -6
View File
@@ -4,7 +4,7 @@ import (
"testing"
"time"
"github.com/ooni/probe-cli/v3/internal/engine/atomicx"
"github.com/ooni/probe-cli/v3/internal/atomicx"
)
func TestClampTimeout(t *testing.T) {
@@ -26,7 +26,7 @@ func TestClampTimeout(t *testing.T) {
}
func TestNewContextWithZeroTimeout(t *testing.T) {
here := atomicx.NewInt64()
here := &atomicx.Int64{}
ctx, cancel := newContext(0)
defer cancel()
go func() {
@@ -41,7 +41,7 @@ func TestNewContextWithZeroTimeout(t *testing.T) {
}
func TestNewContextWithNegativeTimeout(t *testing.T) {
here := atomicx.NewInt64()
here := &atomicx.Int64{}
ctx, cancel := newContext(-1)
defer cancel()
go func() {
@@ -56,7 +56,7 @@ func TestNewContextWithNegativeTimeout(t *testing.T) {
}
func TestNewContextWithHugeTimeout(t *testing.T) {
here := atomicx.NewInt64()
here := &atomicx.Int64{}
ctx, cancel := newContext(maxTimeout + 1)
defer cancel()
go func() {
@@ -71,7 +71,7 @@ func TestNewContextWithHugeTimeout(t *testing.T) {
}
func TestNewContextWithReasonableTimeout(t *testing.T) {
here := atomicx.NewInt64()
here := &atomicx.Int64{}
ctx, cancel := newContext(1)
defer cancel()
go func() {
@@ -86,7 +86,7 @@ func TestNewContextWithReasonableTimeout(t *testing.T) {
}
func TestNewContextWithArtificiallyLowMaxTimeout(t *testing.T) {
here := atomicx.NewInt64()
here := &atomicx.Int64{}
const maxTimeout = 2
ctx, cancel := newContextEx(maxTimeout+1, maxTimeout)
defer cancel()
+4 -4
View File
@@ -43,8 +43,8 @@ import (
"context"
"encoding/json"
"github.com/ooni/probe-cli/v3/internal/engine/atomicx"
"github.com/ooni/probe-cli/v3/internal/engine/runtimex"
"github.com/ooni/probe-cli/v3/internal/atomicx"
"github.com/ooni/probe-cli/v3/internal/runtimex"
"github.com/ooni/probe-cli/v3/pkg/oonimkall/internal/tasks"
)
@@ -77,8 +77,8 @@ func StartTask(input string) (*Task, error) {
ctx, cancel := context.WithCancel(context.Background())
task := &Task{
cancel: cancel,
isdone: atomicx.NewInt64(),
isstopped: atomicx.NewInt64(),
isdone: &atomicx.Int64{},
isstopped: &atomicx.Int64{},
out: make(chan *tasks.Event, bufsiz),
}
go func() {
+1 -1
View File
@@ -4,7 +4,7 @@ import (
"context"
"encoding/json"
"github.com/ooni/probe-cli/v3/internal/engine/runtimex"
"github.com/ooni/probe-cli/v3/internal/runtimex"
)
// WebConnectivityConfig contains settings for WebConnectivity.
+31 -10
View File
@@ -7,12 +7,17 @@ import (
"testing"
"github.com/google/go-cmp/cmp"
"github.com/ooni/probe-cli/v3/internal/atomicx"
"github.com/ooni/probe-cli/v3/internal/engine/model"
)
func TestWebConnectivityRunnerWithMaybeLookupBackendsFailure(t *testing.T) {
errMocked := errors.New("mocked error")
sess := &FakeExperimentSession{LookupBackendsErr: errMocked}
sess := &FakeExperimentSession{
LockCount: &atomicx.Int64{},
LookupBackendsErr: errMocked,
UnlockCount: &atomicx.Int64{},
}
runner := &webConnectivityRunner{sess: sess}
ctx := context.Background()
config := &WebConnectivityConfig{Input: "https://ooni.org"}
@@ -23,14 +28,18 @@ func TestWebConnectivityRunnerWithMaybeLookupBackendsFailure(t *testing.T) {
if out != nil {
t.Fatal("expected nil here")
}
if sess.LockCount != 1 || sess.UnlockCount != 1 {
if sess.LockCount.Load() != 1 || sess.UnlockCount.Load() != 1 {
t.Fatal("invalid locking pattern")
}
}
func TestWebConnectivityRunnerWithMaybeLookupLocationFailure(t *testing.T) {
errMocked := errors.New("mocked error")
sess := &FakeExperimentSession{LookupLocationErr: errMocked}
sess := &FakeExperimentSession{
LockCount: &atomicx.Int64{},
LookupLocationErr: errMocked,
UnlockCount: &atomicx.Int64{},
}
runner := &webConnectivityRunner{sess: sess}
ctx := context.Background()
config := &WebConnectivityConfig{Input: "https://ooni.org"}
@@ -41,14 +50,18 @@ func TestWebConnectivityRunnerWithMaybeLookupLocationFailure(t *testing.T) {
if out != nil {
t.Fatal("expected nil here")
}
if sess.LockCount != 1 || sess.UnlockCount != 1 {
if sess.LockCount.Load() != 1 || sess.UnlockCount.Load() != 1 {
t.Fatal("invalid locking pattern")
}
}
func TestWebConnectivityRunnerWithNewExperimentBuilderFailure(t *testing.T) {
errMocked := errors.New("mocked error")
sess := &FakeExperimentSession{NewExperimentBuilderErr: errMocked}
sess := &FakeExperimentSession{
LockCount: &atomicx.Int64{},
NewExperimentBuilderErr: errMocked,
UnlockCount: &atomicx.Int64{},
}
runner := &webConnectivityRunner{sess: sess}
ctx := context.Background()
config := &WebConnectivityConfig{Input: "https://ooni.org"}
@@ -59,7 +72,7 @@ func TestWebConnectivityRunnerWithNewExperimentBuilderFailure(t *testing.T) {
if out != nil {
t.Fatal("expected nil here")
}
if sess.LockCount != 1 || sess.UnlockCount != 1 {
if sess.LockCount.Load() != 1 || sess.UnlockCount.Load() != 1 {
t.Fatal("invalid locking pattern")
}
}
@@ -69,7 +82,11 @@ func TestWebConnectivityRunnerWithMeasureFailure(t *testing.T) {
cbs := &FakeExperimentCallbacks{}
e := &FakeExperiment{Err: errMocked}
eb := &FakeExperimentBuilder{Experiment: e}
sess := &FakeExperimentSession{ExperimentBuilder: eb}
sess := &FakeExperimentSession{
LockCount: &atomicx.Int64{},
ExperimentBuilder: eb,
UnlockCount: &atomicx.Int64{},
}
runner := &webConnectivityRunner{sess: sess}
ctx := context.Background()
config := &WebConnectivityConfig{
@@ -83,7 +100,7 @@ func TestWebConnectivityRunnerWithMeasureFailure(t *testing.T) {
if out != nil {
t.Fatal("expected nil here")
}
if sess.LockCount != 1 || sess.UnlockCount != 1 {
if sess.LockCount.Load() != 1 || sess.UnlockCount.Load() != 1 {
t.Fatal("invalid locking pattern")
}
if eb.Callbacks != cbs {
@@ -99,7 +116,11 @@ func TestWebConnectivityRunnerWithNoError(t *testing.T) {
cbs := &FakeExperimentCallbacks{}
e := &FakeExperiment{Measurement: m, Sent: 10, Received: 128}
eb := &FakeExperimentBuilder{Experiment: e}
sess := &FakeExperimentSession{ExperimentBuilder: eb}
sess := &FakeExperimentSession{
LockCount: &atomicx.Int64{},
ExperimentBuilder: eb,
UnlockCount: &atomicx.Int64{},
}
runner := &webConnectivityRunner{sess: sess}
ctx := context.Background()
config := &WebConnectivityConfig{
@@ -113,7 +134,7 @@ func TestWebConnectivityRunnerWithNoError(t *testing.T) {
if out == nil {
t.Fatal("expected non-nil here")
}
if sess.LockCount != 1 || sess.UnlockCount != 1 {
if sess.LockCount.Load() != 1 || sess.UnlockCount.Load() != 1 {
t.Fatal("invalid locking pattern")
}
if eb.Callbacks != cbs {