ooni-probe-cli/pkg/oonimkall/experiment.go
Simone Basso 273b70bacc
refactor: interfaces and data types into the model package (#642)
## Checklist

- [x] I have read the [contribution guidelines](https://github.com/ooni/probe-cli/blob/master/CONTRIBUTING.md)
- [x] reference issue for this pull request: https://github.com/ooni/probe/issues/1885
- [x] related ooni/spec pull request: N/A

Location of the issue tracker: https://github.com/ooni/probe

## Description

This PR contains a set of changes to move important interfaces and data types into the `./internal/model` package.

The criteria for including an interface or data type in here is roughly that the type should be important and used by several packages. We are especially interested to move more interfaces here to increase modularity.

An additional side effect is that, by reading this package, one should be able to understand more quickly how different parts of the codebase interact with each other.

This is what I want to move in `internal/model`:

- [x] most important interfaces from `internal/netxlite`
- [x] everything that was previously part of `internal/engine/model`
- [x] mocks from `internal/netxlite/mocks` should also be moved in here as a subpackage
2022-01-03 13:53:23 +01:00

97 lines
2.8 KiB
Go

package oonimkall
import (
"context"
"github.com/ooni/probe-cli/v3/internal/engine"
"github.com/ooni/probe-cli/v3/internal/model"
)
// experimentSession is the abstract representation of
// a session according to an experiment.
type experimentSession interface {
// lock locks the session
lock()
// maybeLookupBackends lookups the backends
maybeLookupBackends(ctx context.Context) error
// maybeLookupLocations lookups the probe location
maybeLookupLocation(ctx context.Context) error
// newExperimentBuilder creates a new experiment builder
newExperimentBuilder(name string) (experimentBuilder, error)
// unlock unlocks the session
unlock()
}
// lock implements experimentSession.lock
func (sess *Session) lock() {
sess.mtx.Lock()
}
// maybeLookupBackends implements experimentSession.maybeLookupBackends
func (sess *Session) maybeLookupBackends(ctx context.Context) error {
return sess.sessp.MaybeLookupBackendsContext(ctx)
}
// maybeLookupLocation implements experimentSession.maybeLookupLocation
func (sess *Session) maybeLookupLocation(ctx context.Context) error {
return sess.sessp.MaybeLookupLocationContext(ctx)
}
// newExperimentBuilder implements experimentSession.newExperimentBuilder
func (sess *Session) newExperimentBuilder(name string) (experimentBuilder, error) {
eb, err := sess.sessp.NewExperimentBuilder(name)
if err != nil {
return nil, err
}
return &experimentBuilderWrapper{eb: eb}, nil
}
// unlock implements experimentSession.unlock
func (sess *Session) unlock() {
sess.mtx.Unlock()
}
// experimentBuilder is the representation of an experiment
// builder that we use inside this package.
type experimentBuilder interface {
// newExperiment creates a new experiment instance
newExperiment() experiment
// setCallbacks sets the experiment callbacks
setCallbacks(ExperimentCallbacks)
}
// experimentBuilderWrapper wraps *ExperimentBuilder
type experimentBuilderWrapper struct {
eb *engine.ExperimentBuilder
}
// newExperiment implements experimentBuilder.newExperiment
func (eb *experimentBuilderWrapper) newExperiment() experiment {
return eb.eb.NewExperiment()
}
// setCallbacks implements experimentBuilder.setCallbacks
func (eb *experimentBuilderWrapper) setCallbacks(cb ExperimentCallbacks) {
eb.eb.SetCallbacks(cb)
}
// experiment is the representation of an experiment that
// we use inside this package for running nettests.
type experiment interface {
// MeasureWithContext runs the measurement with the given input
// and context. It returns a measurement or an error.
MeasureWithContext(ctx context.Context, input string) (
measurement *model.Measurement, err error)
// KibiBytesSent returns the number of KiB sent.
KibiBytesSent() float64
// KibiBytesReceived returns the number of KiB received.
KibiBytesReceived() float64
}