Skip to content

Commit

Permalink
x
Browse files Browse the repository at this point in the history
  • Loading branch information
bassosimone committed Jun 7, 2024
1 parent 8b710f6 commit c4f28d2
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 24 deletions.
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
7 changes: 1 addition & 6 deletions internal/registry/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,10 @@ import (
func init() {
const canonicalName = "example"
AllExperiments[canonicalName] = func() *Factory {
// TODO(bassosimone,DecFox): as pointed out by @ainghazal, this experiment
// should be the one that people modify to start out new experiments, so it's
// kind of suboptimal that it has a constructor with explicit experiment
// name to ease writing some tests that ./pkg/oonimkall needs given that no
// other experiment ever sets the experiment name externally!
return &Factory{
build: func(config interface{}) model.ExperimentMeasurer {
return example.NewExperimentMeasurer(
*config.(*example.Config), "example",
*config.(*example.Config),
)
},
canonicalName: canonicalName,
Expand Down
4 changes: 1 addition & 3 deletions pkg/oonimkall/taskmocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ package oonimkall
// we only use mocks when testing, this file is a `_test.go` file.
//

import (
"sync"
)
import "sync"

// CollectorTaskEmitter is a thread-safe taskEmitter
// that stores all the events inside itself.
Expand Down
4 changes: 0 additions & 4 deletions pkg/oonimkall/taskrunner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ func (dep *MockableTaskRunnerDependencies) NewSession(ctx context.Context, confi
}

func TestTaskRunnerRun(t *testing.T) {
if testing.Short() {
t.Skip("skip test in short mode")
}

// newRunnerForTesting is a factory for creating a new
// runner that wraps newRunner and also sets a specific
// taskSessionBuilder for testing purposes.
Expand Down

0 comments on commit c4f28d2

Please sign in to comment.