@@ -703,3 +703,111 @@ func TestAddPeerAccessOutbound(t *testing.T) {
703
703
expecedScore = peerSlotStatus {state : peerStatusTemporary }
704
704
require .Equal (t , expecedScore , score )
705
705
}
706
+
707
+ // TestRemovePeerAccess asserts `removePeerAccess` correctly update the
708
+ // accessman's internal state based on the peer's status.
709
+ func TestRemovePeerAccess (t * testing.T ) {
710
+ t .Parallel ()
711
+
712
+ // Create a testing accessMan.
713
+ a := & accessMan {
714
+ peerChanInfo : make (map [string ]channeldb.ChanCount ),
715
+ peerScores : make (map [string ]peerSlotStatus ),
716
+ }
717
+
718
+ // numRestrictedExpected specifies the final value to expect once the
719
+ // test finishes.
720
+ var numRestrictedExpected int
721
+
722
+ // peer1 exists with an open channel, which should not be removed. Since
723
+ // it has protected status, the numRestricted should stay unchanged.
724
+ peer1 := "peer1"
725
+ a .peerChanInfo [peer1 ] = channeldb.ChanCount {
726
+ HasOpenOrClosedChan : true ,
727
+ }
728
+ peer1Access := peerStatusProtected
729
+ a .peerScores [peer1 ] = peerSlotStatus {state : peer1Access }
730
+
731
+ // peer2 exists with a pending channel, which should not be removed.
732
+ // Since it has temporary status, the numRestricted should stay
733
+ // unchanged.
734
+ peer2 := "peer2"
735
+ a .peerChanInfo [peer2 ] = channeldb.ChanCount {
736
+ PendingOpenCount : 1 ,
737
+ }
738
+ peer2Access := peerStatusTemporary
739
+ a .peerScores [peer2 ] = peerSlotStatus {state : peer2Access }
740
+
741
+ // peer3 exists without any channels, which will be removed. Since it
742
+ // has restricted status, the numRestricted should be decremented.
743
+ peer3 := "peer3"
744
+ a .peerChanInfo [peer3 ] = channeldb.ChanCount {}
745
+ peer3Access := peerStatusRestricted
746
+ a .peerScores [peer3 ] = peerSlotStatus {state : peer3Access }
747
+ numRestrictedExpected --
748
+
749
+ // peer4 exists with a score and a temporary status, which will be
750
+ // removed.
751
+ peer4 := "peer4"
752
+ peer4Access := peerStatusTemporary
753
+ a .peerScores [peer4 ] = peerSlotStatus {state : peer4Access }
754
+
755
+ // peer5 doesn't exist, removing it will be a NOOP.
756
+ peer5 := "peer5"
757
+
758
+ // We now assert `removePeerAccess` behaves as expected.
759
+ //
760
+ // Remove peer1 should change nothing.
761
+ a .removePeerAccess (peer1 )
762
+
763
+ // peer1 should be removed from peerScores but not peerChanInfo.
764
+ _ , found := a .peerScores [peer1 ]
765
+ require .False (t , found )
766
+ _ , found = a .peerChanInfo [peer1 ]
767
+ require .True (t , found )
768
+
769
+ // Remove peer2 should change nothing.
770
+ a .removePeerAccess (peer2 )
771
+
772
+ // peer2 should be removed from peerScores but not peerChanInfo.
773
+ _ , found = a .peerScores [peer2 ]
774
+ require .False (t , found )
775
+ _ , found = a .peerChanInfo [peer2 ]
776
+ require .True (t , found )
777
+
778
+ // Remove peer3 should remove it from the maps.
779
+ a .removePeerAccess (peer3 )
780
+
781
+ // peer3 should be removed from peerScores and peerChanInfo.
782
+ _ , found = a .peerScores [peer3 ]
783
+ require .False (t , found )
784
+ _ , found = a .peerChanInfo [peer3 ]
785
+ require .False (t , found )
786
+
787
+ // Remove peer4 should remove it from the maps.
788
+ a .removePeerAccess (peer4 )
789
+
790
+ // peer4 should be removed from peerScores and NOT be found in
791
+ // peerChanInfo.
792
+ _ , found = a .peerScores [peer4 ]
793
+ require .False (t , found )
794
+ _ , found = a .peerChanInfo [peer4 ]
795
+ require .False (t , found )
796
+
797
+ // Remove peer5 should be NOOP.
798
+ a .removePeerAccess (peer5 )
799
+
800
+ // peer5 should NOT be found.
801
+ _ , found = a .peerScores [peer5 ]
802
+ require .False (t , found )
803
+ _ , found = a .peerChanInfo [peer5 ]
804
+ require .False (t , found )
805
+
806
+ // Finally, assert the numRestricted is decremented as expected. Given
807
+ // we only have peer3 which has restricted access, it should decrement
808
+ // once.
809
+ //
810
+ // NOTE: The value is actually negative here, which is allowed in
811
+ // accessman.
812
+ require .EqualValues (t , numRestrictedExpected , a .numRestricted )
813
+ }
0 commit comments