Skip to content

Commit

Permalink
Changes:
Browse files Browse the repository at this point in the history
1. pkg/transport/mock.go:
- renamed MockTransportManagers to MockTransportManagersPair
- created MockTransportManager for testing  Manager
2. pkg/transport/manager_test.go: created ExampleManagerCreateTransport

Result:

ExampleManagerCreateTransport fails as expected
  • Loading branch information
ayuryshev committed Apr 1, 2019
1 parent 764be58 commit 4751cd7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 28 deletions.
47 changes: 23 additions & 24 deletions pkg/transport/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"
"time"

"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand All @@ -24,7 +25,7 @@ func TestTransportManager(t *testing.T) {
c1 := &ManagerConfig{pk1, sk1, client, logStore, nil}
c2 := &ManagerConfig{pk2, sk2, client, logStore, nil}

f1, f2 := NewMockFactory(pk1, pk2)
f1, f2 := NewMockFactoryPair(pk1, pk2)
m1, err := NewManager(c1, f1)
require.NoError(t, err)

Expand Down Expand Up @@ -112,7 +113,7 @@ func TestTransportManagerReEstablishTransports(t *testing.T) {
c1 := &ManagerConfig{pk1, sk1, client, logStore, nil}
c2 := &ManagerConfig{pk2, sk2, client, logStore, nil}

f1, f2 := NewMockFactory(pk1, pk2)
f1, f2 := NewMockFactoryPair(pk1, pk2)
m1, err := NewManager(c1, f1)
require.NoError(t, err)

Expand Down Expand Up @@ -169,7 +170,7 @@ func TestTransportManagerLogs(t *testing.T) {
c1 := &ManagerConfig{pk1, sk1, client, logStore1, nil}
c2 := &ManagerConfig{pk2, sk2, client, logStore2, nil}

f1, f2 := NewMockFactory(pk1, pk2)
f1, f2 := NewMockFactoryPair(pk1, pk2)
m1, err := NewManager(c1, f1)
require.NoError(t, err)

Expand Down Expand Up @@ -252,29 +253,27 @@ func ExampleGetTransportUUID() {
}

func ExampleManagerCreateTransport() {
// Repetition is required here to guarantee that correctness does not depends on order of edges
for i := 0; i < 256; i++ {
pkB, mgrA, err := MockTransportManager()
if err != nil {
fmt.Printf("MockTransportManager failed on iteration %v with: %v\n", i, err)
return
}



// discovery := transport.NewDiscoveryMock()
// logs := transport.InMemoryTransportLogStore()

// var sk1, sk2 cipher.SecKey
// pk1, sk1 = cipher.GenerateKeyPair()
// pk2, sk2 = cipher.GenerateKeyPair()

// c1 := &transport.ManagerConfig{PubKey: pk1, SecKey: sk1, DiscoveryClient: discovery, LogStore: logs}
// c2 := &transport.ManagerConfig{PubKey: pk2, SecKey: sk2, DiscoveryClient: discovery, LogStore: logs}

// f1, f2 := transport.NewMockFactory(pk1, pk2)
mtrAB, err := mgrA.CreateTransport(context.TODO(), pkB, "mock", true)
if err != nil {
fmt.Printf("Manager.CreateTransport failed on iteration %v with: %v\n", i, err)
return
}

// if m1, err = transport.NewManager(c1, f1); err != nil {
// return
// }
// if m2, err = transport.NewManager(c2, f2); err != nil {
// return
// }
if (mtrAB.ID == uuid.UUID{}) {
fmt.Printf("Manager.CreateTransport failed on iteration %v", i)
return
}
}

// errCh = make(chan error)
// go func() { errCh <- m1.Serve(context.TODO()) }()
fmt.Println("Manager.CreateTransport success")

// Output: Manager.CreateTransport success
}
15 changes: 11 additions & 4 deletions pkg/transport/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ type MockFactory struct {
fType string
}

// NewMockFactory constructs a pair of MockFactories.
func NewMockFactory(local, remote cipher.PubKey) (*MockFactory, *MockFactory) {
// NewMockFactoryPair constructs a pair of MockFactories.
func NewMockFactoryPair(local, remote cipher.PubKey) (*MockFactory, *MockFactory) {
in := make(chan *fConn)
out := make(chan *fConn)
return &MockFactory{local, in, out, "mock"}, &MockFactory{remote, out, in, "mock"}
Expand Down Expand Up @@ -143,7 +143,8 @@ func (m *MockTransport) Type() string {
return "mock"
}

func MockTransportManagers() (pk1, pk2 cipher.PubKey, m1, m2 *Manager, errCh chan error, err error) {
// MockTransportManagersPair constructs a pair of Transport Managers
func MockTransportManagersPair() (pk1, pk2 cipher.PubKey, m1, m2 *Manager, errCh chan error, err error) {
discovery := NewDiscoveryMock()
logs := InMemoryTransportLogStore()

Expand All @@ -154,7 +155,7 @@ func MockTransportManagers() (pk1, pk2 cipher.PubKey, m1, m2 *Manager, errCh cha
c1 := &ManagerConfig{PubKey: pk1, SecKey: sk1, DiscoveryClient: discovery, LogStore: logs}
c2 := &ManagerConfig{PubKey: pk2, SecKey: sk2, DiscoveryClient: discovery, LogStore: logs}

f1, f2 := NewMockFactory(pk1, pk2)
f1, f2 := NewMockFactoryPair(pk1, pk2)

if m1, err = NewManager(c1, f1); err != nil {
return
Expand All @@ -169,3 +170,9 @@ func MockTransportManagers() (pk1, pk2 cipher.PubKey, m1, m2 *Manager, errCh cha

return
}

// MockTransportManager creates Manager
func MockTransportManager() (cipher.PubKey, *Manager, error) {
_, pkB, mgrA, _, _, err := MockTransportManagersPair()
return pkB, mgrA, err
}

0 comments on commit 4751cd7

Please sign in to comment.