Sfoglia il codice sorgente

lib/connections: Refactor status for testing (ref #6361) (#6362)

Simon Frei 5 anni fa
parent
commit
680b0b14db
2 ha cambiato i file con 56 aggiunte e 8 eliminazioni
  1. 38 0
      lib/connections/connections_test.go
  2. 18 8
      lib/connections/service.go

+ 38 - 0
lib/connections/connections_test.go

@@ -7,6 +7,8 @@
 package connections
 
 import (
+	"context"
+	"errors"
 	"net/url"
 	"testing"
 
@@ -167,3 +169,39 @@ func TestGetDialer(t *testing.T) {
 		}
 	}
 }
+
+func TestConnectionStatus(t *testing.T) {
+	s := newConnectionStatusHandler()
+
+	addr := "testAddr"
+	testErr := errors.New("testErr")
+
+	if stats := s.ConnectionStatus(); len(stats) != 0 {
+		t.Fatal("newly created connectionStatusHandler isn't empty:", len(stats))
+	}
+
+	check := func(in, out error) {
+		t.Helper()
+		s.setConnectionStatus(addr, in)
+		switch stat, ok := s.ConnectionStatus()[addr]; {
+		case !ok:
+			t.Fatal("entry missing")
+		case out == nil:
+			if stat.Error != nil {
+				t.Fatal("expected nil error, got", stat.Error)
+			}
+		case *stat.Error != out.Error():
+			t.Fatalf("expected %v error, got %v", out.Error(), *stat.Error)
+		}
+	}
+
+	check(nil, nil)
+
+	check(context.Canceled, nil)
+
+	check(testErr, testErr)
+
+	check(context.Canceled, testErr)
+
+	check(nil, nil)
+}

+ 18 - 8
lib/connections/service.go

@@ -110,6 +110,8 @@ type ConnectionStatusEntry struct {
 
 type service struct {
 	*suture.Supervisor
+	connectionStatusHandler
+
 	cfg                  config.Wrapper
 	myID                 protocol.DeviceID
 	model                Model
@@ -127,9 +129,6 @@ type service struct {
 	listeners          map[string]genericListener
 	listenerTokens     map[string]suture.ServiceToken
 	listenerSupervisor *suture.Supervisor
-
-	connectionStatusMut sync.RWMutex
-	connectionStatus    map[string]ConnectionStatusEntry // address -> latest error/status
 }
 
 func NewService(cfg config.Wrapper, myID protocol.DeviceID, mdl Model, tlsCfg *tls.Config, discoverer discover.Finder, bepProtocolName string, tlsDefaultCommonName string, evLogger events.Logger) Service {
@@ -140,6 +139,8 @@ func NewService(cfg config.Wrapper, myID protocol.DeviceID, mdl Model, tlsCfg *t
 			},
 			PassThroughPanics: true,
 		}),
+		connectionStatusHandler: newConnectionStatusHandler(),
+
 		cfg:                  cfg,
 		myID:                 myID,
 		model:                mdl,
@@ -168,9 +169,6 @@ func NewService(cfg config.Wrapper, myID protocol.DeviceID, mdl Model, tlsCfg *t
 			FailureBackoff:    600 * time.Second,
 			PassThroughPanics: true,
 		}),
-
-		connectionStatusMut: sync.NewRWMutex(),
-		connectionStatus:    make(map[string]ConnectionStatusEntry),
 	}
 	cfg.Subscribe(service)
 
@@ -702,7 +700,19 @@ func (s *service) ListenerStatus() map[string]ListenerStatusEntry {
 	return result
 }
 
-func (s *service) ConnectionStatus() map[string]ConnectionStatusEntry {
+type connectionStatusHandler struct {
+	connectionStatusMut sync.RWMutex
+	connectionStatus    map[string]ConnectionStatusEntry // address -> latest error/status
+}
+
+func newConnectionStatusHandler() connectionStatusHandler {
+	return connectionStatusHandler{
+		connectionStatusMut: sync.NewRWMutex(),
+		connectionStatus:    make(map[string]ConnectionStatusEntry),
+	}
+}
+
+func (s *connectionStatusHandler) ConnectionStatus() map[string]ConnectionStatusEntry {
 	result := make(map[string]ConnectionStatusEntry)
 	s.connectionStatusMut.RLock()
 	for k, v := range s.connectionStatus {
@@ -712,7 +722,7 @@ func (s *service) ConnectionStatus() map[string]ConnectionStatusEntry {
 	return result
 }
 
-func (s *service) setConnectionStatus(address string, err error) {
+func (s *connectionStatusHandler) setConnectionStatus(address string, err error) {
 	if errors.Cause(err) == context.Canceled {
 		return
 	}