ooni-probe-cli/pkg/oonimkall/experiment_test.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

98 lines
2.7 KiB
Go

package oonimkall
import (
"context"
"sync"
"github.com/ooni/probe-cli/v3/internal/atomicx"
"github.com/ooni/probe-cli/v3/internal/model"
)
// FakeExperimentCallbacks contains fake ExperimentCallbacks.
type FakeExperimentCallbacks struct{}
// OnProgress implements ExperimentCallbacks.OnProgress
func (cb *FakeExperimentCallbacks) OnProgress(percentage float64, message string) {}
// FakeExperimentSession is a fake experimentSession
type FakeExperimentSession struct {
ExperimentBuilder experimentBuilder
LockCount *atomicx.Int64
LookupBackendsErr error
LookupLocationErr error
NewExperimentBuilderErr error
UnlockCount *atomicx.Int64
}
// lock implements experimentSession.lock
func (sess *FakeExperimentSession) lock() {
if sess.LockCount != nil {
sess.LockCount.Add(1)
}
}
// maybeLookupBackends implements experimentSession.maybeLookupBackends
func (sess *FakeExperimentSession) maybeLookupBackends(ctx context.Context) error {
return sess.LookupBackendsErr
}
// maybeLookupLocation implements experimentSession.maybeLookupLocation
func (sess *FakeExperimentSession) maybeLookupLocation(ctx context.Context) error {
return sess.LookupLocationErr
}
// newExperimentBuilder implements experimentSession.newExperimentBuilder
func (sess *FakeExperimentSession) newExperimentBuilder(name string) (experimentBuilder, error) {
return sess.ExperimentBuilder, sess.NewExperimentBuilderErr
}
// unlock implements experimentSession.unlock
func (sess *FakeExperimentSession) unlock() {
if sess.UnlockCount != nil {
sess.UnlockCount.Add(1)
}
}
// FakeExperimentBuilder is a fake experimentBuilder
type FakeExperimentBuilder struct {
Callbacks ExperimentCallbacks
Experiment experiment
mu sync.Mutex
}
// newExperiment implements experimentBuilder.newExperiment
func (eb *FakeExperimentBuilder) newExperiment() experiment {
return eb.Experiment
}
// setCallbacks implements experimentBuilder.setCallbacks
func (eb *FakeExperimentBuilder) setCallbacks(cb ExperimentCallbacks) {
defer eb.mu.Unlock()
eb.mu.Lock()
eb.Callbacks = cb
}
// FakeExperiment is a fake experiment
type FakeExperiment struct {
Err error
Measurement *model.Measurement
Received float64
Sent float64
}
// MeasureWithContext implements experiment.MeasureWithContext.
func (e *FakeExperiment) MeasureWithContext(ctx context.Context, input string) (
measurement *model.Measurement, err error) {
return e.Measurement, e.Err
}
// KibiBytesSent implements experiment.KibiBytesSent
func (e *FakeExperiment) KibiBytesSent() float64 {
return e.Sent
}
// KibiBytesReceived implements experiment.KibiBytesReceived
func (e *FakeExperiment) KibiBytesReceived() float64 {
return e.Received
}