1818 */
1919package org .apache .pulsar .broker .loadbalance .impl ;
2020
21+ import static org .mockito .ArgumentMatchers .any ;
22+ import static org .mockito .Mockito .mock ;
23+ import static org .mockito .Mockito .when ;
2124import java .util .ArrayList ;
2225import java .util .List ;
2326import java .util .Optional ;
2629import java .util .concurrent .TimeUnit ;
2730import lombok .Cleanup ;
2831import lombok .extern .slf4j .Slf4j ;
32+ import org .apache .pulsar .broker .PulsarService ;
33+ import org .apache .pulsar .broker .ServiceConfiguration ;
2934import org .apache .pulsar .broker .loadbalance .LinuxInfoUtils ;
35+ import org .mockito .MockedStatic ;
36+ import org .mockito .Mockito ;
3037import org .testng .Assert ;
3138import org .testng .annotations .Test ;
3239
@@ -38,7 +45,7 @@ public void checkOverrideBrokerNicSpeedGbps() {
3845 @ Cleanup ("shutdown" )
3946 ScheduledExecutorService executorService = Executors .newSingleThreadScheduledExecutor ();
4047 LinuxBrokerHostUsageImpl linuxBrokerHostUsage =
41- new LinuxBrokerHostUsageImpl (1 , Optional .of (3.0 ), executorService );
48+ new LinuxBrokerHostUsageImpl (1 , Optional .of (3.0 ), new ArrayList <>(), executorService );
4249 List <String > nics = new ArrayList <>();
4350 nics .add ("1" );
4451 nics .add ("2" );
@@ -47,6 +54,34 @@ public void checkOverrideBrokerNicSpeedGbps() {
4754 Assert .assertEquals (totalLimit , 3.0 * 1000 * 1000 * 3 );
4855 }
4956
57+ @ Test
58+ public void checkOverrideBrokerNics () {
59+ try (MockedStatic <LinuxInfoUtils > mockedUtils = Mockito .mockStatic (LinuxInfoUtils .class )) {
60+ mockedUtils .when (() -> LinuxInfoUtils .getTotalNicUsage (any (), any (), any ())).thenReturn (3.0d );
61+ mockedUtils .when (LinuxInfoUtils ::getCpuUsageForEntireHost ).thenReturn (LinuxInfoUtils .ResourceUsage .empty ());
62+ List <String > nics = new ArrayList <>();
63+ nics .add ("1" );
64+ nics .add ("2" );
65+ nics .add ("3" );
66+ ServiceConfiguration config = new ServiceConfiguration ();
67+ config .setLoadBalancerOverrideBrokerNicSpeedGbps (Optional .of (3.0d ));
68+ config .setLoadBalancerOverrideBrokerNics (nics );
69+ PulsarService pulsarService = mock (PulsarService .class );
70+ when (pulsarService .getConfiguration ()).thenReturn (config );
71+ @ Cleanup ("shutdown" )
72+ ScheduledExecutorService executorService = Executors .newSingleThreadScheduledExecutor ();
73+ when (pulsarService .getLoadManagerExecutor ()).thenReturn (executorService );
74+ LinuxBrokerHostUsageImpl linuxBrokerHostUsage = new LinuxBrokerHostUsageImpl (pulsarService );
75+ linuxBrokerHostUsage .calculateBrokerHostUsage ();
76+ double totalLimit = linuxBrokerHostUsage .getTotalNicLimitWithConfiguration (nics );
77+ Assert .assertEquals (totalLimit , 3.0 * 1000 * 1000 * 3 );
78+ double totalNicLimitRx = linuxBrokerHostUsage .getBrokerHostUsage ().getBandwidthIn ().limit ;
79+ double totalNicLimitTx = linuxBrokerHostUsage .getBrokerHostUsage ().getBandwidthOut ().limit ;
80+ Assert .assertEquals (totalNicLimitRx , 3.0 * 1000 * 1000 * 3 );
81+ Assert .assertEquals (totalNicLimitTx , 3.0 * 1000 * 1000 * 3 );
82+ }
83+ }
84+
5085 @ Test
5186 public void testCpuUsage () throws InterruptedException {
5287 if (!LinuxInfoUtils .isLinux ()) {
@@ -56,7 +91,8 @@ public void testCpuUsage() throws InterruptedException {
5691 @ Cleanup ("shutdown" )
5792 ScheduledExecutorService executorService = Executors .newSingleThreadScheduledExecutor ();
5893 LinuxBrokerHostUsageImpl linuxBrokerHostUsage =
59- new LinuxBrokerHostUsageImpl (Integer .MAX_VALUE , Optional .empty (), executorService );
94+ new LinuxBrokerHostUsageImpl (Integer .MAX_VALUE , Optional .empty (),
95+ new ArrayList <>(), executorService );
6096
6197 linuxBrokerHostUsage .calculateBrokerHostUsage ();
6298 TimeUnit .SECONDS .sleep (1 );
0 commit comments