ooni-probe-cli/internal/netxlite/trace_test.go
Simone Basso 5ebdeb56ca
feat: tlsping and tcpping using step-by-step (#815)
## Checklist

- [x] I have read the [contribution guidelines](https://github.com/ooni/probe-cli/blob/master/CONTRIBUTING.md)
- [x] reference issue for this pull request: https://github.com/ooni/probe/issues/2158
- [x] if you changed anything related how experiments work and you need to reflect these changes in the ooni/spec repository, please link to the related ooni/spec pull request: https://github.com/ooni/spec/pull/250

## Description

This diff refactors the codebase to reimplement tlsping and tcpping
to use the step-by-step measurements style.

See docs/design/dd-003-step-by-step.md for more information on the
step-by-step measurement style.
2022-07-01 12:22:22 +02:00

41 lines
921 B
Go

package netxlite
import (
"context"
"testing"
"github.com/ooni/probe-cli/v3/internal/model/mocks"
)
func TestContextTraceOrDefault(t *testing.T) {
t.Run("without a configured trace we get a default", func(t *testing.T) {
ctx := context.Background()
tx := ContextTraceOrDefault(ctx)
_ = tx.(*traceDefault) // panic if cannot cast
})
t.Run("with a configured trace we get the expected trace", func(t *testing.T) {
realTrace := &mocks.Trace{}
ctx := ContextWithTrace(context.Background(), realTrace)
tx := ContextTraceOrDefault(ctx)
if tx != realTrace {
t.Fatal("not the trace we expected")
}
})
}
func TestContextWithTrace(t *testing.T) {
t.Run("panics if passed a nil trace", func(t *testing.T) {
var called bool
func() {
defer func() {
called = (recover() != nil)
}()
_ = ContextWithTrace(context.Background(), nil)
}()
if !called {
t.Fatal("not called")
}
})
}