Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleanup(pkg/oonimkall): simplify test code #1619

Merged
merged 22 commits into from
Jun 7, 2024
Merged
13 changes: 7 additions & 6 deletions internal/experiment/example/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (

const testVersion = "0.1.0"

const testName = "example"

// Config contains the experiment config.
//
// This contains all the settings that user can set to modify the behaviour
Expand All @@ -22,7 +24,7 @@ const testVersion = "0.1.0"
type Config struct {
Message string `ooni:"Message to emit at test completion"`
ReturnError bool `ooni:"Toogle to return a mocked error"`
SleepTime int64 `ooni:"Amount of time to sleep for"`
SleepTime int64 `ooni:"Amount of time to sleep for in nanosecond"`
}

// TestKeys contains the experiment's result.
Expand All @@ -38,13 +40,12 @@ type TestKeys struct {

// Measurer performs the measurement.
type Measurer struct {
config Config
testName string
config Config
}

// ExperimentName implements model.ExperimentMeasurer.ExperimentName.
func (m Measurer) ExperimentName() string {
return m.testName
return testName
}

// ExperimentVersion implements model.ExperimentMeasurer.ExperimentVersion.
Expand Down Expand Up @@ -81,6 +82,6 @@ func (m Measurer) Run(ctx context.Context, args *model.ExperimentArgs) error {
}

// NewExperimentMeasurer creates a new ExperimentMeasurer.
func NewExperimentMeasurer(config Config, testName string) model.ExperimentMeasurer {
return Measurer{config: config, testName: testName}
func NewExperimentMeasurer(config Config) model.ExperimentMeasurer {
return Measurer{config: config}
}
18 changes: 13 additions & 5 deletions internal/experiment/example/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,26 @@ import (

"github.com/apex/log"
"github.com/ooni/probe-cli/v3/internal/experiment/example"
"github.com/ooni/probe-cli/v3/internal/legacy/mockable"
"github.com/ooni/probe-cli/v3/internal/mocks"
"github.com/ooni/probe-cli/v3/internal/model"
)

func TestSuccess(t *testing.T) {
m := example.NewExperimentMeasurer(example.Config{
SleepTime: int64(2 * time.Millisecond),
}, "example")
})
if m.ExperimentName() != "example" {
t.Fatal("invalid ExperimentName")
}
if m.ExperimentVersion() != "0.1.0" {
t.Fatal("invalid ExperimentVersion")
}
ctx := context.Background()
sess := &mockable.Session{MockableLogger: log.Log}
sess := &mocks.Session{
MockLogger: func() model.Logger {
return log.Log
},
}
callbacks := model.NewPrinterCallbacks(sess.Logger())
measurement := new(model.Measurement)
args := &model.ExperimentArgs{
Expand All @@ -41,9 +45,13 @@ func TestFailure(t *testing.T) {
m := example.NewExperimentMeasurer(example.Config{
SleepTime: int64(2 * time.Millisecond),
ReturnError: true,
}, "example")
})
ctx := context.Background()
sess := &mockable.Session{MockableLogger: log.Log}
sess := &mocks.Session{
MockLogger: func() model.Logger {
return log.Log
},
}
callbacks := model.NewPrinterCallbacks(sess.Logger())
args := &model.ExperimentArgs{
Callbacks: callbacks,
Expand Down
30 changes: 30 additions & 0 deletions internal/mocks/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ type Session struct {

MockCheckIn func(ctx context.Context,
config *model.OOAPICheckInConfig) (*model.OOAPICheckInResult, error)

MockClose func() error

MockMaybeLookupBackendsContext func(ctx context.Context) error

MockMaybeLookupLocationContext func(ctx context.Context) error

MockResolverASNString func() string

MockResolverNetworkName func() string
}

func (sess *Session) GetTestHelpersByName(name string) ([]model.OOAPIService, bool) {
Expand Down Expand Up @@ -159,3 +169,23 @@ func (sess *Session) CheckIn(ctx context.Context,
config *model.OOAPICheckInConfig) (*model.OOAPICheckInResult, error) {
return sess.MockCheckIn(ctx, config)
}

func (sess *Session) Close() error {
return sess.MockClose()
}

func (sess *Session) MaybeLookupBackendsContext(ctx context.Context) error {
return sess.MockMaybeLookupBackendsContext(ctx)
}

func (sess *Session) MaybeLookupLocationContext(ctx context.Context) error {
return sess.MockMaybeLookupLocationContext(ctx)
}

func (sess *Session) ResolverASNString() string {
return sess.MockResolverASNString()
}

func (sess *Session) ResolverNetworkName() string {
return sess.MockResolverNetworkName()
}
61 changes: 61 additions & 0 deletions internal/mocks/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,4 +354,65 @@ func TestSession(t *testing.T) {
t.Fatal("unexpected out")
}
})

t.Run("Close", func(t *testing.T) {
expected := errors.New("mocked err")
s := &Session{
MockClose: func() error {
return expected
},
}
err := s.Close()
if !errors.Is(err, expected) {
t.Fatal("unexpected err")
}
})

t.Run("MaybeLookupBackendsContext", func(t *testing.T) {
expected := errors.New("mocked err")
s := &Session{
MockMaybeLookupBackendsContext: func(ctx context.Context) error {
return expected
},
}
err := s.MaybeLookupBackendsContext(context.Background())
if !errors.Is(err, expected) {
t.Fatal("unexpected err")
}
})

t.Run("MaybeLookupLocationContext", func(t *testing.T) {
expected := errors.New("mocked err")
s := &Session{
MockMaybeLookupLocationContext: func(ctx context.Context) error {
return expected
},
}
err := s.MaybeLookupLocationContext(context.Background())
if !errors.Is(err, expected) {
t.Fatal("unexpected err")
}
})

t.Run("ResolverASNString", func(t *testing.T) {
s := &Session{
MockResolverASNString: func() string {
return "xx"
},
}
if s.ResolverASNString() != "xx" {
t.Fatal("unexpected result")
}
})

t.Run("ResolverNetworkName", func(t *testing.T) {
s := &Session{
MockResolverNetworkName: func() string {
return "xx"
},
}
if s.ResolverNetworkName() != "xx" {
t.Fatal("unexpected result")
}
})
}
2 changes: 1 addition & 1 deletion internal/registry/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func init() {
return &Factory{
build: func(config interface{}) model.ExperimentMeasurer {
return example.NewExperimentMeasurer(
*config.(*example.Config), "example",
*config.(*example.Config),
)
},
canonicalName: canonicalName,
Expand Down
Loading
Loading