forked from cwtch.im/cwtch
simplify server check so it doesn't leak goroutine; clean up init logic
This commit is contained in:
parent
583a8013d6
commit
f68c5096ec
|
@ -7,8 +7,8 @@ import (
|
|||
"cwtch.im/cwtch/peer/connections"
|
||||
cwtchserver "cwtch.im/cwtch/server"
|
||||
"fmt"
|
||||
"github.com/s-rah/go-ricochet"
|
||||
"github.com/s-rah/go-ricochet/utils"
|
||||
"golang.org/x/net/proxy"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
|
@ -28,8 +28,20 @@ var (
|
|||
carolLines = []string{"Howdy, thanks!"}
|
||||
)
|
||||
|
||||
func loadOrGenPrivateKey(t *testing.T) (pk *rsa.PrivateKey, generated bool) {
|
||||
func loadPrivateKey(t *testing.T) *rsa.PrivateKey {
|
||||
if _, err := os.Stat(serverKeyfile); os.IsNotExist(err) {
|
||||
return nil
|
||||
}
|
||||
|
||||
fmt.Println("Found server key " + serverKeyfile + ", loading...")
|
||||
pk, err := utils.LoadPrivateKeyFromFile(serverKeyfile)
|
||||
if err != nil {
|
||||
t.Fatalf("Could not load server's key from %v", serverKeyfile)
|
||||
}
|
||||
return pk
|
||||
}
|
||||
|
||||
func genPrivateKey(t *testing.T) *rsa.PrivateKey {
|
||||
fmt.Println("generating new private key...")
|
||||
pk, err := utils.GeneratePrivateKey()
|
||||
if err != nil {
|
||||
|
@ -39,15 +51,7 @@ func loadOrGenPrivateKey(t *testing.T) (pk *rsa.PrivateKey, generated bool) {
|
|||
if err != nil {
|
||||
t.Fatalf("error writing new private key to file %s: %v\n", localKeyfile, err)
|
||||
}
|
||||
|
||||
return pk, true
|
||||
}
|
||||
fmt.Println("Found server key " + serverKeyfile + ", loading...")
|
||||
pk, err := utils.LoadPrivateKeyFromFile(serverKeyfile)
|
||||
if err != nil {
|
||||
t.Fatalf("Could not load server's key from %v", serverKeyfile)
|
||||
}
|
||||
return pk, false
|
||||
return pk
|
||||
}
|
||||
|
||||
func printAndCountVerifedTimeline(t *testing.T, timeline []model.Message) int {
|
||||
|
@ -61,22 +65,33 @@ func printAndCountVerifedTimeline(t *testing.T, timeline []model.Message) int {
|
|||
return numVerified
|
||||
}
|
||||
|
||||
func serverCheck(serverAddr string) bool {
|
||||
rc, err := goricochet.Open(serverAddr)
|
||||
|
||||
if err == nil {
|
||||
// TODO should change libricochet-go to avoid accessing member to shutdown all goroutines.
|
||||
rc.Conn.Close()
|
||||
return true
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
conn.Close()
|
||||
return true
|
||||
}
|
||||
|
||||
func waitForPeerConnection(t *testing.T, peer *peer.CwtchPeer, server string) {
|
||||
for {
|
||||
servers := peer.GetServers()
|
||||
fmt.Println(servers)
|
||||
state, ok := servers[server]
|
||||
if ok {
|
||||
if state == connections.FAILED {
|
||||
t.Fatalf("%v could not connect to %v", peer.Profile.Onion, server)
|
||||
}
|
||||
if state != connections.AUTHENTICATED {
|
||||
time.Sleep(time.Second * 10)
|
||||
continue
|
||||
|
@ -96,19 +111,23 @@ func TestCwtchPeerIntegration(t *testing.T) {
|
|||
|
||||
// ***** Cwtch Server managment *****
|
||||
var server *cwtchserver.Server = nil
|
||||
serverKey, generatedKey := loadOrGenPrivateKey(t)
|
||||
serverAddr, _ := utils.GetOnionAddress(serverKey)
|
||||
|
||||
serverKey := loadPrivateKey(t)
|
||||
serverOnline := false
|
||||
if !generatedKey {
|
||||
var serverAddr string
|
||||
|
||||
if serverKey != nil {
|
||||
serverAddr, _ = utils.GetOnionAddress(serverKey)
|
||||
fmt.Printf("Checking if test server %v is online...\n", serverAddr)
|
||||
serverOnline = serverCheck(serverAddr)
|
||||
serverOnline = serverCheck(t, serverAddr)
|
||||
}
|
||||
|
||||
if !serverOnline {
|
||||
// launch app
|
||||
// launch app with new key
|
||||
fmt.Println("No server found!")
|
||||
serverKey = genPrivateKey(t)
|
||||
serverAddr, _ = utils.GetOnionAddress(serverKey)
|
||||
server = new(cwtchserver.Server)
|
||||
fmt.Printf("No server found\nStarting cwtch server...\n")
|
||||
fmt.Println("Starting cwtch server...")
|
||||
go server.Run(localKeyfile, 100)
|
||||
|
||||
// let tor get established
|
||||
|
@ -117,7 +136,6 @@ func TestCwtchPeerIntegration(t *testing.T) {
|
|||
fmt.Printf("Found existing cwtch server %v, using for tests...\n", serverAddr)
|
||||
}
|
||||
|
||||
time.Sleep(time.Second * 2)
|
||||
numGoRoutinesPostServer := runtime.NumGoroutine()
|
||||
|
||||
// ***** Peer setup *****
|
||||
|
@ -366,7 +384,8 @@ func TestCwtchPeerIntegration(t *testing.T) {
|
|||
numGoRoutinesStart, numGoRoutinesPostServer, numGoRoutinesPostPeerStart, numGoRoutinesPostServerConnect,
|
||||
numGoRoutinesPostAlice, numGoRotinesPostCarolConnect, numGoRoutinesPostBob, numGoRoutinesPostServerShutdown, numGoRoutinesPostCarol)
|
||||
|
||||
if numGoRoutinesPostServer != numGoRoutinesPostCarol {
|
||||
t.Errorf("Number of GoRoutines once server checks were completed (%v) does not match number of goRoutines after cleanup of peers and servers (%v), clean up failed, leak detected!", numGoRoutinesPostServer, numGoRoutinesPostCarol)
|
||||
if numGoRoutinesStart != numGoRoutinesPostCarol {
|
||||
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, numGoRoutinesPostCarol)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue