2018-03-31 19:33:32 +00:00
package testing
2018-03-30 21:16:51 +00:00
import (
2019-05-15 20:12:11 +00:00
app2 "cwtch.im/cwtch/app"
2019-06-21 21:50:43 +00:00
"cwtch.im/cwtch/app/utils"
"cwtch.im/cwtch/event/bridge"
2018-05-28 18:05:06 +00:00
"cwtch.im/cwtch/model"
"cwtch.im/cwtch/peer"
2019-01-04 21:44:21 +00:00
"cwtch.im/cwtch/protocol/connections"
2018-05-28 18:05:06 +00:00
cwtchserver "cwtch.im/cwtch/server"
2018-05-20 19:58:16 +00:00
"fmt"
2018-11-10 22:14:12 +00:00
"git.openprivacy.ca/openprivacy/libricochet-go/connectivity"
2019-02-04 22:18:24 +00:00
"git.openprivacy.ca/openprivacy/libricochet-go/log"
2018-06-14 00:42:08 +00:00
"golang.org/x/net/proxy"
2018-11-10 22:14:12 +00:00
"os"
2018-05-30 18:42:17 +00:00
"runtime"
2019-01-04 21:44:21 +00:00
"runtime/pprof"
2018-05-20 19:58:16 +00:00
"testing"
"time"
2018-03-30 21:16:51 +00:00
)
2018-04-27 19:20:33 +00:00
const (
2018-06-05 22:06:38 +00:00
serverKeyfile = "./../server/app/private_key"
2018-06-03 19:36:20 +00:00
localKeyfile = "./private_key"
2018-04-27 19:20:33 +00:00
)
var (
2018-06-05 22:06:38 +00:00
aliceLines = [ ] string { "Hello, I'm Alice" , "bye" }
bobLines = [ ] string { "Hi, my name is Bob." , "toodles" , "welcome" }
carolLines = [ ] string { "Howdy, thanks!" }
2018-04-27 19:20:33 +00:00
)
2018-06-05 22:06:38 +00:00
func printAndCountVerifedTimeline ( t * testing . T , timeline [ ] model . Message ) int {
numVerified := 0
2018-05-20 19:58:16 +00:00
for _ , message := range timeline {
2018-10-05 03:18:34 +00:00
fmt . Printf ( "%v %v> %s\n" , message . Timestamp , message . PeerID , message . Message )
numVerified ++
2018-04-27 19:20:33 +00:00
}
2018-06-05 22:06:38 +00:00
return numVerified
2018-04-27 19:20:33 +00:00
}
2018-06-14 00:42:08 +00:00
func serverCheck ( t * testing . T , serverAddr string ) bool {
torDialer , err := proxy . SOCKS5 ( "tcp" , "127.0.0.1:9050" , nil , proxy . Direct )
if err != nil {
t . Logf ( "Could not get SOCKS5 proxy: %v" , err )
return false
}
2018-05-30 18:42:17 +00:00
2018-06-14 00:42:08 +00:00
// Doesn't seem to be a way to turn the default timeout of 2 minutes down
conn , err := torDialer . Dial ( "tcp" , serverAddr + ".onion:9878" )
if err != nil {
t . Logf ( "Could not dial %v: %v" , serverAddr , err )
return false
2018-04-27 19:20:33 +00:00
}
2018-06-14 00:42:08 +00:00
conn . Close ( )
return true
2018-04-27 19:20:33 +00:00
}
2019-05-15 20:12:11 +00:00
func waitForPeerGroupConnection ( t * testing . T , peer peer . CwtchPeer , groupID string ) {
2018-06-03 19:36:20 +00:00
for {
2019-05-15 20:12:11 +00:00
_ , ok := peer . GetProfile ( ) . Groups [ groupID ]
2018-06-03 19:36:20 +00:00
if ok {
2019-05-15 20:12:11 +00:00
state := peer . GetGroupState ( groupID )
//log.Infof("Waiting for Peer %v to join group %v - state: %v\n", peer.GetProfile().Name, groupID, state)
2018-06-14 00:42:08 +00:00
if state == connections . FAILED {
2019-05-15 20:12:11 +00:00
t . Fatalf ( "%v could not connect to %v" , peer . GetProfile ( ) . Onion , groupID )
2018-06-14 00:42:08 +00:00
}
2019-07-19 17:27:50 +00:00
if state != connections . SYNCED {
2019-05-15 20:12:11 +00:00
fmt . Printf ( "peer %v waiting connect to group %v, currently: %v\n" , peer . GetProfile ( ) . Onion , groupID , connections . ConnectionStateName [ state ] )
2019-01-04 21:44:21 +00:00
time . Sleep ( time . Second * 5 )
2018-06-03 19:36:20 +00:00
continue
2019-01-04 21:44:21 +00:00
} else {
break
2018-06-03 19:36:20 +00:00
}
2019-01-04 21:44:21 +00:00
} // It might take a second for the server to show up as it is now going through the event bus
time . Sleep ( time . Second )
2018-06-03 19:36:20 +00:00
}
return
}
2018-11-10 22:14:12 +00:00
func waitForPeerPeerConnection ( t * testing . T , peera peer . CwtchPeer , peerb peer . CwtchPeer ) {
for {
2019-05-15 20:12:11 +00:00
//peers := peera.GetPeers()
_ , ok := peera . GetProfile ( ) . Contacts [ peerb . GetProfile ( ) . Onion ]
2018-11-10 22:14:12 +00:00
if ok {
2019-05-15 20:12:11 +00:00
state := peera . GetPeerState ( peerb . GetProfile ( ) . Onion )
//log.Infof("Waiting for Peer %v to peer with peer: %v - state: %v\n", peera.GetProfile().Name, peerb.GetProfile().Name, state)
2018-11-10 22:14:12 +00:00
if state == connections . FAILED {
t . Fatalf ( "%v could not connect to %v" , peera . GetProfile ( ) . Onion , peerb . GetProfile ( ) . Onion )
}
if state != connections . AUTHENTICATED {
fmt . Printf ( "peer% v waiting connect to peer %v, currently: %v\n" , peera . GetProfile ( ) . Onion , peerb . GetProfile ( ) . Onion , connections . ConnectionStateName [ state ] )
2019-01-04 21:44:21 +00:00
time . Sleep ( time . Second * 5 )
2018-11-10 22:14:12 +00:00
continue
2019-01-04 21:44:21 +00:00
} else {
break
2018-11-10 22:14:12 +00:00
}
2019-01-04 21:44:21 +00:00
} // It might take a second for the peer to show up as it is now going through the event bus
time . Sleep ( time . Second )
2018-11-10 22:14:12 +00:00
}
return
}
2018-03-30 21:16:51 +00:00
func TestCwtchPeerIntegration ( t * testing . T ) {
2018-05-30 18:42:17 +00:00
numGoRoutinesStart := runtime . NumGoroutine ( )
2018-04-27 19:20:33 +00:00
2019-02-04 22:18:24 +00:00
log . AddEverythingFromPattern ( "connectivity" )
2019-06-21 21:50:43 +00:00
log . SetLevel ( log . LevelDebug )
log . ExcludeFromPattern ( "connection/connection" )
log . ExcludeFromPattern ( "outbound/3dhauthchannel" )
log . ExcludeFromPattern ( "event/eventmanager" )
2019-07-19 17:27:50 +00:00
log . ExcludeFromPattern ( "pipeBridge" )
2018-11-22 18:01:04 +00:00
acn , err := connectivity . StartTor ( "." , "" )
2018-11-10 22:14:12 +00:00
if err != nil {
t . Fatalf ( "Could not start Tor: %v" , err )
}
2019-01-28 20:09:25 +00:00
// ***** Cwtch Server management *****
2018-06-15 16:21:07 +00:00
var server * cwtchserver . Server
2018-04-27 19:20:33 +00:00
serverOnline := false
2018-06-14 00:42:08 +00:00
var serverAddr string
2018-05-20 19:58:16 +00:00
if ! serverOnline {
2018-06-14 00:42:08 +00:00
// launch app with new key
fmt . Println ( "No server found!" )
2018-05-30 18:42:17 +00:00
server = new ( cwtchserver . Server )
2018-06-14 00:42:08 +00:00
fmt . Println ( "Starting cwtch server..." )
2018-11-10 22:14:12 +00:00
os . Remove ( "server-test.json" )
2018-11-01 20:22:29 +00:00
config := cwtchserver . LoadConfig ( "." , "server-test.json" )
2018-10-05 03:18:34 +00:00
identity := config . Identity ( )
serverAddr = identity . Hostname ( )
2018-11-22 18:01:04 +00:00
go server . Run ( acn , config )
2018-04-27 19:20:33 +00:00
// let tor get established
fmt . Printf ( "Establishing Tor hidden service: %v...\n" , serverAddr )
} else {
fmt . Printf ( "Found existing cwtch server %v, using for tests...\n" , serverAddr )
}
2018-05-30 18:42:17 +00:00
numGoRoutinesPostServer := runtime . NumGoroutine ( )
2019-05-15 20:12:11 +00:00
app := app2 . NewApp ( acn , "./storage" )
2019-07-19 17:27:50 +00:00
bridgeClient := bridge . NewPipeBridgeClient ( "./clientPipe" , "./servicePipe" )
bridgeService := bridge . NewPipeBridgeService ( "./servicePipe" , "./clientPipe" )
appClient := app2 . NewAppClient ( "./storage" , bridgeClient )
appService := app2 . NewAppService ( acn , "./storage" , bridgeService )
2019-06-05 20:40:55 +00:00
numGoRoutinesPostAppStart := runtime . NumGoroutine ( )
2018-05-28 18:36:04 +00:00
2019-06-05 20:40:55 +00:00
// ***** cwtchPeer setup *****
2019-01-04 21:44:21 +00:00
2018-04-27 19:20:33 +00:00
fmt . Println ( "Creating Alice..." )
2019-06-05 20:40:55 +00:00
app . CreatePeer ( "alice" , "asdfasdf" )
2018-04-27 19:20:33 +00:00
2018-05-28 18:36:04 +00:00
fmt . Println ( "Creating Bob..." )
2019-06-05 20:40:55 +00:00
app . CreatePeer ( "bob" , "asdfasdf" )
2018-05-28 18:36:04 +00:00
2018-06-05 22:06:38 +00:00
fmt . Println ( "Creating Carol..." )
2019-06-05 20:40:55 +00:00
appClient . CreatePeer ( "carol" , "asdfasdf" )
2019-06-21 21:50:43 +00:00
alice := utils . WaitGetPeer ( app , "alice" )
2019-06-05 20:40:55 +00:00
fmt . Println ( "Alice created:" , alice . GetProfile ( ) . Onion )
2019-06-21 21:50:43 +00:00
bob := utils . WaitGetPeer ( app , "bob" )
2019-06-05 20:40:55 +00:00
fmt . Println ( "Bob created:" , bob . GetProfile ( ) . Onion )
2019-06-21 21:50:43 +00:00
carol := utils . WaitGetPeer ( appClient , "carol" )
2018-06-19 22:28:44 +00:00
fmt . Println ( "Carol created:" , carol . GetProfile ( ) . Onion )
2018-06-05 22:06:38 +00:00
2019-05-15 20:12:11 +00:00
app . LaunchPeers ( )
2019-06-05 20:40:55 +00:00
appClient . LaunchPeers ( )
2019-05-15 20:12:11 +00:00
fmt . Println ( "Waiting for Alice, Bob, and Carol to connect with onion network..." )
2018-07-02 16:09:49 +00:00
time . Sleep ( time . Second * 90 )
2018-05-30 18:42:17 +00:00
numGoRoutinesPostPeerStart := runtime . NumGoroutine ( )
2018-05-28 18:36:04 +00:00
2018-06-05 22:06:38 +00:00
// ***** Peering, server joining, group creation / invite *****
2018-05-28 18:36:04 +00:00
fmt . Println ( "Creating group on " , serverAddr , "..." )
2018-06-19 22:28:44 +00:00
groupID , _ , err := alice . StartGroup ( serverAddr )
2018-06-15 16:21:07 +00:00
fmt . Printf ( "Created group: %v!\n" , groupID )
2018-04-27 19:20:33 +00:00
if err != nil {
2018-06-05 22:06:38 +00:00
t . Errorf ( "Failed to init group: %v" , err )
2018-04-27 19:20:33 +00:00
return
}
2018-06-05 22:06:38 +00:00
fmt . Println ( "Alice peering with Bob..." )
2019-05-15 20:12:11 +00:00
alice . AddContact ( "Bob" , bob . GetProfile ( ) . Onion , false ) // Add contact so we can track connection state
2018-06-19 22:28:44 +00:00
alice . PeerWithOnion ( bob . GetProfile ( ) . Onion )
2018-06-05 22:06:38 +00:00
fmt . Println ( "Alice peering with Carol..." )
2019-05-15 20:12:11 +00:00
alice . AddContact ( "Carol" , carol . GetProfile ( ) . Onion , false )
2018-06-19 22:28:44 +00:00
alice . PeerWithOnion ( carol . GetProfile ( ) . Onion )
2018-04-27 19:20:33 +00:00
2018-06-05 22:06:38 +00:00
fmt . Println ( "Alice joining server..." )
alice . JoinServer ( serverAddr )
fmt . Println ( "Bob joining server..." )
bob . JoinServer ( serverAddr )
2018-11-10 22:14:12 +00:00
fmt . Println ( "Waiting for alice to join server..." )
2019-05-15 20:12:11 +00:00
waitForPeerGroupConnection ( t , alice , groupID )
2018-11-10 22:14:12 +00:00
2019-05-15 20:12:11 +00:00
//fmt.Println("Waiting for bob to join server...")
//waitForPeerGroupConnection(t, bob, groupID)
2018-11-10 22:14:12 +00:00
fmt . Println ( "Waiting for alice and Bob to peer..." )
waitForPeerPeerConnection ( t , alice , bob )
/ * fmt . Println ( "Waiting for peerings and server joins..." )
time . Sleep ( time . Second * 240 ) * /
2018-04-27 19:20:33 +00:00
2018-05-28 18:36:04 +00:00
fmt . Println ( "Alice inviting Bob to group..." )
2018-06-19 22:28:44 +00:00
err = alice . InviteOnionToGroup ( bob . GetProfile ( ) . Onion , groupID )
2018-05-28 18:36:04 +00:00
if err != nil {
t . Fatalf ( "Error for Alice inviting Bob to group: %v" , err )
}
time . Sleep ( time . Second * 10 )
2018-04-27 19:20:33 +00:00
2018-05-28 18:36:04 +00:00
fmt . Println ( "Bob examining groups and accepting invites..." )
2018-06-19 22:28:44 +00:00
for _ , groupID := range bob . GetGroups ( ) {
group := bob . GetGroup ( groupID )
2018-05-28 18:36:04 +00:00
fmt . Printf ( "Bob group: %v (Accepted: %v)\n" , group . GroupID , group . Accepted )
if group . Accepted == false {
fmt . Printf ( "Bob received and accepting group invite: %v\n" , group . GroupID )
bob . AcceptInvite ( group . GroupID )
}
}
2018-06-05 22:06:38 +00:00
time . Sleep ( time . Second * 5 )
2018-04-27 19:20:33 +00:00
2018-06-05 22:06:38 +00:00
numGoRoutinesPostServerConnect := runtime . NumGoroutine ( )
2018-05-30 18:42:17 +00:00
2018-06-03 19:36:20 +00:00
// Wait for them to join the server
2019-05-15 20:12:11 +00:00
waitForPeerGroupConnection ( t , alice , groupID )
waitForPeerGroupConnection ( t , bob , groupID )
2018-06-03 19:36:20 +00:00
//numGouRoutinesPostServerConnect := runtime.NumGoroutine()
2018-05-28 18:36:04 +00:00
// ***** Conversation *****
2018-04-27 19:20:33 +00:00
fmt . Println ( "Starting conversation in group..." )
2018-04-27 19:20:33 +00:00
// Conversation
fmt . Println ( "Alice> " , aliceLines [ 0 ] )
2018-06-15 16:21:07 +00:00
err = alice . SendMessageToGroup ( groupID , aliceLines [ 0 ] )
2018-05-20 19:58:16 +00:00
if err != nil {
2018-05-28 18:36:04 +00:00
t . Fatalf ( "Alice failed to send a message to the group: %v" , err )
2018-05-20 19:58:16 +00:00
}
time . Sleep ( time . Second * 10 )
2018-04-27 19:20:33 +00:00
fmt . Println ( "Bob> " , bobLines [ 0 ] )
2018-06-15 16:21:07 +00:00
err = bob . SendMessageToGroup ( groupID , bobLines [ 0 ] )
2018-05-20 19:58:16 +00:00
if err != nil {
2018-05-28 18:36:04 +00:00
t . Fatalf ( "Bob failed to send a message to the group: %v" , err )
2018-05-20 19:58:16 +00:00
}
time . Sleep ( time . Second * 10 )
2018-04-27 19:20:33 +00:00
fmt . Println ( "Alice> " , aliceLines [ 1 ] )
2018-06-15 16:21:07 +00:00
alice . SendMessageToGroup ( groupID , aliceLines [ 1 ] )
2018-06-05 22:06:38 +00:00
time . Sleep ( time . Second * 10 )
2018-04-27 19:20:33 +00:00
fmt . Println ( "Bob> " , bobLines [ 1 ] )
2018-06-15 16:21:07 +00:00
bob . SendMessageToGroup ( groupID , bobLines [ 1 ] )
2018-05-20 19:58:16 +00:00
time . Sleep ( time . Second * 10 )
2018-04-27 19:20:33 +00:00
2018-06-05 22:06:38 +00:00
fmt . Println ( "Alice inviting Carol to group..." )
2018-06-19 22:28:44 +00:00
err = alice . InviteOnionToGroup ( carol . GetProfile ( ) . Onion , groupID )
2018-06-05 22:06:38 +00:00
if err != nil {
t . Fatalf ( "Error for Alice inviting Carol to group: %v" , err )
}
2018-05-20 19:58:16 +00:00
time . Sleep ( time . Second * 10 )
2018-06-05 22:06:38 +00:00
fmt . Println ( "Carol examining groups and accepting invites..." )
2018-06-19 22:28:44 +00:00
for _ , groupID := range carol . GetGroups ( ) {
group := carol . GetGroup ( groupID )
2018-06-05 22:06:38 +00:00
fmt . Printf ( "Carol group: %v (Accepted: %v)\n" , group . GroupID , group . Accepted )
if group . Accepted == false {
fmt . Printf ( "Carol received and accepting group invite: %v\n" , group . GroupID )
carol . AcceptInvite ( group . GroupID )
}
}
fmt . Println ( "Shutting down Alice..." )
2019-05-15 20:12:11 +00:00
app . ShutdownPeer ( alice . GetProfile ( ) . Onion )
2018-06-05 22:06:38 +00:00
time . Sleep ( time . Second * 5 )
numGoRoutinesPostAlice := runtime . NumGoroutine ( )
fmt . Println ( "Carol joining server..." )
carol . JoinServer ( serverAddr )
2019-05-15 20:12:11 +00:00
waitForPeerGroupConnection ( t , carol , groupID )
2018-06-05 22:06:38 +00:00
numGoRotinesPostCarolConnect := runtime . NumGoroutine ( )
2018-04-27 19:20:33 +00:00
fmt . Println ( "Bob> " , bobLines [ 2 ] )
2018-06-15 16:21:07 +00:00
bob . SendMessageToGroup ( groupID , bobLines [ 2 ] )
2018-05-20 19:58:16 +00:00
time . Sleep ( time . Second * 10 )
2018-04-27 19:20:33 +00:00
2018-06-05 22:06:38 +00:00
fmt . Println ( "Carol> " , carolLines [ 0 ] )
2018-06-15 16:21:07 +00:00
carol . SendMessageToGroup ( groupID , carolLines [ 0 ] )
2018-06-05 22:06:38 +00:00
time . Sleep ( time . Second * 10 )
2018-04-27 19:20:33 +00:00
2018-05-28 18:36:04 +00:00
// ***** Verify Test *****
2018-04-27 19:20:33 +00:00
2018-06-27 15:14:59 +00:00
fmt . Println ( "Final syncing time..." )
time . Sleep ( time . Second * 30 )
2018-04-27 19:20:33 +00:00
2018-06-19 22:28:44 +00:00
alicesGroup := alice . GetGroup ( groupID )
2018-04-27 19:20:33 +00:00
if alicesGroup == nil {
t . Error ( "aliceGroup == nil" )
return
}
2018-04-27 19:20:33 +00:00
2018-06-05 22:06:38 +00:00
fmt . Printf ( "Alice's TimeLine:\n" )
aliceVerified := printAndCountVerifedTimeline ( t , alicesGroup . GetTimeline ( ) )
if aliceVerified != 4 {
t . Errorf ( "Alice did not have 4 verified messages" )
}
2018-04-27 19:20:33 +00:00
2018-06-19 22:28:44 +00:00
bobsGroup := bob . GetGroup ( groupID )
2018-04-27 19:20:33 +00:00
if bobsGroup == nil {
t . Error ( "bobGroup == nil" )
return
}
2018-06-05 22:06:38 +00:00
fmt . Printf ( "Bob's TimeLine:\n" )
bobVerified := printAndCountVerifedTimeline ( t , bobsGroup . GetTimeline ( ) )
2018-10-05 03:18:34 +00:00
if bobVerified != 6 {
2018-06-05 22:06:38 +00:00
t . Errorf ( "Bob did not have 5 verified messages" )
}
2018-06-19 22:28:44 +00:00
carolsGroup := carol . GetGroup ( groupID )
2018-06-05 22:06:38 +00:00
fmt . Printf ( "Carol's TimeLine:\n" )
carolVerified := printAndCountVerifedTimeline ( t , carolsGroup . GetTimeline ( ) )
2018-10-05 03:18:34 +00:00
if carolVerified != 6 {
2018-06-05 22:06:38 +00:00
t . Errorf ( "Carol did not have 3 verified messages" )
}
2018-04-27 19:20:33 +00:00
2018-06-05 22:06:38 +00:00
if len ( alicesGroup . GetTimeline ( ) ) != 4 {
2018-04-27 19:20:33 +00:00
t . Errorf ( "Alice's timeline does not have all messages" )
2018-06-27 15:14:59 +00:00
} else {
// check message 0,1,2,3
aliceGroupTimeline := alicesGroup . GetTimeline ( )
if aliceGroupTimeline [ 0 ] . Message != aliceLines [ 0 ] || aliceGroupTimeline [ 1 ] . Message != bobLines [ 0 ] ||
aliceGroupTimeline [ 2 ] . Message != aliceLines [ 1 ] || aliceGroupTimeline [ 3 ] . Message != bobLines [ 1 ] {
t . Errorf ( "Some of Alice's timeline messages did not have the expected content!" )
}
2018-04-27 19:20:33 +00:00
}
2018-06-05 22:06:38 +00:00
if len ( bobsGroup . GetTimeline ( ) ) != 6 {
t . Errorf ( "Bob's timeline does not have all messages" )
2018-06-27 15:14:59 +00:00
} else {
// check message 0,1,2,3,4,5
bobGroupTimeline := bobsGroup . GetTimeline ( )
if bobGroupTimeline [ 0 ] . Message != aliceLines [ 0 ] || bobGroupTimeline [ 1 ] . Message != bobLines [ 0 ] ||
bobGroupTimeline [ 2 ] . Message != aliceLines [ 1 ] || bobGroupTimeline [ 3 ] . Message != bobLines [ 1 ] ||
bobGroupTimeline [ 4 ] . Message != bobLines [ 2 ] || bobGroupTimeline [ 5 ] . Message != carolLines [ 0 ] {
t . Errorf ( "Some of Bob's timeline messages did not have the expected content!" )
}
2018-06-05 22:06:38 +00:00
}
if len ( carolsGroup . GetTimeline ( ) ) != 6 {
t . Errorf ( "Carol's timeline does not have all messages" )
2018-06-27 15:14:59 +00:00
} else {
// check message 0,1,2,3,4,5
carolGroupTimeline := carolsGroup . GetTimeline ( )
if carolGroupTimeline [ 0 ] . Message != aliceLines [ 0 ] || carolGroupTimeline [ 1 ] . Message != bobLines [ 0 ] ||
carolGroupTimeline [ 2 ] . Message != aliceLines [ 1 ] || carolGroupTimeline [ 3 ] . Message != bobLines [ 1 ] ||
carolGroupTimeline [ 4 ] . Message != bobLines [ 2 ] || carolGroupTimeline [ 5 ] . Message != carolLines [ 0 ] {
t . Errorf ( "Some of Carol's timeline messages did not have the expected content!" )
}
2018-04-27 19:20:33 +00:00
}
2018-05-30 18:42:17 +00:00
fmt . Println ( "Shutting down Bob..." )
2019-05-15 20:12:11 +00:00
app . ShutdownPeer ( bob . GetProfile ( ) . Onion )
2018-05-30 18:42:17 +00:00
time . Sleep ( time . Second * 3 )
numGoRoutinesPostBob := runtime . NumGoroutine ( )
if server != nil {
fmt . Println ( "Shutting down server..." )
server . Shutdown ( )
time . Sleep ( time . Second * 3 )
}
numGoRoutinesPostServerShutdown := runtime . NumGoroutine ( )
2018-06-05 22:06:38 +00:00
2019-01-04 21:44:21 +00:00
fmt . Println ( "Shutting down Carol..." )
2019-06-05 20:40:55 +00:00
appClient . ShutdownPeer ( carol . GetProfile ( ) . Onion )
2018-05-30 18:42:17 +00:00
time . Sleep ( time . Second * 3 )
2018-06-05 22:06:38 +00:00
numGoRoutinesPostCarol := runtime . NumGoroutine ( )
2018-05-30 18:42:17 +00:00
2019-06-05 20:40:55 +00:00
fmt . Println ( "Shutting down apps..." )
2019-07-19 17:27:50 +00:00
fmt . Printf ( "app Shutdown: %v\n" , runtime . NumGoroutine ( ) )
2019-06-05 20:40:55 +00:00
app . Shutdown ( )
2019-07-19 17:27:50 +00:00
fmt . Printf ( "appClientShutdown: %v\n" , runtime . NumGoroutine ( ) )
2019-06-05 20:40:55 +00:00
appClient . Shutdown ( )
2019-07-19 17:27:50 +00:00
fmt . Printf ( "appServiceShutdown: %v\n" , runtime . NumGoroutine ( ) )
2019-06-05 20:40:55 +00:00
appService . Shutdown ( )
2019-07-19 17:27:50 +00:00
fmt . Printf ( "bridgeClientShutdown: %v\n" , runtime . NumGoroutine ( ) )
bridgeClient . Shutdown ( )
time . Sleep ( 2 * time . Second )
fmt . Printf ( "brideServiceShutdown: %v\n" , runtime . NumGoroutine ( ) )
bridgeService . Shutdown ( )
time . Sleep ( 2 * time . Second )
fmt . Printf ( "Done shutdown: %v\n" , runtime . NumGoroutine ( ) )
2019-06-05 20:40:55 +00:00
numGoRoutinesPostAppShutdown := runtime . NumGoroutine ( )
2019-02-04 22:18:24 +00:00
fmt . Println ( "Shutting down ACN..." )
acn . Close ( )
time . Sleep ( time . Second * 2 ) // Server ^^ has a 5 second loop attempting reconnect before exiting
numGoRoutinesPostACN := runtime . NumGoroutine ( )
2019-01-04 21:44:21 +00:00
// Printing out the current goroutines
// Very useful if we are leaking any.
pprof . Lookup ( "goroutine" ) . WriteTo ( os . Stdout , 1 )
2019-06-05 20:40:55 +00:00
fmt . Printf ( "numGoRoutinesStart: %v\nnumGoRoutinesPostServer: %v\nnumGoRoutinesPostAppStart: %v\nnumGoRoutinesPostPeerStart: %v\nnumGoRoutinesPostPeerAndServerConnect: %v\n" +
"numGoRoutinesPostAlice: %v\nnumGoRotinesPostCarolConnect: %v\nnumGoRoutinesPostBob: %v\nnumGoRoutinesPostServerShutdown: %v\nnumGoRoutinesPostCarol: %v\nnumGoRoutinesPostAppShutdown: %v\nnumGoRoutinesPostACN: %v\n" ,
numGoRoutinesStart , numGoRoutinesPostServer , numGoRoutinesPostAppStart , numGoRoutinesPostPeerStart , numGoRoutinesPostServerConnect ,
numGoRoutinesPostAlice , numGoRotinesPostCarolConnect , numGoRoutinesPostBob , numGoRoutinesPostServerShutdown , numGoRoutinesPostCarol , numGoRoutinesPostAppShutdown , numGoRoutinesPostACN )
2018-05-30 18:42:17 +00:00
2019-02-04 22:18:24 +00:00
if numGoRoutinesStart != numGoRoutinesPostACN {
t . Errorf ( "Number of GoRoutines at start (%v) does not match number of goRoutines after cleanup of peers and servers (%v), clean up failed, leak detected!" , numGoRoutinesStart , numGoRoutinesPostACN )
2018-05-30 18:42:17 +00:00
}
2018-06-14 00:42:08 +00:00
2018-03-30 21:16:51 +00:00
}