Skip to content

Commit

Permalink
Add demonstration test
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkren committed Jun 18, 2019
1 parent 0d37f4f commit b14212e
Showing 1 changed file with 142 additions and 5 deletions.
147 changes: 142 additions & 5 deletions pkg/dmsg/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ import (
"math"
"math/rand"
"net"
"os"
"runtime"
"runtime/pprof"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -544,7 +541,7 @@ func TestServer_Serve(t *testing.T) {
})

t.Run("test failed accept not hanging already established transport", func(t *testing.T) {
f, err := os.Create("./cpu.prof")
/*f, err := os.Create("./cpu.prof")
if err != nil {
log.Fatalf("Error creating cpu profile: %v\n", err)
}
Expand All @@ -566,7 +563,7 @@ func TestServer_Serve(t *testing.T) {
if err := p.WriteTo(blockF, 0); err != nil {
log.Fatalf("Error saving block profile: %v\n", err)
}
}()
}()*/

// generate keys for both clients
aPK, aSK := cipher.GenerateKeyPair()
Expand Down Expand Up @@ -683,6 +680,146 @@ func TestServer_Serve(t *testing.T) {
err = b.Close()
require.NoError(t, err)
})

t.Run("test failed accept not hanging already established transport 2", func(t *testing.T) {
/*f, err := os.Create("./cpu.prof")
if err != nil {
log.Fatalf("Error creating cpu profile: %v\n", err)
}
if err := pprof.StartCPUProfile(f); err != nil {
log.Fatalf("Error starting cpu profiling: %v\n", err)
}
defer pprof.StopCPUProfile()
blockF, err := os.Create("./block.prof")
if err != nil {
log.Fatalf("Error creating block profile: %v\n", err)
}
runtime.SetBlockProfileRate(1)
p := pprof.Lookup("block")
defer func() {
if err := p.WriteTo(blockF, 0); err != nil {
log.Fatalf("Error saving block profile: %v\n", err)
}
}()*/

// generate keys for both clients
aPK, aSK := cipher.GenerateKeyPair()
bPK, bSK := cipher.GenerateKeyPair()

// create remote
a := NewClient(aPK, aSK, dc)
a.SetLogger(logging.MustGetLogger("A"))
err = a.InitiateServerConnections(context.Background(), 1)
require.NoError(t, err)

// create initiator
b := NewClient(bPK, bSK, dc)
b.SetLogger(logging.MustGetLogger("B"))
err = b.InitiateServerConnections(context.Background(), 1)
require.NoError(t, err)

aDone := make(chan struct{})
var aTransport transport.Transport
var aErr error
go func() {
aTransport, aErr = a.Accept(context.Background())
close(aDone)
}()

bTransport, err := b.Dial(context.Background(), aPK)
require.NoError(t, err)

<-aDone
require.NoError(t, aErr)

aTpDone := make(chan struct{})
bTpDone := make(chan struct{})

var bErr error
var tpReadWriteWG sync.WaitGroup
tpReadWriteWG.Add(2)
// run infinite reading from tp loop in goroutine
go func() {
for {
select {
case <-aTpDone:
log.Println("ATransport DONE")
tpReadWriteWG.Done()
return
default:
var msg []byte
if _, aErr = aTransport.Read(msg); aErr != nil {
tpReadWriteWG.Done()
return
}
log.Printf("GOT MESSAGE %s", string(msg))
}
}
}()

// run infinite writing to tp loop in goroutine
go func() {
for {
select {
case <-bTpDone:
log.Println("BTransport DONE")
tpReadWriteWG.Done()
return
default:
msg := []byte("Hello there!")
log.Println("BEFORE TRANSPORT WRITE")
if _, bErr = bTransport.Write(msg); bErr != nil {
log.Println("ERROR IN TRANSPORT WRITE")
tpReadWriteWG.Done()
return
}
log.Println("AFTER TRANSPORT WRITE")
}
}
}()

// continue creating transports untill the error occurs
for {
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)

_, err = a.Dial(ctx, bPK)
cancel()
if err != nil {
break
}
}
// must be error
require.Error(t, err)

// wait more time to ensure that the initially created transport works
time.Sleep(2 * time.Second)

// stop reading/writing goroutines
close(aTpDone)
close(bTpDone)

// wait for goroutines to stop
tpReadWriteWG.Wait()
// check that the initial transport had been working properly all the time
require.NoError(t, aErr)
require.NoError(t, bErr)

err = aTransport.Close()
require.NoError(t, err)

err = bTransport.Close()
require.NoError(t, err)

err = a.Close()
require.NoError(t, err)

err = b.Close()
require.NoError(t, err)
})
}

// Given two client instances (a & b) and a server instance (s),
Expand Down

0 comments on commit b14212e

Please sign in to comment.