refactor(netxlite): finish grouping tests (#488)

They are now more readable. I'll do another pass and start
separating integration testing from unit testing.

I think we need to have some always on integration testing
for netxlite that runs on macOS, linux, and windows.

See https://github.com/ooni/probe/issues/1591
This commit is contained in:
Simone Basso 2021-09-08 11:39:27 +02:00 committed by GitHub
commit f2e3e5cc08
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 1100 additions and 1088 deletions

View file

@ -7,74 +7,80 @@ import (
"github.com/ooni/probe-cli/v3/internal/netxlite/mocks"
)
func TestResolverLegacyAdapterWithCompatibleType(t *testing.T) {
var called bool
r := NewResolverLegacyAdapter(&mocks.Resolver{
MockNetwork: func() string {
return "network"
},
MockAddress: func() string {
return "address"
},
MockCloseIdleConnections: func() {
called = true
},
func TestResolverLegacyAdapter(t *testing.T) {
t.Run("with compatible type", func(t *testing.T) {
var called bool
r := NewResolverLegacyAdapter(&mocks.Resolver{
MockNetwork: func() string {
return "network"
},
MockAddress: func() string {
return "address"
},
MockCloseIdleConnections: func() {
called = true
},
})
if r.Network() != "network" {
t.Fatal("invalid Network")
}
if r.Address() != "address" {
t.Fatal("invalid Address")
}
r.CloseIdleConnections()
if !called {
t.Fatal("not called")
}
})
if r.Network() != "network" {
t.Fatal("invalid Network")
}
if r.Address() != "address" {
t.Fatal("invalid Address")
}
r.CloseIdleConnections()
if !called {
t.Fatal("not called")
}
}
func TestResolverLegacyAdapterDefaults(t *testing.T) {
r := NewResolverLegacyAdapter(&net.Resolver{})
if r.Network() != "adapter" {
t.Fatal("invalid Network")
}
if r.Address() != "" {
t.Fatal("invalid Address")
}
r.CloseIdleConnections() // does not crash
}
func TestDialerLegacyAdapterWithCompatibleType(t *testing.T) {
var called bool
r := NewDialerLegacyAdapter(&mocks.Dialer{
MockCloseIdleConnections: func() {
called = true
},
t.Run("with incompatible type", func(t *testing.T) {
r := NewResolverLegacyAdapter(&net.Resolver{})
if r.Network() != "adapter" {
t.Fatal("invalid Network")
}
if r.Address() != "" {
t.Fatal("invalid Address")
}
r.CloseIdleConnections() // does not crash
})
r.CloseIdleConnections()
if !called {
t.Fatal("not called")
}
}
func TestDialerLegacyAdapterDefaults(t *testing.T) {
r := NewDialerLegacyAdapter(&net.Dialer{})
r.CloseIdleConnections() // does not crash
}
func TestQUICContextDialerAdapterWithCompatibleType(t *testing.T) {
var called bool
d := NewQUICDialerFromContextDialerAdapter(&mocks.QUICDialer{
MockCloseIdleConnections: func() {
called = true
},
func TestDialerLegacyAdapter(t *testing.T) {
t.Run("with compatible type", func(t *testing.T) {
var called bool
r := NewDialerLegacyAdapter(&mocks.Dialer{
MockCloseIdleConnections: func() {
called = true
},
})
r.CloseIdleConnections()
if !called {
t.Fatal("not called")
}
})
t.Run("with incompatible type", func(t *testing.T) {
r := NewDialerLegacyAdapter(&net.Dialer{})
r.CloseIdleConnections() // does not crash
})
d.CloseIdleConnections()
if !called {
t.Fatal("not called")
}
}
func TestQUICContextDialerAdapterDefaults(t *testing.T) {
d := NewQUICDialerFromContextDialerAdapter(&mocks.QUICContextDialer{})
d.CloseIdleConnections() // does not crash
func TestQUICContextDialerAdapter(t *testing.T) {
t.Run("with compatible type", func(t *testing.T) {
var called bool
d := NewQUICDialerFromContextDialerAdapter(&mocks.QUICDialer{
MockCloseIdleConnections: func() {
called = true
},
})
d.CloseIdleConnections()
if !called {
t.Fatal("not called")
}
})
t.Run("with incompatible type", func(t *testing.T) {
d := NewQUICDialerFromContextDialerAdapter(&mocks.QUICContextDialer{})
d.CloseIdleConnections() // does not crash
})
}