diff --git a/acn.go b/acn.go index af5f497..9ca82df 100644 --- a/acn.go +++ b/acn.go @@ -38,7 +38,7 @@ type ACN interface { // On ACN error state it returns -2 GetBootstrapStatus() (int, string) // WaitTillBootstrapped Blocks until underlying network is bootstrapped - WaitTillBootstrapped() + WaitTillBootstrapped() error // Sets the callback function to be called when ACN status changes SetStatusCallback(callback func(int, string)) diff --git a/error_acn.go b/error_acn.go index 425356c..d9dbb2b 100644 --- a/error_acn.go +++ b/error_acn.go @@ -1,6 +1,7 @@ package connectivity import ( + "errors" "fmt" "net" ) @@ -19,7 +20,8 @@ func (e ErrorACN) GetBootstrapStatus() (int, string) { return -1, "error initializing tor" } -func (e ErrorACN) WaitTillBootstrapped() { +func (e ErrorACN) WaitTillBootstrapped() error { + return errors.New("error initializing tor") } func (e *ErrorACN) SetStatusCallback(callback func(int, string)) { diff --git a/localProvider.go b/localProvider.go index 6c66499..656d0ba 100644 --- a/localProvider.go +++ b/localProvider.go @@ -56,7 +56,8 @@ func (lp *localProvider) GetVersion() string { } // WaitTillBootstrapped Blocks until underlying network is bootstrapped -func (lp *localProvider) WaitTillBootstrapped() { +func (lp *localProvider) WaitTillBootstrapped() error { + return nil } func (lp *localProvider) Listen(identity PrivateKey, port int) (ListenService, error) { diff --git a/proxy_acn.go b/proxy_acn.go index 809f5b6..0161085 100644 --- a/proxy_acn.go +++ b/proxy_acn.go @@ -37,8 +37,8 @@ func (p *ProxyACN) GetBootstrapStatus() (int, string) { return p.acn.GetBootstrapStatus() } -func (p *ProxyACN) WaitTillBootstrapped() { - p.acn.WaitTillBootstrapped() +func (p *ProxyACN) WaitTillBootstrapped() error { + return p.acn.WaitTillBootstrapped() } func (p *ProxyACN) SetStatusCallback(callback func(int, string)) { diff --git a/testing/launch_tor_integration_test.go b/testing/launch_tor_integration_test.go index da031b1..7611c74 100644 --- a/testing/launch_tor_integration_test.go +++ b/testing/launch_tor_integration_test.go @@ -32,7 +32,11 @@ func TestLaunchTor(t *testing.T) { if err != nil { t.Fatalf("tor failed to start: %v", err) } else { - acn.WaitTillBootstrapped() + err := acn.WaitTillBootstrapped() + if err != nil { + t.Fatalf("error bootstrapping tor %v", err) + } + if pid, err := acn.GetPID(); err == nil { t.Logf("tor pid: %v", pid) } else { diff --git a/tor/torProvider.go b/tor/torProvider.go index 2a3e182..ee2c6ef 100644 --- a/tor/torProvider.go +++ b/tor/torProvider.go @@ -72,6 +72,7 @@ type torProvider struct { statusCallback func(int, string) lastRestartTime time.Time authenticator tor.Authenticator + isClosed bool } func (ols *onionListenService) AddressFull() string { @@ -157,14 +158,15 @@ func (tp *torProvider) GetVersion() string { } // WaitTillBootstrapped Blocks until underlying network is bootstrapped -func (tp *torProvider) WaitTillBootstrapped() { - for { +func (tp *torProvider) WaitTillBootstrapped() error { + for !tp.isClosed { progress, _ := tp.GetBootstrapStatus() if progress == 100 { - break + return nil } time.Sleep(100 * time.Millisecond) } + return errors.New("close called before bootstrap") } func (tp *torProvider) Listen(identity connectivity.PrivateKey, port int) (connectivity.ListenService, error) { @@ -287,9 +289,9 @@ func (tp *torProvider) Close() { for _, child := range tp.childListeners { child.Close() } - tp.lock.Lock() defer tp.lock.Unlock() + tp.isClosed = true tp.breakChan <- true if tp.t != nil { tp.t.Close()