feat(session): expose CheckIn method (#266)

* feat(session): expose CheckIn method

It seems to me the right thing to do is to query the CheckIn API
from the Session rather than querying it from InputLoader.

Then, InputLoader could just take a reference to a Session-like
interface that allows this functionality.

So, this diff exposes the Session.CheckIn method.

Doing that, in turn, required some refactoring to allow for
more and better unit tests.

While doing that, I also noticed that Session required a mutex
to be a well-behaving type, so I did that.

While doing that, I also tried to cover all the lines in session.go
and, as part of that, I have removed unused code.

Reference issue: https://github.com/ooni/probe/issues/1299.

* fix: reinstate comment I shan't have removed

* fix: repair broken test

* fix: a bit more coverage, annotations, etc.

* Update internal/engine/session.go

* Update internal/engine/session_integration_test.go

* Update internal/engine/session_internal_test.go
This commit is contained in:
Simone Basso
2021-03-29 15:04:41 +02:00
committed by GitHub
parent 0115d6c470
commit e0b0dfedc1
7 changed files with 440 additions and 68 deletions
+2 -2
View File
@@ -479,17 +479,17 @@ func (sess *Session) FetchURLList(ctx *Context, config *URLListConfig) (*URLList
if config.CountryCode == "" {
config.CountryCode = "XX"
info, err := sess.sessp.LookupLocationContext(ctx.ctx)
// TODO(bassosimone): this piece of code feels wrong to me. We don't
// want to continue if we cannot discover the country.
if err == nil && info != nil {
config.CountryCode = info.CountryCode
}
}
cfg := model.URLListConfig{
Categories: config.Categories,
CountryCode: config.CountryCode,
Limit: config.Limit,
}
result, err := psc.FetchURLList(ctx.ctx, cfg)
if err != nil {
return nil, err
+9 -3
View File
@@ -12,7 +12,6 @@ import (
"testing"
"time"
engine "github.com/ooni/probe-cli/v3/internal/engine"
"github.com/ooni/probe-cli/v3/internal/engine/geolocate"
"github.com/ooni/probe-cli/v3/internal/engine/model"
"github.com/ooni/probe-cli/v3/pkg/oonimkall"
@@ -362,7 +361,7 @@ func TestCheckInNewProbeServicesFailure(t *testing.T) {
config.WebConnectivity.Add("NEWS")
config.WebConnectivity.Add("CULTR")
result, err := sess.CheckIn(ctx, &config)
if !errors.Is(err, engine.ErrAllProbeServicesFailed) {
if !errors.Is(err, context.Canceled) {
t.Fatalf("not the error we expected: %+v", err)
}
if result != nil {
@@ -440,11 +439,18 @@ func TestFetchURLListSuccess(t *testing.T) {
if result == nil || result.Results == nil {
t.Fatal("got nil result")
}
for _, entry := range result.Results {
for idx := int64(0); idx < result.Size(); idx++ {
entry := result.At(idx)
if entry.CategoryCode != "NEWS" && entry.CategoryCode != "CULTR" {
t.Fatalf("unexpected category code: %+v", entry)
}
}
if result.At(-1) != nil {
t.Fatal("expected nil here")
}
if result.At(result.Size()) != nil {
t.Fatal("expected nil here")
}
}
func TestFetchURLListWithCC(t *testing.T) {
+3 -3
View File
@@ -58,10 +58,10 @@ func TestGood(t *testing.T) {
if err := json.Unmarshal([]byte(eventstr), &event); err != nil {
t.Fatal(err)
}
if event.Key != "task_terminated" {
t.Fatalf("unexpected event.Key: %s", event.Key)
if event.Key == "task_terminated" {
break
}
break
t.Fatalf("unexpected event.Key: %s", event.Key)
}
}