0f61778e7a
* poc: mobile api for running WebConnectivity Yesterday we had a team meeting where we discussed the importance of stopping using MK wrappers for running experiments. Here's a PoC showing how we can do that for WebConnectivity. It seems to me we probably want to add more tests and merge this code such that we can experiment with it quite soon. There seems to be opportunities for auto-generating code, BTW. While working on this PoC, I've also took a chance to revamp the external documentation of pkg/oonimkall. * feat: start making the code more abstract * chore: write unit tests for new code * fix(oonimkall): improve naming * refactor: cosmetic changes * fix: explain
94 lines
2.6 KiB
Go
94 lines
2.6 KiB
Go
package oonimkall
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
"sync/atomic"
|
|
|
|
"github.com/ooni/probe-cli/v3/internal/engine/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 int32
|
|
LookupBackendsErr error
|
|
LookupLocationErr error
|
|
NewExperimentBuilderErr error
|
|
UnlockCount int32
|
|
}
|
|
|
|
// lock implements experimentSession.lock
|
|
func (sess *FakeExperimentSession) lock() {
|
|
atomic.AddInt32(&sess.LockCount, 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() {
|
|
atomic.AddInt32(&sess.UnlockCount, 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
|
|
}
|