refactor: make measurex depend on measurexlite (#892)

This diff makes `measurex` depend on `measurexlite` rather than the other way around.

While there, add unit tests.

Closes https://github.com/ooni/probe/issues/2240
This commit is contained in:
Simone Basso
2022-08-28 21:41:58 +02:00
committed by GitHub
parent 7c1b2bbcb0
commit 8ca7645026
7 changed files with 312 additions and 71 deletions
+4 -10
View File
@@ -1,12 +1,6 @@
package measurex
// NewFailure creates a serializable failure from an error. We
// cannot round trip an error using JSON, so we serialize to this
// intermediate format that is a sort of Optional<string>.
func NewFailure(err error) *string {
if err == nil {
return nil
}
s := err.Error()
return &s
}
import "github.com/ooni/probe-cli/v3/internal/measurexlite"
// NewFailure is an alias for measurexlite.NewFailure
var NewFailure = measurexlite.NewFailure
+6 -54
View File
@@ -1,63 +1,15 @@
package measurex
import "github.com/ooni/probe-cli/v3/internal/measurexlite"
//
// Logger
//
// Code for logging
//
import (
"fmt"
"sync"
"time"
// NewOperationLogger is an alias for measurex.NewOperationLogger.
var NewOperationLogger = measurexlite.NewOperationLogger
"github.com/ooni/probe-cli/v3/internal/model"
)
// NewOperationLogger creates a new logger that logs
// about an in-progress operation.
func NewOperationLogger(logger model.Logger, format string, v ...interface{}) *OperationLogger {
ol := &OperationLogger{
sighup: make(chan interface{}),
logger: logger,
once: &sync.Once{},
message: fmt.Sprintf(format, v...),
wg: &sync.WaitGroup{},
}
ol.wg.Add(1)
go ol.logloop()
return ol
}
// OperationLogger logs about an in-progress operation
type OperationLogger struct {
logger model.Logger
message string
once *sync.Once
sighup chan interface{}
wg *sync.WaitGroup
}
func (ol *OperationLogger) logloop() {
defer ol.wg.Done()
timer := time.NewTimer(500 * time.Millisecond)
defer timer.Stop()
select {
case <-timer.C:
ol.logger.Infof("%s... in progress", ol.message)
case <-ol.sighup:
// we'll emit directly in stop
}
}
func (ol *OperationLogger) Stop(err error) {
ol.once.Do(func() {
close(ol.sighup)
ol.wg.Wait()
if err != nil {
ol.logger.Infof("%s... %s", ol.message, err.Error())
return
}
ol.logger.Infof("%s... ok", ol.message)
})
}
// OperationLogger is an alias for measurex.OperationLogger.
type OperationLogger = measurexlite.OperationLogger