@@ -62,6 +62,47 @@ public class JMXStartStopTest {
6262
6363 private static final boolean verbose = false ;
6464
65+ /**
66+ * Dynamically allocates two distinct ports using {@linkplain java.net.ServerSocket}
67+ * It keeps each of those ports blocked until it is first accessed by its getter
68+ */
69+ private static class PortAllocator {
70+ private final int port1 , port2 ;
71+ private final ServerSocket ss1 , ss2 ;
72+ PortAllocator () {
73+ try {
74+ ss1 = new ServerSocket (0 );
75+ ss2 = new ServerSocket (0 );
76+ port1 = ss1 .getLocalPort ();
77+ port2 = ss2 .getLocalPort ();
78+ } catch (IOException e ) {
79+ throw new Error ("Error while obtaining free ports" , e );
80+ }
81+ }
82+
83+ public int getPort1 () {
84+ if (!ss1 .isClosed ()) {
85+ try {
86+ ss1 .close ();
87+ } catch (IOException e ) {
88+ // just ignore
89+ }
90+ }
91+ return port1 ;
92+ }
93+
94+ public int getPort2 () {
95+ if (!ss2 .isClosed ()) {
96+ try {
97+ ss2 .close ();
98+ } catch (IOException e ) {
99+ // just ignore
100+ }
101+ }
102+ return port2 ;
103+ }
104+ }
105+
65106 private static void dbg_print (String msg ){
66107 if (verbose ) {
67108 System .out .println ("DBG: " +msg );
@@ -407,29 +448,28 @@ private static void jcmd(String target, final Consumer<String> c, String ... com
407448 private static final String CMD_STOP = "ManagementAgent.stop" ;
408449 private static final String CMD_START = "ManagementAgent.start" ;
409450 private static final String CMD_START_LOCAL = "ManagementAgent.start_local" ;
410- private static final int port1 = 50234 ;
411- private static final int port2 = 50235 ;
412451
413452 static void test_01 () throws Exception {
414453 // Run an app with JMX enabled stop it and
415454 // restart on other port
416455
417456 System .out .println ("**** Test one ****" );
457+ PortAllocator pa = new PortAllocator ();
418458
419459 Something s = doSomething (
420460 "test_01" ,
421- "-Dcom.sun.management.jmxremote.port=" + port1 ,
461+ "-Dcom.sun.management.jmxremote.port=" + pa . getPort1 () ,
422462 "-Dcom.sun.management.jmxremote.authenticate=false" ,
423463 "-Dcom.sun.management.jmxremote.ssl=false" );
424464
425465 try {
426- testConnect (port1 );
466+ testConnect (pa . getPort1 () );
427467
428468 jcmd (CMD_STOP );
429- testNoConnect (port1 );
469+ testNoConnect (pa . getPort1 () );
430470
431- jcmd (CMD_START , "jmxremote.port=" + port2 );
432- testConnect (port2 );
471+ jcmd (CMD_START , "jmxremote.port=" + pa . getPort2 () );
472+ testConnect (pa . getPort2 () );
433473 } finally {
434474 s .stop ();
435475 }
@@ -442,14 +482,16 @@ static void test_02() throws Exception {
442482 System .out .println ("**** Test two ****" );
443483
444484 Something s = doSomething ("test_02" );
485+ PortAllocator pa = new PortAllocator ();
445486 try {
446487 jcmd (CMD_START ,
447- "jmxremote.port=" + port1 ,
488+ "jmxremote.port=" + pa . getPort1 () ,
448489 "jmxremote.authenticate=false" ,
449490 "jmxremote.ssl=false" );
450491
451- testConnect (port1 );
492+ testConnect (pa . getPort1 () );
452493 } finally {
494+ // debugPortUsage(pa);
453495 s .stop ();
454496 }
455497 }
@@ -461,23 +503,24 @@ static void test_03() throws Exception {
461503 System .out .println ("**** Test three ****" );
462504
463505 Something s = doSomething ("test_03" );
506+ PortAllocator pa = new PortAllocator ();
464507 try {
465508 jcmd (CMD_START ,
466- "jmxremote.port=" + port1 ,
509+ "jmxremote.port=" + pa . getPort1 () ,
467510 "jmxremote.authenticate=false" ,
468511 "jmxremote.ssl=false" );
469512
470513 // Second agent shouldn't start
471514 jcmd (CMD_START ,
472- "jmxremote.port=" + port2 ,
515+ "jmxremote.port=" + pa . getPort2 () ,
473516 "jmxremote.authenticate=false" ,
474517 "jmxremote.ssl=false" );
475518
476519 // First agent should connect
477- testConnect (port1 );
520+ testConnect (pa . getPort1 () );
478521
479522 // Second agent should not connect
480- testNoConnect (port2 );
523+ testNoConnect (pa . getPort2 () );
481524 } finally {
482525 s .stop ();
483526 }
@@ -490,15 +533,15 @@ static void test_04() throws Exception {
490533 System .out .println ("**** Test four ****" );
491534
492535 Something s = doSomething ("test_04" );
493-
536+ PortAllocator pa = new PortAllocator ();
494537 try {
495538 jcmd (CMD_START ,
496- "jmxremote.port=" + port1 ,
497- "jmxremote.rmi.port=" + port2 ,
539+ "jmxremote.port=" + pa . getPort1 () ,
540+ "jmxremote.rmi.port=" + pa . getPort2 () ,
498541 "jmxremote.authenticate=false" ,
499542 "jmxremote.ssl=false" );
500543
501- testConnect (port1 , port2 );
544+ testConnect (pa . getPort1 (), pa . getPort2 () );
502545 } finally {
503546 s .stop ();
504547 }
@@ -511,10 +554,11 @@ static void test_05() throws Exception {
511554 System .out .println ("**** Test five ****" );
512555
513556 Something s = doSomething ("test_05" );
557+ PortAllocator pa = new PortAllocator ();
514558 try {
515559 jcmd (CMD_START_LOCAL );
516560
517- testNoConnect (port1 );
561+ testNoConnect (pa . getPort1 () );
518562 testConnectLocal (s .getPid ());
519563 } finally {
520564 s .stop ();
@@ -533,14 +577,14 @@ static void test_06() throws Exception {
533577 System .out .println ("**** Test six ****" );
534578
535579 Something s = doSomething ("test_06" );
536-
580+ PortAllocator pa = new PortAllocator ();
537581 try {
538582 jcmd (CMD_START ,
539- "jmxremote.port=" + port1 ,
583+ "jmxremote.port=" + pa . getPort1 () ,
540584 "jmxremote.authenticate=false" ,
541585 "jmxremote.ssl=false" );
542586
543- testConnect (port1 , port2 );
587+ testConnect (pa . getPort1 (), pa . getPort2 () );
544588
545589 final boolean [] checks = new boolean [3 ];
546590 jcmd (
@@ -550,7 +594,7 @@ static void test_06() throws Exception {
550594 }
551595 },
552596 CMD_START ,
553- "jmxremote.port=" + port1 ,
597+ "jmxremote.port=" + pa . getPort1 () ,
554598 "jmxremote.authenticate=false" ,
555599 "jmxremote.ssl=false" );
556600
@@ -561,7 +605,7 @@ static void test_06() throws Exception {
561605 }
562606 },
563607 CMD_START ,
564- "jmxremote.port=" + port2 ,
608+ "jmxremote.port=" + pa . getPort2 () ,
565609 "jmxremote.authenticate=false" ,
566610 "jmxremote.ssl=false" );
567611
@@ -578,15 +622,15 @@ static void test_06() throws Exception {
578622 },
579623 CMD_START ,
580624 "jmxremote.port=" + ss .getLocalPort (),
581- "jmxremote.rmi.port=" + port2 ,
625+ "jmxremote.rmi.port=" + pa . getPort2 () ,
582626 "jmxremote.authenticate=false" ,
583627 "jmxremote.ssl=false" );
584628 if (!checks [0 ]) {
585- throw new Exception ("Starting agent on port " + port1 + " should " +
629+ throw new Exception ("Starting agent on port " + pa . getPort1 () + " should " +
586630 "report an invalid agent state" );
587631 }
588632 if (!checks [1 ]) {
589- throw new Exception ("Starting agent on poprt " + port2 + " should " +
633+ throw new Exception ("Starting agent on poprt " + pa . getPort2 () + " should " +
590634 "report an invalid agent state" );
591635 }
592636 if (!checks [2 ]) {
@@ -609,16 +653,17 @@ private static void test_07() throws Exception {
609653 "test_07" ,
610654 "-Dcom.sun.management.jmxremote.authenticate=false" ,
611655 "-Dcom.sun.management.jmxremote.ssl=true" );
656+ PortAllocator pa = new PortAllocator ();
612657
613658 try {
614- testNoConnect (port1 );
659+ testNoConnect (pa . getPort1 () );
615660 jcmd (
616661 CMD_START ,
617- "jmxremote.port=" + port2 ,
662+ "jmxremote.port=" + pa . getPort2 () ,
618663 "jmxremote.authenticate=false" ,
619664 "jmxremote.ssl=false"
620665 );
621- testConnect (port2 );
666+ testConnect (pa . getPort2 () );
622667 } finally {
623668 s .stop ();
624669 }
@@ -631,28 +676,29 @@ static void test_08() throws Exception {
631676 // make sure these properties overridden corectly
632677
633678 System .out .println ("**** Test eight ****" );
679+ PortAllocator pa = new PortAllocator ();
634680
635681 Something s = doSomething (
636682 "test_08" ,
637- "-Dcom.sun.management.jmxremote.port=" + port1 ,
683+ "-Dcom.sun.management.jmxremote.port=" + pa . getPort1 () ,
638684 "-Dcom.sun.management.jmxremote.authenticate=false" ,
639685 "-Dcom.sun.management.jmxremote.ssl=true" );
640686
641687 try {
642- testNoConnect (port1 );
688+ testNoConnect (pa . getPort1 () );
643689
644690 jcmd (CMD_STOP );
645691
646- testNoConnect (port1 );
692+ testNoConnect (pa . getPort1 () );
647693
648694 jcmd (
649695 CMD_START ,
650- "jmxremote.port=" + port2 ,
696+ "jmxremote.port=" + pa . getPort2 () ,
651697 "jmxremote.authenticate=false" ,
652698 "jmxremote.ssl=false"
653699 );
654700
655- testConnect (port2 );
701+ testConnect (pa . getPort2 () );
656702 } finally {
657703 s .stop ();
658704 }
@@ -673,22 +719,23 @@ static void test_09() throws Exception {
673719 TEST_SRC + File .separator + "management_cl.properties" ,
674720 "-Dcom.sun.management.jmxremote.authenticate=false"
675721 );
722+ PortAllocator pa = new PortAllocator ();
676723
677724 try {
678- testNoConnect (port1 );
725+ testNoConnect (pa . getPort1 () );
679726
680727 jcmd (CMD_STOP );
681728
682- testNoConnect (port1 );
729+ testNoConnect (pa . getPort1 () );
683730
684731 jcmd (CMD_START ,
685732 "config.file=" + TEST_SRC + File .separator +
686733 "management_jcmd.properties" ,
687734 "jmxremote.authenticate=false" ,
688- "jmxremote.port=" + port2
735+ "jmxremote.port=" + pa . getPort2 ()
689736 );
690737
691- testConnect (port2 );
738+ testConnect (pa . getPort2 () );
692739 } finally {
693740 s .stop ();
694741 }
@@ -702,29 +749,30 @@ static void test_10() throws Exception {
702749 // make sure these properties overridden corectly
703750
704751 System .out .println ("**** Test ten ****" );
752+ PortAllocator pa = new PortAllocator ();
705753
706754 Something s = doSomething (
707755 "test_10" ,
708- "-Dcom.sun.management.jmxremote.port=" + port1 ,
756+ "-Dcom.sun.management.jmxremote.port=" + pa . getPort1 () ,
709757 "-Dcom.sun.management.jmxremote.authenticate=false" ,
710758 "-Dcom.sun.management.jmxremote.ssl=true" );
711759
712760 try {
713- testNoConnect (port1 );
761+ testNoConnect (pa . getPort1 () );
714762
715763 jcmd (CMD_STOP );
716764 jcmd (CMD_START ,
717765 "jmxremote.ssl=false" ,
718- "jmxremote.port=" + port1
766+ "jmxremote.port=" + pa . getPort1 ()
719767 );
720768 testConnect (port1 );
721769
722770 jcmd (CMD_STOP );
723771 jcmd (CMD_START ,
724- "jmxremote.port=" + port1
772+ "jmxremote.port=" + pa . getPort1 ()
725773 );
726774
727- testNoConnect (port1 );
775+ testNoConnect (pa . getPort1 () );
728776 } finally {
729777 s .stop ();
730778 }
@@ -736,14 +784,15 @@ static void test_11() throws Exception {
736784 // make sure local agent is not affected
737785
738786 System .out .println ("**** Test eleven ****" );
787+ PortAllocator pa = new PortAllocator ();
739788
740789 Something s = doSomething (
741790 "test_11" ,
742- "-Dcom.sun.management.jmxremote.port=" + port1 ,
791+ "-Dcom.sun.management.jmxremote.port=" + pa . getPort1 () ,
743792 "-Dcom.sun.management.jmxremote.authenticate=false" ,
744793 "-Dcom.sun.management.jmxremote.ssl=false" );
745794 try {
746- testConnect (port1 );
795+ testConnect (pa . getPort1 () );
747796 jcmd (CMD_STOP );
748797 testConnectLocal (s .getPid ());
749798 } finally {
@@ -758,9 +807,10 @@ static void test_12() throws Exception {
758807 System .out .println ("**** Test twelve ****" );
759808
760809 Something s = doSomething ("test_12" );
810+ PortAllocator pa = new PortAllocator ();
761811
762812 try {
763- testNoConnect (port1 );
813+ testNoConnect (pa . getPort1 () );
764814 jcmd (CMD_START + "_local" );
765815
766816 testConnectLocal (s .getPid ());
0 commit comments