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:
parent
0115d6c470
commit
e0b0dfedc1
7 changed files with 441 additions and 69 deletions
|
|
@ -3,6 +3,7 @@ package engine
|
|||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
|
|
@ -10,17 +11,34 @@ import (
|
|||
"os"
|
||||
"syscall"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/apex/log"
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/geolocate"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/model"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/netx"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/probeservices"
|
||||
"github.com/ooni/probe-cli/v3/internal/version"
|
||||
)
|
||||
|
||||
func TestSessionByteCounter(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skip test in short mode")
|
||||
}
|
||||
s := newSessionForTesting(t)
|
||||
client := s.DefaultHTTPClient()
|
||||
resp, err := client.Get("https://www.google.com")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if _, err := io.Copy(ioutil.Discard, resp.Body); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if s.KibiBytesSent() <= 0 || s.KibiBytesReceived() <= 0 {
|
||||
t.Fatal("byte counter is not working")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewSessionBuilderChecks(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skip test in short mode")
|
||||
|
|
@ -307,6 +325,21 @@ func TestSessionLocationLookup(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestSessionCheckInWithRealAPI(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skip test in short mode")
|
||||
}
|
||||
sess := newSessionForTesting(t)
|
||||
defer sess.Close()
|
||||
results, err := sess.CheckIn(context.Background(), &model.CheckInConfig{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if results == nil {
|
||||
t.Fatal("expected non nil results here")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSessionCloseCancelsTempDir(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skip test in short mode")
|
||||
|
|
@ -584,50 +617,31 @@ func TestNewOrchestraClientMaybeLookupBackendsFailure(t *testing.T) {
|
|||
if testing.Short() {
|
||||
t.Skip("skip test in short mode")
|
||||
}
|
||||
errMocked := errors.New("mocked error")
|
||||
sess := newSessionForTestingNoLookups(t)
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel() // fail immediately
|
||||
client, err := sess.NewOrchestraClient(ctx)
|
||||
if !errors.Is(err, ErrAllProbeServicesFailed) {
|
||||
t.Fatal("not the error we expected")
|
||||
sess.testMaybeLookupBackendsContext = func(ctx context.Context) error {
|
||||
return errMocked
|
||||
}
|
||||
client, err := sess.NewOrchestraClient(context.Background())
|
||||
if !errors.Is(err, errMocked) {
|
||||
t.Fatal("not the error we expected", err)
|
||||
}
|
||||
if client != nil {
|
||||
t.Fatal("expected nil client here")
|
||||
}
|
||||
}
|
||||
|
||||
type httpTransportThatSleeps struct {
|
||||
txp netx.HTTPRoundTripper
|
||||
st time.Duration
|
||||
}
|
||||
|
||||
func (txp httpTransportThatSleeps) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
resp, err := txp.txp.RoundTrip(req)
|
||||
time.Sleep(txp.st)
|
||||
return resp, err
|
||||
}
|
||||
|
||||
func (txp httpTransportThatSleeps) CloseIdleConnections() {
|
||||
txp.txp.CloseIdleConnections()
|
||||
}
|
||||
|
||||
func TestNewOrchestraClientMaybeLookupLocationFailure(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skip test in short mode")
|
||||
}
|
||||
errMocked := errors.New("mocked error")
|
||||
sess := newSessionForTestingNoLookups(t)
|
||||
sess.httpDefaultTransport = httpTransportThatSleeps{
|
||||
txp: sess.httpDefaultTransport,
|
||||
st: 5 * time.Second,
|
||||
sess.testMaybeLookupLocationContext = func(ctx context.Context) error {
|
||||
return errMocked
|
||||
}
|
||||
// The transport sleeps for five seconds, so the context should be expired by
|
||||
// the time in which we attempt at looking up the location. Because the
|
||||
// implementation performs the round-trip and _then_ sleeps, it means we'll
|
||||
// see the context expired error when performing the location lookup.
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 4*time.Second)
|
||||
defer cancel()
|
||||
client, err := sess.NewOrchestraClient(ctx)
|
||||
if !errors.Is(err, geolocate.ErrAllIPLookuppersFailed) {
|
||||
client, err := sess.NewOrchestraClient(context.Background())
|
||||
if !errors.Is(err, errMocked) {
|
||||
t.Fatalf("not the error we expected: %+v", err)
|
||||
}
|
||||
if client != nil {
|
||||
|
|
@ -651,3 +665,14 @@ func TestNewOrchestraClientProbeServicesNewClientFailure(t *testing.T) {
|
|||
t.Fatal("expected nil client here")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSessionNewSubmitterReturnsNonNilSubmitter(t *testing.T) {
|
||||
sess := newSessionForTesting(t)
|
||||
subm, err := sess.NewSubmitter(context.Background())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if subm == nil {
|
||||
t.Fatal("expected non nil submitter here")
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue