forked from ooni/probe-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
submitter_test.go
93 lines (82 loc) · 2.05 KB
/
submitter_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
package engine
import (
"context"
"errors"
"testing"
"github.com/apex/log"
"github.com/ooni/probe-cli/v3/internal/atomicx"
"github.com/ooni/probe-cli/v3/internal/model"
)
func TestSubmitterNotEnabled(t *testing.T) {
ctx := context.Background()
submitter, err := NewSubmitter(ctx, SubmitterConfig{
Enabled: false,
})
if err != nil {
t.Fatal(err)
}
if _, ok := submitter.(stubSubmitter); !ok {
t.Fatal("we did not get a stubSubmitter instance")
}
m := new(model.Measurement)
if err := submitter.Submit(ctx, m); err != nil {
t.Fatal(err)
}
}
type FakeSubmitter struct {
Calls *atomicx.Int64
Error error
}
func (fs *FakeSubmitter) Submit(ctx context.Context, m *model.Measurement) error {
if fs.Calls != nil {
fs.Calls.Add(1)
}
return fs.Error
}
var _ Submitter = &FakeSubmitter{}
type FakeSubmitterSession struct {
Error error
Submitter Submitter
}
func (fse FakeSubmitterSession) NewSubmitter(ctx context.Context) (Submitter, error) {
return fse.Submitter, fse.Error
}
var _ SubmitterSession = FakeSubmitterSession{}
func TestNewSubmitterFails(t *testing.T) {
expected := errors.New("mocked error")
ctx := context.Background()
submitter, err := NewSubmitter(ctx, SubmitterConfig{
Enabled: true,
Session: FakeSubmitterSession{Error: expected},
})
if !errors.Is(err, expected) {
t.Fatalf("not the error we expected: %+v", err)
}
if submitter != nil {
t.Fatal("expected nil submitter here")
}
}
func TestNewSubmitterWithFailedSubmission(t *testing.T) {
expected := errors.New("mocked error")
ctx := context.Background()
fakeSubmitter := &FakeSubmitter{
Calls: &atomicx.Int64{},
Error: expected,
}
submitter, err := NewSubmitter(ctx, SubmitterConfig{
Enabled: true,
Logger: log.Log,
Session: FakeSubmitterSession{Submitter: fakeSubmitter},
})
if err != nil {
t.Fatal(err)
}
m := new(model.Measurement)
err = submitter.Submit(context.Background(), m)
if !errors.Is(err, expected) {
t.Fatalf("not the error we expected: %+v", err)
}
if fakeSubmitter.Calls.Load() != 1 {
t.Fatal("unexpected number of calls")
}
}