fix tests with nodisable flag

This commit is contained in:
Zack Scholl 2019-04-30 15:46:27 -06:00
parent a1e5a283f9
commit e72795985b
2 changed files with 62 additions and 53 deletions

View File

@ -52,6 +52,7 @@ type Options struct {
RelayPorts []string RelayPorts []string
Stdout bool Stdout bool
NoPrompt bool NoPrompt bool
DisableLocal bool
} }
type Client struct { type Client struct {
@ -226,56 +227,58 @@ func (c *Client) Send(options TransferOptions) (err error) {
// connect to the relay for messaging // connect to the relay for messaging
errchan := make(chan error, 1) errchan := make(chan error, 1)
// setup the relay locally if !c.Options.DisableLocal {
for _, port := range c.Options.RelayPorts { // setup the relay locally
go func(portStr string) { for _, port := range c.Options.RelayPorts {
debugString := "warn" go func(portStr string) {
if c.Options.Debug { debugString := "warn"
debugString = "debug" if c.Options.Debug {
debugString = "debug"
}
err = tcp.Run(debugString, portStr)
if err != nil {
panic(err)
}
}(port)
}
// look for peers first
go func() {
discoveries, err := peerdiscovery.Discover(peerdiscovery.Settings{
Limit: 1,
Payload: []byte("9009"),
Delay: 10 * time.Millisecond,
TimeLimit: 30 * time.Second,
})
log.Debugf("discoveries: %+v", discoveries)
if err == nil && len(discoveries) > 0 {
log.Debug("using local server")
} }
err = tcp.Run(debugString, portStr) }()
go func() {
time.Sleep(500 * time.Millisecond)
log.Debug("establishing connection")
conn, err := tcp.ConnectToTCPServer("localhost:"+c.Options.RelayPorts[0], c.Options.SharedSecret)
if err != nil { if err != nil {
panic(err) err = errors.Wrap(err, fmt.Sprintf("could not connect to %s", c.Options.RelayAddress))
return
} }
}(port) log.Debugf("connection established: %+v", conn)
for {
data, _ := conn.Receive()
if bytes.Equal(data, []byte("handshake")) {
break
}
}
c.conn[0] = conn
log.Debug("exchanged header message")
c.Options.RelayAddress = "localhost"
errchan <- c.transfer(options)
}()
} }
// look for peers first
go func() {
discoveries, err := peerdiscovery.Discover(peerdiscovery.Settings{
Limit: 1,
Payload: []byte("9009"),
Delay: 10 * time.Millisecond,
TimeLimit: 30 * time.Second,
})
log.Debugf("discoveries: %+v", discoveries)
if err == nil && len(discoveries) > 0 {
log.Debug("using local server")
}
}()
go func() {
time.Sleep(500 * time.Millisecond)
log.Debug("establishing connection")
conn, err := tcp.ConnectToTCPServer("localhost:"+c.Options.RelayPorts[0], c.Options.SharedSecret)
if err != nil {
err = errors.Wrap(err, fmt.Sprintf("could not connect to %s", c.Options.RelayAddress))
return
}
log.Debugf("connection established: %+v", conn)
for {
data, _ := conn.Receive()
if bytes.Equal(data, []byte("handshake")) {
break
}
}
c.conn[0] = conn
log.Debug("exchanged header message")
c.Options.RelayAddress = "localhost"
errchan <- c.transfer(options)
}()
go func() { go func() {
log.Debug("establishing connection") log.Debug("establishing connection")
conn, err := tcp.ConnectToTCPServer(c.Options.RelayAddress+":"+c.Options.RelayPorts[0], c.Options.SharedSecret) conn, err := tcp.ConnectToTCPServer(c.Options.RelayAddress+":"+c.Options.RelayPorts[0], c.Options.SharedSecret)
@ -302,14 +305,18 @@ func (c *Client) Send(options TransferOptions) (err error) {
// Receive will receive a file // Receive will receive a file
func (c *Client) Receive() (err error) { func (c *Client) Receive() (err error) {
// look for peers first // look for peers first
discoveries, err := peerdiscovery.Discover(peerdiscovery.Settings{ if !c.Options.DisableLocal {
Limit: 1,
Payload: []byte("ok"), discoveries, err := peerdiscovery.Discover(peerdiscovery.Settings{
Delay: 10 * time.Millisecond, Limit: 1,
TimeLimit: 100 * time.Millisecond, Payload: []byte("ok"),
}) Delay: 10 * time.Millisecond,
log.Debugf("discoveries: %+v", discoveries) TimeLimit: 100 * time.Millisecond,
log.Debug("establishing connection") })
_ = err
log.Debugf("discoveries: %+v", discoveries)
log.Debug("establishing connection")
}
c.conn[0], err = tcp.ConnectToTCPServer(c.Options.RelayAddress+":"+c.Options.RelayPorts[0], c.Options.SharedSecret) c.conn[0], err = tcp.ConnectToTCPServer(c.Options.RelayAddress+":"+c.Options.RelayPorts[0], c.Options.SharedSecret)
if err != nil { if err != nil {
err = errors.Wrap(err, fmt.Sprintf("could not connect to %s", c.Options.RelayAddress)) err = errors.Wrap(err, fmt.Sprintf("could not connect to %s", c.Options.RelayAddress))

View File

@ -30,6 +30,7 @@ func TestCroc(t *testing.T) {
RelayPorts: []string{"8081", "8082", "8083"}, RelayPorts: []string{"8081", "8082", "8083"},
Stdout: false, Stdout: false,
NoPrompt: true, NoPrompt: true,
DisableLocal: true,
}) })
if err != nil { if err != nil {
panic(err) panic(err)
@ -44,6 +45,7 @@ func TestCroc(t *testing.T) {
RelayPorts: []string{"8081", "8082", "8083"}, RelayPorts: []string{"8081", "8082", "8083"},
Stdout: false, Stdout: false,
NoPrompt: true, NoPrompt: true,
DisableLocal: true,
}) })
if err != nil { if err != nil {
panic(err) panic(err)