refactor(tor): rewrite using measurex (#652)
This diff rewrites the tor experiment to use measurex "easy" API. To this end, we need to introduce an "easy" measurex API, which basically performs easy measurements returning two pieces of data: 1. the resulting measurement, which is already using the OONI archival data format and is always non-nil 2. a failure (i.e., the pointer to an error string), which is nil on success and points to a string on failure With this change, we should now be able to completely dispose of the original netx API, which was only used by tor. Reference issue: https://github.com/ooni/probe/issues/1688.
This commit is contained in:
@@ -22,3 +22,8 @@ func PanicIfFalse(assertion bool, message string) {
|
||||
func PanicIfTrue(assertion bool, message string) {
|
||||
PanicIfFalse(!assertion, message)
|
||||
}
|
||||
|
||||
// PanicIfNil calls panic if the given interface is nil.
|
||||
func PanicIfNil(v interface{}, message string) {
|
||||
PanicIfTrue(v == nil, message)
|
||||
}
|
||||
|
||||
@@ -12,12 +12,12 @@ func TestPanicOnError(t *testing.T) {
|
||||
defer func() {
|
||||
out = recover().(error)
|
||||
}()
|
||||
runtimex.PanicOnError(in, "antani failed")
|
||||
runtimex.PanicOnError(in, "we expect this assertion to fail")
|
||||
return
|
||||
}
|
||||
|
||||
t.Run("error is nil", func(t *testing.T) {
|
||||
runtimex.PanicOnError(nil, "antani failed")
|
||||
runtimex.PanicOnError(nil, "this assertion should not fail")
|
||||
})
|
||||
|
||||
t.Run("error is not nil", func(t *testing.T) {
|
||||
@@ -38,7 +38,7 @@ func TestPanicIfFalse(t *testing.T) {
|
||||
}
|
||||
|
||||
t.Run("assertion is true", func(t *testing.T) {
|
||||
runtimex.PanicIfFalse(true, "antani failed")
|
||||
runtimex.PanicIfFalse(true, "this assertion should not fail")
|
||||
})
|
||||
|
||||
t.Run("assertion is false", func(t *testing.T) {
|
||||
@@ -60,7 +60,7 @@ func TestPanicIfTrue(t *testing.T) {
|
||||
}
|
||||
|
||||
t.Run("assertion is false", func(t *testing.T) {
|
||||
runtimex.PanicIfTrue(false, "antani failed")
|
||||
runtimex.PanicIfTrue(false, "this assertion should not fail")
|
||||
})
|
||||
|
||||
t.Run("assertion is true", func(t *testing.T) {
|
||||
@@ -71,3 +71,25 @@ func TestPanicIfTrue(t *testing.T) {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestPanicIfNil(t *testing.T) {
|
||||
badfunc := func(in interface{}, message string) (out error) {
|
||||
defer func() {
|
||||
out = errors.New(recover().(string))
|
||||
}()
|
||||
runtimex.PanicIfNil(in, message)
|
||||
return
|
||||
}
|
||||
|
||||
t.Run("value is not nil", func(t *testing.T) {
|
||||
runtimex.PanicIfNil(false, "this assertion should not fail")
|
||||
})
|
||||
|
||||
t.Run("value is nil", func(t *testing.T) {
|
||||
message := "mocked error"
|
||||
err := badfunc(nil, message)
|
||||
if err == nil || err.Error() != message {
|
||||
t.Fatal("not the error we expected", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user