58adb68b2c
* refactor: move tracex outside of engine/netx Consistently with https://github.com/ooni/probe/issues/2121 and https://github.com/ooni/probe/issues/2115, we can now move tracex outside of engine/netx. The main reason why this makes sense now is that the package is now changed significantly from the one that we imported from ooni/probe-engine. We have improved its implementation, which had not been touched significantly for quite some time, and converted it to unit testing. I will document tomorrow some extra work I'd like to do with this package but likely could not do $soon. * go fmt * regen tutorials
91 lines
2.0 KiB
Go
91 lines
2.0 KiB
Go
package tracex
|
|
|
|
import (
|
|
"sync"
|
|
"testing"
|
|
|
|
"github.com/ooni/probe-cli/v3/internal/model/mocks"
|
|
)
|
|
|
|
func TestSaver(t *testing.T) {
|
|
t.Run("concurrent writes followed by read", func(t *testing.T) {
|
|
saver := Saver{}
|
|
var wg sync.WaitGroup
|
|
const parallel = 10
|
|
wg.Add(parallel)
|
|
for idx := 0; idx < parallel; idx++ {
|
|
go func() {
|
|
saver.Write(&EventReadFromOperation{&EventValue{}})
|
|
wg.Done()
|
|
}()
|
|
}
|
|
wg.Wait()
|
|
ev := saver.Read()
|
|
if len(ev) != parallel {
|
|
t.Fatal("unexpected number of events read")
|
|
}
|
|
})
|
|
|
|
t.Run("NewConnectObserver", func(t *testing.T) {
|
|
t.Run("nil Saver", func(t *testing.T) {
|
|
var saver *Saver
|
|
obs := saver.NewConnectObserver()
|
|
if obs != nil {
|
|
t.Fatal("expected nil observer")
|
|
}
|
|
})
|
|
|
|
t.Run("nonnnil Saver", func(t *testing.T) {
|
|
saver := &Saver{}
|
|
obs := saver.NewConnectObserver()
|
|
underlying := obs.(*dialerConnectObserver)
|
|
if underlying.saver != saver {
|
|
t.Fatal("invalid saver")
|
|
}
|
|
})
|
|
})
|
|
|
|
t.Run("NewReadWriteObserver", func(t *testing.T) {
|
|
t.Run("nil Saver", func(t *testing.T) {
|
|
var saver *Saver
|
|
obs := saver.NewReadWriteObserver()
|
|
if obs != nil {
|
|
t.Fatal("expected nil observer")
|
|
}
|
|
})
|
|
|
|
t.Run("nonnnil Saver", func(t *testing.T) {
|
|
saver := &Saver{}
|
|
obs := saver.NewReadWriteObserver()
|
|
underlying := obs.(*dialerReadWriteObserver)
|
|
if underlying.saver != saver {
|
|
t.Fatal("invalid saver")
|
|
}
|
|
})
|
|
})
|
|
|
|
t.Run("WrapQUICDialer", func(t *testing.T) {
|
|
t.Run("nil Saver", func(t *testing.T) {
|
|
var saver *Saver
|
|
base := &mocks.QUICDialer{}
|
|
qd := saver.WrapQUICDialer(base)
|
|
if qd != base {
|
|
t.Fatal("unexpected returned QUICDialer")
|
|
}
|
|
})
|
|
|
|
t.Run("nonnnil Saver", func(t *testing.T) {
|
|
saver := &Saver{}
|
|
base := &mocks.QUICDialer{}
|
|
qd := saver.WrapQUICDialer(base)
|
|
underlying := qd.(*QUICDialerSaver)
|
|
if underlying.Saver != saver {
|
|
t.Fatal("invalid Saver")
|
|
}
|
|
if underlying.QUICDialer != base {
|
|
t.Fatal("invalid QUICDialer")
|
|
}
|
|
})
|
|
})
|
|
}
|