-
Notifications
You must be signed in to change notification settings - Fork 54
/
experiment_test.go
97 lines (80 loc) · 2.68 KB
/
experiment_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package oonimkall
import (
"context"
"sync"
"sync/atomic"
"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 *atomic.Int64
LookupBackendsErr error
LookupLocationErr error
NewExperimentBuilderErr error
UnlockCount *atomic.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, target model.ExperimentTarget) (
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
}